故事板打靶多个对象,使用SetTarget方法,不能正常工作 [英] Storyboard targetting multiple objects, using SetTarget method, doesn't work

查看:311
本文介绍了故事板打靶多个对象,使用SetTarget方法,不能正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能帮我试图找出为什么这是行不通的。

Can anyone help me trying to find out why this doesn't work.

刷子变量包含刷的pre填充列表。 如果我尝试了迭代过程中应用 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();

这code根本不做任何事(我可以看到... ...)。

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.

再次感谢所有的建议。

推荐答案

尝试使用<一个href="http://msdn.microsoft.com/enus/library/system.windows.media.animation.storyboard.settargetname.aspx"相对=nofollow> Storyboard.SettargetName 方法,而不是 Storyboard.SetTarget 。我prepared工作的样品给你:

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 方法。

另请参见:<一href="http://stackoverflow.com/questions/2338714/why-dont-these-animations-work-when-im-using-a-storyboard">Another答案在#1 。

这篇关于故事板打靶多个对象,使用SetTarget方法,不能正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆