使用SetTarget方法定位多个对象的故事板不起作用

Gui*_*rte 6 c# 3d wpf animation

任何人都可以帮我试图找出为什么这不起作用.

Brush变量包含一个预先填充的画笔列表.如果我尝试BeginAnimation在迭代期间直接应用它,它可以正常工作.但是每个动画分别开始有很大的开销......

所以我试图将所有动画放在一个故事板中,然后立即将它们全部解开......

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();
Run Code Online (Sandbox Code Playgroud)

这段代码什么都不做(我可以看到......).

谢谢!!

编辑:仍然不确定SetTarget方法有什么问题,要么是一个bug,要么我只是不应该使用它.无论如何,我解决了在运行时为我的画笔生成唯一名称并使用SetTargetName方法的问题.

再次感谢所有的建议.

Y.Y*_*hus 2

尝试使用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);
Run Code Online (Sandbox Code Playgroud)

请记住,在这种情况下,您还应该将名称范围设置为Storyboard.Begin方法的参数。

另请参阅:Stackoverflow 上的另一个答案