WPF - 不透明度属性第二次不起作用

发布于 2024-10-16 13:37:22 字数 1447 浏览 3 评论 0原文

我有以下代码:

    // Fade out the photo, and after, fade in canvas
double DurationSeconds = duration.TimeSpan.TotalSeconds;

System.Windows.Media.Animation.Storyboard storyboard = new System.Windows.Media.Animation.Storyboard();

System.Windows.Media.Animation.DoubleAnimation animScreenshot = new System.Windows.Media.Animation.DoubleAnimation(1, 0, new Duration(TimeSpan.FromSeconds(DurationSeconds / 3)));
System.Windows.Media.Animation.Storyboard.SetTargetName(animScreenshot, DrawingCanvasTransition.Name);
System.Windows.Media.Animation.Storyboard.SetTargetProperty(animScreenshot, new PropertyPath(Image.OpacityProperty));

// !!!
RectangleTransition.Opacity = 1; // <----------------- PROBLEM IS HERE
RectangleTransition.UpdateLayout();

System.Windows.Media.Animation.DoubleAnimation animRect = new System.Windows.Media.Animation.DoubleAnimation(1, 0, new Duration(TimeSpan.FromSeconds(DurationSeconds / 3)));
animRect.BeginTime = TimeSpan.FromSeconds( DurationSeconds * .66);
System.Windows.Media.Animation.Storyboard.SetTargetName(animRect, RectangleTransition.Name);
System.Windows.Media.Animation.Storyboard.SetTargetProperty(animRect, new PropertyPath(Rectangle.OpacityProperty));

storyboard.Children.Add(animScreenshot);
storyboard.Children.Add(animRect);

// And start!
storyboard.Begin(this);

问题是第一次调用该函数时,动画效果完美。 第二次,不透明度属性根本没有改变。可能是什么问题? 在调试器中,.Opacity 属性 = 0,即使在应更改为 1 的行之后也是如此。 然而动画确实有效。

I have the following code:

    // Fade out the photo, and after, fade in canvas
double DurationSeconds = duration.TimeSpan.TotalSeconds;

System.Windows.Media.Animation.Storyboard storyboard = new System.Windows.Media.Animation.Storyboard();

System.Windows.Media.Animation.DoubleAnimation animScreenshot = new System.Windows.Media.Animation.DoubleAnimation(1, 0, new Duration(TimeSpan.FromSeconds(DurationSeconds / 3)));
System.Windows.Media.Animation.Storyboard.SetTargetName(animScreenshot, DrawingCanvasTransition.Name);
System.Windows.Media.Animation.Storyboard.SetTargetProperty(animScreenshot, new PropertyPath(Image.OpacityProperty));

// !!!
RectangleTransition.Opacity = 1; // <----------------- PROBLEM IS HERE
RectangleTransition.UpdateLayout();

System.Windows.Media.Animation.DoubleAnimation animRect = new System.Windows.Media.Animation.DoubleAnimation(1, 0, new Duration(TimeSpan.FromSeconds(DurationSeconds / 3)));
animRect.BeginTime = TimeSpan.FromSeconds( DurationSeconds * .66);
System.Windows.Media.Animation.Storyboard.SetTargetName(animRect, RectangleTransition.Name);
System.Windows.Media.Animation.Storyboard.SetTargetProperty(animRect, new PropertyPath(Rectangle.OpacityProperty));

storyboard.Children.Add(animScreenshot);
storyboard.Children.Add(animRect);

// And start!
storyboard.Begin(this);

The problem is that the first time the function is called, the animation works perfectly.
The second time, the opacity property doesn't change at all. What could be the problem?
In the debugger, the .Opacity property = 0 even after the line where it should change to 1.
The animations do however work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半衬遮猫 2024-10-23 13:37:22

除了将 FillBehavior 设置为 Stop 之外,正如您提到的,还有其他几种方法可以确保不会发生此属性锁定:

  • 通过处理动画的 Completed 事件来删除动画对象。 (例如,“storyboard.Remove(myObject)”)
  • 通过设置“自动反转”使动画可反转(如果您希望最终结果与开始时的结果不同,显然这不起作用)。

这实际上是一个非常常见的混淆来源,MSDN 有一个大小合适的文章 关于这个主题。

Aside from setting FillBehavior to Stop, as you mentioned there are a couple of other ways to make sure this property locking doesn't happen:

  • Remove the animation object by handling the animation's Completed event. (e.g., "storyboard.Remove(myObject)")
  • Make the animation reversible by setting the AutoReverse (obviously this doesn't work if you want the end result to be different from how you started).

This is actually a very common source of confusion and MSDN has a decent-sized writeup on this topic.

彼岸花ソ最美的依靠 2024-10-23 13:37:22

比较此问题的答案。
您应该通过动画再次更改不透明度或以某种方式禁用动画。

Compare the answer to this question.
You should change the opacity again via an animation or somehow disable your animation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文