在设置了一个对象的Opacity动画之后,再为该对象设置一个Opacity值将会无效,奇怪的是也没有异常抛出,而且在该语句之后的语句也都不执行了。例如:

DoubleAnimation myDA = new DoubleAnimation(0.8, 0.0, new Duration(TimeSpan.FromMilliseconds(800)));

Storyboard.SetTargetName(myDA, “myObj”);
Storyboard.SetTargetProperty(myDA, new PropertyPath(Canvas.OpacityProperty));

Storyboard mySB = new Storyboard();
mySB.Children.Add(myDA);
mySB.Begin(myObj);

若在此后再设置该对象的Opacity值将会无效,例如:

myObj.Opacity = 1.0;

且该句之后的语句都不会执行。想想可能是因为动画把该值给锁定了,于是尝试在动画完成之后remove掉该动画,于是把代码改成如下的样子:

DoubleAnimation myDA = new DoubleAnimation(0.8, 0.0, new Duration(TimeSpan.FromMilliseconds(800)));

Storyboard.SetTargetName(myDA, “myObj”);
Storyboard.SetTargetProperty(myDA, new PropertyPath(Canvas.OpacityProperty));

Storyboard mySB = new Storyboard();
mySB.Children.Add(myDA);
mySB.Completed += new EventHandler(mySB_Completed);
mySB.Begin(myObj);

void mySB_Completed(object sender, EventArgs e)
{
mySB.Remove(myObj);
}

可惜还是没有解决这个问题,后来搜索了一下storyboard,在Allan的blog上发现原来在Begin的时候要加上一个参数true才能remove掉storyboard,即改成如下的样子:

mySB.Begin(myObj, true);

查查MSDN,上面Begin有十二个overload,其中有两个第二参数为boolean类型的重载,对其说明是这样的:

true if the storyboard should be interactively controllable; otherwise, false.

如果storyboard是可控制的,则必须设置为true,否则可不要该参数了。

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.