Why don't these animations work when I'm using a storyboard?

前端 未结 2 1761
清酒与你
清酒与你 2020-12-01 13:32

I\'ve created a simple subclass of StackPanel that I can move around on the screen using an animated TranslateTransform. It looks like this:

相关标签:
2条回答
  • 2020-12-01 13:56

    It's property path syntax. The following approach works:

    public void BeginMove(Point translatePosition)
    {
      RenderTransform = new TranslateTransform();
      Duration d = new Duration(new TimeSpan(0, 0, 0, 0, 400));
      DoubleAnimation x = new DoubleAnimation(translatePosition.X, d);
      DoubleAnimation y = new DoubleAnimation(translatePosition.Y, d);
    
      Storyboard.SetTarget(x, this);
      Storyboard.SetTargetProperty(x, 
                  new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.X)"));
    
      Storyboard.SetTarget(y, this);
      Storyboard.SetTargetProperty(y, 
                  new PropertyPath("(UIElement.RenderTransform).(TranslateTransform.Y)"));
    
      Storyboard sb = new Storyboard();
      sb.Children.Add(x);
      sb.Children.Add(y);
      sb.Completed += sb_Completed;
      sb.Begin();
    
      //RenderTransform.BeginAnimation(TranslateTransform.XProperty, x);
      //RenderTransform.BeginAnimation(TranslateTransform.YProperty, y);
    }
    
    0 讨论(0)
  • 2020-12-01 14:03

    As it turns out, you can't use property path syntax in this case, because the properties being animated aren't properties of a FrameworkElement. At least, that's how I interpret the remarkably bewildering exception that I get when I make the change that Anvaka suggested:

    Cannot automatically create animation clone for frozen property values on     
    'System.Windows.Media.TranslateTransform' objects. Only FrameworkElement and 
    FrameworkContentElement (or derived) types are supported.
    

    To animate those, it seems, I have to use a NameScope and use SetTargetName to name the TransformElement. Then, as long as I pass the FrameworkElement that I set the name scope on to the Begin method, the storyboard can find the object and the properties and animate them and it all works. The end result looks like this:

    public void BeginMove(Point translatePosition)
    {
        NameScope.SetNameScope(this, new NameScope());
    
        RenderTransform = new TranslateTransform();
        RegisterName("TranslateTransform", RenderTransform);
    
        Duration d = new Duration(new TimeSpan(0, 0, 0, 0, 400));
        DoubleAnimation x = new DoubleAnimation(translatePosition.X, d);
        DoubleAnimation y = new DoubleAnimation(translatePosition.Y, d);
    
        Storyboard.SetTargetName(x, "TranslateTransform");
        Storyboard.SetTargetProperty(x, new PropertyPath(TranslateTransform.XProperty));
    
        Storyboard.SetTargetName(y, "TranslateTransform");
        Storyboard.SetTargetProperty(y, new PropertyPath(TranslateTransform.YProperty));
    
        Storyboard sb = new Storyboard();
        sb.Children.Add(x);
        sb.Children.Add(y);
        sb.Completed += sb_Completed;
    
        // you must pass this to the Begin method, otherwise the timeline won't be
        // able to find the named objects it's animating because it doesn't know
        // what name scope to look in
    
        sb.Begin(this);
    
    }
    
    0 讨论(0)
提交回复
热议问题