WPF中Storyboard动画中的异常

时间:2012-02-06 16:21:49

标签: wpf

这是我的完整代码

namespace WpfApplication71

{

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>

    public partial class MainWindow : Window
    {
        Frame objframe;
        TransformGroup trGrp;
        TranslateTransform trTns;
        ScaleTransform trScl;

        public MainWindow()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            objframe = new Frame();
            objframe.Background = Brushes.Blue;
            trTns = new TranslateTransform();
            trScl = new ScaleTransform();
            trGrp = new TransformGroup();
            trGrp.Children.Add(trTns);
            trGrp.Children.Add(trScl);
            objframe.RenderTransform = trGrp; 
            objframe.Loaded += new RoutedEventHandler(objframe_Loaded);
            mygrid.Children.Add(objframe);
        }

        void objframe_Loaded(object sender, RoutedEventArgs e)
        {
            var sb1 = new Storyboard();
            var da1 = new DoubleAnimationUsingKeyFrames();
            da1.KeyFrames.Add(new SplineDoubleKeyFrame { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0)), Value = 0 });
            da1.KeyFrames.Add(new SplineDoubleKeyFrame { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(20)), Value = 1, KeySpline = new KeySpline(0, 0, 0.4, 0.969) });
            da1.KeyFrames.Add(new SplineDoubleKeyFrame { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(25)), Value = 1, KeySpline = new KeySpline(0, 0, 0.4, 0.969) });
            Storyboard.SetTargetProperty(objframe, new PropertyPath("(UIElement.Opacity)"));
            Storyboard.SetTarget(da1, objframe);
            sb1.Children.Add(da1);
            var da3 = new DoubleAnimationUsingKeyFrames();
            da3.KeyFrames.Add(new SplineDoubleKeyFrame { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0)), Value = 1 });
            da3.KeyFrames.Add(new SplineDoubleKeyFrame { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(15)), Value = 0.8, KeySpline = new KeySpline(0, 0, 0.4, 0.969) });
            da3.KeyFrames.Add(new SplineDoubleKeyFrame { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(15)), Value = 1 });
            da3.KeyFrames.Add(new SplineDoubleKeyFrame { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(25)), Value = 1 });
            Storyboard.SetTargetProperty(da3, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleX)"));
            Storyboard.SetTarget(da3, objframe);
            sb1.Children.Add(da3);

            var da5 = new DoubleAnimationUsingKeyFrames();
            da5.KeyFrames.Add(new SplineDoubleKeyFrame { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0)), Value = 1 });
            da5.KeyFrames.Add(new SplineDoubleKeyFrame { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(15)), Value = 0.8, KeySpline = new KeySpline(0, 0, 0.4, 0.969) });
            da5.KeyFrames.Add(new SplineDoubleKeyFrame { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(15)), Value = 1 });
            da5.KeyFrames.Add(new SplineDoubleKeyFrame { KeyTime = KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(25)), Value = 1 });

            Storyboard.SetTargetProperty(da5, new PropertyPath("(UIElement.RenderTransform).(TransformGroup.Children)[0].(ScaleTransform.ScaleY)"));
            Storyboard.SetTarget(da5, objframe);

            sb1.Children.Add(da5);
            sb1.Begin();
        }
    }
}

当我点击按钮异常时,会发生这种情况:

Must specify TargetProperty for 'System.Windows.Media.Animation.DoubleAnimationUsingKeyFrames'.

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您需要设置该属性,它是attached property,可以通过StoryBoard.SetTargetProperty进行设置。