繁体   English   中英

在代码中使用情节提要应用动画的问题

[英]Issue applying an animation using a storyboard in code

我有一个带有名为MarkerEllipse对象的Canvas

<!-- 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);

是的...你是对的。 由于效果对象某种程度上位于树的深处,因此您必须这样做才能访问它。 或者,您可以从椭圆访问效果并将属性路径更改为如下所示:

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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM