在Windows 8 Metro中通过C#进行动画制作

时间:2013-12-10 07:08:11

标签: c# animation windows-8 windows-store-apps winrt-xaml

我正在尝试制作一个动画,其中我的特定网格的所有元素都应该振动。

所以我尝试了这段代码..

Storyboard hintstoryboard = new Storyboard();

for (int i = 3; i < gridpieces.Children.Count; i++)
{
    DoubleAnimationUsingKeyFrames doubleanimation = new DoubleAnimationUsingKeyFrames();
    Storyboard.SetTargetName(doubleanimation, ((Image)gridpieces.Children[i]).Name);
    Storyboard.SetTargetProperty(doubleanimation, "(UIElement.Projection).(PlaneProjection.RotationZ)");

    EasingDoubleKeyFrame frame = new EasingDoubleKeyFrame();
    frame.KeyTime = TimeSpan.FromSeconds(0.1);
    frame.Value = -5;

    EasingDoubleKeyFrame frame1 = new EasingDoubleKeyFrame();
    frame1.KeyTime = TimeSpan.FromSeconds(0.2);
    frame1.Value = 0;

    EasingDoubleKeyFrame frame2 = new EasingDoubleKeyFrame();
    frame2.KeyTime = TimeSpan.FromSeconds(0.3);
    frame2.Value = 5;

    EasingDoubleKeyFrame frame3 = new EasingDoubleKeyFrame();
    frame3.KeyTime = TimeSpan.FromSeconds(0.4);
    frame3.Value = 0;

    doubleanimation.KeyFrames.Add(frame);
    doubleanimation.KeyFrames.Add(frame1);
    doubleanimation.KeyFrames.Add(frame2);
    doubleanimation.KeyFrames.Add(frame3);

    hintstoryboard.Children.Add(doubleanimation);
}

hintstoryboard.Begin();

但它给出了一个异常,即找不到目标名称,但是我的网格具有相同名称的图像...我只是按代码制作了网格...

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

我认为你不能在XAML之外使用SetTargetName。在代码背后你应该替换它:

Storyboard.SetTargetName(doubleanimation, ((Image)gridpieces.Children[i]).Name);

用这个

Storyboard.SetTarget(doubleanimation, (Image)gridpieces.Children[i]);