谁能帮我找出为什么这不起作用。
brushes 变量包含一个预填充的画笔列表。
如果我尝试在迭代期间直接应用 BeginAnimation
,它工作正常。但是单独启动每个动画的开销很大......
所以我试图将所有动画放在一个 Storyboard中,然后同时触发它们......
var storyBoard = new Storyboard();
var duration = new Duration(TimeSpan.FromMilliseconds(time));
foreach (Brush brush in brushes)
{
var animation = new DoubleAnimation(toValue, duration);
storyBoard.Children.Add(animation);
Storyboard.SetTargetProperty(animation, new PropertyPath(Brush.OpacityProperty));
Storyboard.SetTarget(animation, brush);
}
storyBoard.Begin();
这段代码什么都不做(我可以看到...)。
编辑: 仍然不确定 SetTarget 方法有什么问题,要么是错误,要么我只是没有按应有的方式使用。无论如何,我解决了在运行时为画笔生成唯一名称并使用 SetTargetName 方法的问题。
最佳答案
尝试使用Storyboard.SettargetName方法而不是 Storyboard.SetTarget
。我为您准备了工作示例:
var brushes = new string[] { "br1", "br2", "br3" };
var sb = new Storyboard();
var dur = new Duration(TimeSpan.FromMilliseconds(500.0));
double toValue = 1.0;
foreach (var brush in brushes)
{
var anim = new DoubleAnimation(toValue, dur);
Storyboard.SetTargetName(anim, brush);
Storyboard.SetTargetProperty(anim, new PropertyPath("(0)", new DependencyProperty[] { Brush.OpacityProperty }));
sb.Children.Add(anim);
}
sb.Begin(this);
请记住,在这种情况下,您还应该设置一个 Namescope作为 Storyboard.Begin
方法的参数。
关于c# - 使用 SetTarget 方法针对多个对象的 Storyboard不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5031866/