【问题标题】:Issue applying an animation using a storyboard in code在代码中使用故事板应用动画的问题
【发布时间】:2013-06-17 13:36:29
【问题描述】:

我有一个Canvas 和一个名为Ellipse 的对象Marker

<!-- Boilerplate -->
<Window x:Class="WpfApplication6.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:WpfApplication6="clr-namespace:WpfApplication6"
        Title="MainWindow" Height="350" Width="525"
        SnapsToDevicePixels="True">
  <Grid>
    <Canvas x:Name="DrawingCanvas">
      <!-- ##### Item of interest, below ##### -->
      <Ellipse x:Name="Marker" Canvas.Left="200" Canvas.Top="100" 
               Width="25" Height="25" Stroke="Black" Fill="#E0E000">
        <Ellipse.Effect>
          <BlurEffect x:Name="MarkerBlurEffect" Radius="0.0" />
        </Ellipse.Effect>
      </Ellipse>
    </Canvas>
  </Grid>
</Window>

我正在尝试以编程方式为 Marker 椭圆提供模糊动画:

  DoubleAnimation blurEffectAnimation=new DoubleAnimation
  {
    From=0, 
    To=10, 
    Duration=TimeSpan.FromSeconds(2.0)
  };

  // If I uncomment the line below, the blur effect animation works perfectly
  //Marker.Effect.BeginAnimation(BlurEffect.RadiusProperty, blurEffectAnimation); 

  // If I do it using the storyboard code below, the animation has no effect
  Storyboard.SetTarget(blurEffectAnimation, Marker.Effect);
  Storyboard.SetTargetProperty(blurEffectAnimation, 
                               new PropertyPath(BlurEffect.RadiusProperty));

  Storyboard sb=new Storyboard();

  sb.Children.Add(blurEffectAnimation);
  sb.Begin();

有趣的是,如果我按名称注册效果并将该名称用于情节提要,效果会再次开始工作:

  // Register the MarketBlurEffect (as it's named in the XAML) effect by name 
  // with the FrameworkElement being animated (why do I need to?)
  Marker.RegisterName("MarkerBlurEffect",MarkerBlurEffect);

  // Instead of SetTarget, use SetTargetName on the registered name
  Storyboard.SetTargetName(blurEffectAnimation, "MarkerBlurEffect");
  Storyboard.SetTargetProperty(blurEffectAnimation, 
    new PropertyPath(BlurEffect.RadiusProperty));

  Storyboard sb=new Storyboard();

  sb.Children.Add(blurEffectAnimation);
  // Provide Marker to the Begin function in order to specify the name scope 
  // for the registered ("MarkerBlurEffect") name. Not doing this will result
  // in an InvalidOperationException with the Message property set to:
  //   No applicable name scope exists to resolve the name 'MarkerBlurEffect'.
  sb.Begin(Marker);

【问题讨论】:

    标签: c# wpf xaml c#-4.0 .net-4.0


    【解决方案1】:

    是的……你是对的。由于 Effect 对象在树的深处,您必须这样做才能访问它。或者您可以从椭圆访问您的效果并将您的属性路径更改为如下所示:

    DoubleAnimation blurEffectAnimation = new DoubleAnimation
    {
      From = 0,
      To = 10,
      Duration = TimeSpan.FromSeconds(2.0)
    };
    
    Storyboard.SetTarget(
      blurEffectAnimation, 
      Marker);
    
    Storyboard.SetTargetProperty(
      blurEffectAnimation,
      new PropertyPath("(Effect).Radius"));
    
    Storyboard sb = new Storyboard();
    
    sb.Children.Add(blurEffectAnimation);
    sb.Begin();
    

    【讨论】:

    • 这很有趣,您将目标设置为椭圆形(标记)而不是椭圆形的 Effect 属性。然后,您通过更复杂的路径引用了该属性,该路径同时提到了形状的 Effect 属性及其 Radius 属性。你的方法确实有效,所以现在+1。这使得 Storyboard 的 SetTarget 函数看起来需要一个 FrameworkElement 而不仅仅是一个依赖属性,尽管 SetTarget 函数的原型导致人们不相信。
    • 我刚刚在路径中使用 EllipseGeometry 的 RadiusX 属性尝试了同样的事情。不适用于目标 Path.Data 和属性 RadiusX,但适用于目标路径和属性 (Data).RadiusX。我同意 SetTarget 期望 FrameworkElement 或至少 UIElement 的解释。我认为这与逻辑或视觉树的深度无关。
    • 更多信息是herehere
    • 其实这个stackoverflow.com/questions/2338714/… 直接回答了为什么按名称方法有效而按属性方法无效。
    猜你喜欢
    • 2013-06-05
    • 2011-01-07
    • 2011-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-21
    • 2011-10-16
    • 2011-10-03
    相关资源
    最近更新 更多