【问题标题】:Storyboard targeting multiple objects, using SetTarget method, doesn't work使用 SetTarget 方法针对多个对象的情节提要不起作用
【发布时间】:2019-06-29 11:26:19
【问题描述】:

谁能帮我找出为什么这不起作用。

画笔变量包含一个预填充的画笔列表。 如果我尝试在迭代期间直接应用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();

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

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

【问题讨论】:

  • 您不应该先找到包含画笔的属性,然后是画笔不透明度吗?像new PropertyPath("(Shape.Fill).(SolidColorBrush.Opacity)") 这样的东西,然后你会在下一行看到更接近StoryBoard.SetTarget(animation, this) 的东西?我充其量只是一个初学者,但这似乎是问题的一部分。您想更改对象上属性的画笔,而不是画笔本身作为目标(我认为您无论如何都不能这样做)

标签: c# wpf animation 3d


【解决方案1】:

尝试使用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 方法的参数。

另请参阅:Another answers on the Stackoverflow

【讨论】:

  • 您提供的代码实际上有效。但是,在您的情况下,您有一个带有画笔名称的字符串数组。在我的解决方案中,3D 模型是从外部资源库加载的,因此我不能假设每个画笔都是一个名称。无论如何,我现在正在使用您的解决方案,并在运行时为画笔生成唯一名称。同时,我可能会尝试找到一种为所有 Visual3D 组件生成名称的好方法。
猜你喜欢
  • 1970-01-01
  • 2014-04-27
  • 2011-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-27
相关资源
最近更新 更多