简体   繁体   中英

use storyboard in code Behind in winrt

I need some help to animate an object to Circular path. for this purpose i create some Method in LoadEvent But I have no idea to add Storyboard to this Code. Storyboard should change the location of my Object in Circular path. Circular path Calculated by AngelToPointMethod . If I SetTargetProperty to TranslateX and TranslateY my control move to Direct path but i need to move angel by angel.

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;
    }

}

Update

I updated my code As above but i faced to two new problem

1: how to handle propertyChanged of Angle property?

Does the implementation that I've done, right?

I need run UpdatePosition just when the change value of Angle not all Properties.

2: how to SetTargetProperty to Angle property?

Assume that you have a KeyFrame defined as :

<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>

You can use x:Name attribute to let you find the Frame in your cs . And you can find the ref of your Storyboard by its x:Name:

var stoaryboard = RootGrid.Resources["MyStoryboard"] as Stoaryboard;
stoaryboard.Begin();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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