WPF - 不透明度属性第二次不起作用
我有以下代码:
// 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
除了将 FillBehavior 设置为 Stop 之外,正如您提到的,还有其他几种方法可以确保不会发生此属性锁定:
这实际上是一个非常常见的混淆来源,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:
This is actually a very common source of confusion and MSDN has a decent-sized writeup on this topic.
比较此问题的答案。
您应该通过动画再次更改不透明度或以某种方式禁用动画。
Compare the answer to this question.
You should change the opacity again via an animation or somehow disable your animation.