使用SetTarget方法针对多个对象的情节提要板不起作用 [英] Storyboard targeting multiple objects, using SetTarget method, doesn't work
问题描述
任何人都可以帮助我找出为什么这种方法不起作用。
Can anyone help me trying to find out why this doesn't work.
brush变量包含一个预填充的画笔列表。
如果我尝试在迭代过程中直接应用 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);
请记住,在这种情况下,您还应该设置名称范围作为 Storyboard.Begin
的参数方法。
Remember, that in this case you also should set a Namescope as parameter for Storyboard.Begin
method.
另请参阅:有关Stackoverflow的另一个答案。
这篇关于使用SetTarget方法针对多个对象的情节提要板不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!