C# Windows Phone 8中的编程移动动画

C# Windows Phone 8中的编程移动动画,c#,wpf,xaml,animation,windows-phone-8,C#,Wpf,Xaml,Animation,Windows Phone 8,我正在windows中创建移动动画。我想将对象从一个位置移动到另一个位置。基本上,我尝试做的是翻转选定的矩形,然后随机交换两个矩形对象的位置。移动应显示为动画 我已经尝试并实现了翻转动画。但是现在,如果网格中有4个矩形,那么随机的两个矩形应该交换它们的位置,然后是一个移动动画。我尝试使用下面的代码。但它不起作用 它只在屏幕的中心创建一个矩形,其他什么都没有 1) 创建了一个新项目 2) 将以下代码添加到MainPage.xaml.cs文件中 public partial class MainPa

我正在windows中创建移动动画。我想将对象从一个位置移动到另一个位置。基本上,我尝试做的是翻转选定的矩形,然后随机交换两个矩形对象的位置。移动应显示为动画

我已经尝试并实现了翻转动画。但是现在,如果网格中有4个矩形,那么随机的两个矩形应该交换它们的位置,然后是一个移动动画。我尝试使用下面的代码。但它不起作用

它只在屏幕的中心创建一个矩形,其他什么都没有

1) 创建了一个新项目

2) 将以下代码添加到MainPage.xaml.cs文件中

public partial class MainPage : PhoneApplicationPage
{
    // Constructor
    public MainPage()
    {
        InitializeComponent();
        Loaded += Create_And_Run_Animation;
        // Sample code to localize the ApplicationBar
        //BuildLocalizedApplicationBar();
    }
    private void Create_And_Run_Animation(object sender, EventArgs e)
    {
        // Create a red rectangle that will be the target
        // of the animation.
        Rectangle myRectangle = new Rectangle();
        myRectangle.Width = 200;
        myRectangle.Height = 200;
        Color myColor = Color.FromArgb(255, 255, 0, 0);
        SolidColorBrush myBrush = new SolidColorBrush();
        myBrush.Color = myColor;
        myRectangle.Fill = myBrush;

        // Add the rectangle to the tree.
        ContentPanel.Children.Add(myRectangle);

        // Create a duration of 2 seconds.
        Duration duration = new Duration(TimeSpan.FromSeconds(2));

        // Create two DoubleAnimations and set their properties.
        DoubleAnimation myDoubleAnimation1 = new DoubleAnimation();
        DoubleAnimation myDoubleAnimation2 = new DoubleAnimation();

        myDoubleAnimation1.Duration = duration;
        myDoubleAnimation2.Duration = duration;

        Storyboard sb = new Storyboard();
        sb.Duration = duration;

        sb.Children.Add(myDoubleAnimation1);
        sb.Children.Add(myDoubleAnimation2);

        Storyboard.SetTarget(myDoubleAnimation1, myRectangle);
        Storyboard.SetTarget(myDoubleAnimation2, myRectangle);

        // Set the attached properties of Canvas.Left and Canvas.Top
        // to be the target properties of the two respective DoubleAnimations.
        Storyboard.SetTargetProperty(myDoubleAnimation1, new PropertyPath("(Canvas.Left)"));
        Storyboard.SetTargetProperty(myDoubleAnimation2, new PropertyPath("(Canvas.Top)"));

        myDoubleAnimation1.To = 200;
        myDoubleAnimation2.To = 200;

        // Make the Storyboard a resource.
        ContentPanel.Resources.Add("unique_id", sb);

        // Begin the animation.
        sb.Begin();
    }
}
我没有在xaml文件中添加任何内容。事实就是这样。有什么我需要添加到xaml文件中的动画才能正常运行吗

请帮忙

迟做总比不做好;-)

我自己也有问题,你需要补充一下

myDoubleAnimation1.EnableDependentAnimation = true;
myDoubleAnimation2.EnableDependentAnimation = true;
您的代码强制WinRT执行动画-否则,由于可能的性能问题,它认为您的动画不值得。。。唉


资料来源:

有人能更新一下吗?谢谢