2011-02-10 96 views
0

我有以下代码:WPF - Opacity属性不起作用第二次

// 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的行之后。 然而,动画确实有效。

+0

RectangleTransition是否有与之相关的画笔,以便确实更改颜色的不透明度? – 2011-02-10 15:47:24

回答

2

除了设置FillBehavior停止,你提到有一些其他的方式,以确保该物业锁不会发生:

  • 通过处理动画的完成事件删除动画对象。 (例如,“storyboard.Remove(myObject)”)
  • 通过设置AutoReverse使动画可逆(显然,如果您希望最终结果与您的启动方式不同,则此功能无效)。

这实际上是一个非常常见的混淆来源,MSDN在此主题上有一个decent-sized writeup

1

将答案与this question进行比较。
您应该通过动画再次更改不透明度,或者以某种方式禁用您的动画。

相关问题