在winrt的code Behind中使用故事板

本文关键字:故事 Behind winrt code | 更新日期: 2023-09-27 18:09:40

我需要一些帮助来动画一个对象到圆形路径。为此,我在LoadEvent中创建了一些Method,但我不知道将Storyboard添加到此代码中。Storyboard应该改变我的Object在圆形路径的位置。圆路径由AngelToPointMethod计算。如果我SetTargetPropertyTranslateXTranslateY,我的控制移动到直接路径,但我需要一个一个角度移动。

here my Code

 public class Test : Canvas
{
     private int StartAngle = 215;
    private Rectangle _myControl = new Rectangle();
    private double _angle;
    public double Angle
    {
        get { return _angle;}
        set {_angle = value;}
    }
    public Test()
    {
        Loaded += OnLoaded;  
    }
    private void OnLoaded(object sender, Windows.UI.Xaml.RoutedEventArgs e)
    {
       // UpdatePosition(_myControl, StartAngle);
        AnimatePositionAtTheFrist(StartOuterAngle);
    }
     static void OnPropertyChanged(DependencyObject obj,
                                  DependencyPropertyChangedEventArgs args)
    {
        (obj as CircularSliderControl).OnPropertyChanged(args);
    }
    void OnPropertyChanged(DependencyPropertyChangedEventArgs args)
    {
        UpdatePosition(_myControl, StartAngle);
    }
    private void AnimatePositionAtTheFrist( int startAngle)
    {
        Duration duration = new Duration(TimeSpan.FromSeconds(1));
        Storyboard sb = new Storyboard();
        sb.Duration = duration;
        //var da = new DoubleAnimationUsingKeyFrames();
        var da = new DoubleAnimation();
        da.EnableDependentAnimation = true;
        da.Duration = duration;
        da.From = 0;
        da.To = startAngle;
        SineEase easingFunction = new SineEase();
        easingFunction.EasingMode = EasingMode.EaseOut;
        da.EasingFunction = easingFunction;
        Storyboard.SetTarget(da, this);
        Storyboard.SetTargetProperty(da, "Angle"); // error: Cannot resolve TargetProperty Angle on specified object.
        sb.Children.Add(da);
        sb.Begin();
    }
    private void UpdatePosition(Rectangle _myControl, int StartAngle)
    {
        var startPoint = AngelToPoint(StartAngle);
        var tr = new CompositeTransform();
        tr.TranslateX = startPoint.X;
        tr.TranslateY = startPoint.Y ;
        _myControl.RenderTransform = tr;
    }
}
<标题> 更新

我更新了我的代码,但我面临两个新的问题

1:如何处理Angle属性的propertyChanged ?

我所做的实现,对吧?

我需要运行UpdatePosition只是当改变Angle的值而不是所有的属性。

2: how to SetTargetProperty to Angle property?

在winrt的code Behind中使用故事板

假设你有一个KeyFrame定义为:

<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="PaneGrid">
                                <EasingDoubleKeyFrame x:Name="Frame1" KeyTime="0:0:0.1" Value="1"/>
                                <EasingDoubleKeyFrame x:Name="Frame2" KeyTime="0:0:0.4" Value="1"/>
                            </DoubleAnimationUsingKeyFrames>

您可以使用x:Name属性让您在cs中找到框架。你可以通过x:Name:

找到Storyboard的ref
var stoaryboard = RootGrid.Resources["MyStoryboard"] as Stoaryboard;
stoaryboard.Begin();