使用 SetTarget 方法定位多个对象的故事板不起作用 [英] Storyboard targeting multiple objects, using SetTarget method, doesn't work
问题描述
谁能帮我找出为什么这不起作用.
Can anyone help me trying to find out why this doesn't work.
Brushes 变量包含一个预先填充的画笔列表.如果我尝试在迭代过程中直接应用 BeginAnimation
,它工作正常.但是单独启动每个动画的开销很大...
The brushes variable contains a pre-filled list of brushes.
If I try to apply the BeginAnimation
directly during the iteration, it works fine. But has a great overhead starting each animation separately...
所以我试图将所有动画放在一个情节提要中,然后同时触发它们...
So I was trying to put all the animations in a single storyboard, and fire them all at once...
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();
这段代码什么都不做(我可以看到......).
This code simply does nothing (that I can see...).
仍然不确定 SetTarget 方法有什么问题,要么是错误,要么我只是没有按应有的方式使用.无论如何,我解决了在运行时为我的画笔生成唯一名称并使用 SetTargetName 方法的问题.
Still not sure of what is problem with the SetTarget method, either a bug or I'm just not using as it should be. Anyway I solved the problem generating unique names for my brushes at runtime and using the SetTargetName method.
推荐答案
尝试使用 Storyboard.SettargetName 方法而不是 Storyboard.SetTarget
.我为您准备了工作样本:
Try to use Storyboard.SettargetName method instead of Storyboard.SetTarget
. I prepared working sample for you:
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
方法的参数.
Remember, that in this case you also should set a Namescope as parameter for Storyboard.Begin
method.
这篇关于使用 SetTarget 方法定位多个对象的故事板不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!