来自单独线程的C#WPF动画

时间:2019-01-26 05:32:49

标签: c# wpf multithreading animation

我在动画方面遇到麻烦。我必须从异步线程控制我的双重动画。单独的线程将异步更改uklepe值,动画将作为更改的结果播放。这就是我尝试过的方法,但是即使使用Dispatcher,我仍然会遇到此错误:

  

由于其他线程拥有该对象,因此调用线程无法访问该对象

    protected bool üstklepe = false;
    public bool uklepe
    {
        get
        {
            return üstklepe;
        }
        set
        {
            if (üstklepe == false && value == true)
            {
                var da = new DoubleAnimation(0, 90, new Duration(TimeSpan.FromMilliseconds(100)));
                var rt = new RotateTransform(0, 0, 0);
               Dispatcher.Invoke(() => {Ustklepe.RenderTransform = rt;});
                Dispatcher.Invoke(() => {Ustklepe.RenderTransformOrigin = new Point(0, 0);});
                 Dispatcher.Invoke(() => {rt.BeginAnimation(RotateTransform.AngleProperty, da);});

                RadialGradientBrush myBrush = new RadialGradientBrush();
                myBrush.GradientOrigin = new Point(0.35, 0.65);
                myBrush.GradientStops.Add(new GradientStop(Colors.White, 0.0));
                myBrush.GradientStops.Add(new GradientStop((Color)ColorConverter.ConvertFromString("#FF91D393"), 0.916));

                //SolidColorBrush Brush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FF91D393"));
                Dispatcher.Invoke(() => { Ustklepe.Fill = myBrush; });



                üstklepe = value;

            }
            else if (üstklepe == true && value == false)
            {
                var da = new DoubleAnimation(90, 0, new Duration(TimeSpan.FromMilliseconds(100)));
                var rt = new RotateTransform(0, 0, 0);
                 Dispatcher.Invoke(() => {Ustklepe.RenderTransform = rt;});
                 Dispatcher.Invoke(() => {Ustklepe.RenderTransformOrigin = new Point(0, 0);});
                 Dispatcher.Invoke(() => {rt.BeginAnimation(RotateTransform.AngleProperty, da);});


                RadialGradientBrush myBrush = new RadialGradientBrush();
                myBrush.GradientOrigin = new Point(0.35, 0.65);
                myBrush.GradientStops.Add(new GradientStop(Colors.White, 0.0));
                myBrush.GradientStops.Add(new GradientStop((Color)ColorConverter.ConvertFromString("#FFFF2F33"), 0.916));

                //SolidColorBrush Brush = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#FFFF2F33"));
                Dispatcher.Invoke(() => { Ustklepe.Fill = myBrush; });



                üstklepe = value;

            }
            else
                üstklepe = value;

        }
    }

1 个答案:

答案 0 :(得分:0)

我认为问题是,即使您使用调度程序设置了旋转变换,您还是在使用这些行设置uklepe属性的线程中创建了实例。

        var da = new DoubleAnimation(0, 90, new Duration(TimeSpan.FromMilliseconds(100)));
        var rt = new RotateTransform(0, 0, 0);

编辑:这些行也

        RadialGradientBrush myBrush = new RadialGradientBrush();
        myBrush.GradientOrigin = new Point(0.35, 0.65);
        myBrush.GradientStops.Add(new GradientStop(Colors.White, 0.0));
        myBrush.GradientStops.Add(new GradientStop((Color)ColorConverter.ConvertFromString("#FFFF2F33"), 0.916));

您还需要在UI线程中运行这些行