0

我正在使用 OxyPlot 为简单的正弦运动设置动画:

       void ViewMotion()
    {
        foreach (File item in LoadedFiles())
        {
            LineSeries skeleton = new LineSeries();
            skeleton.MarkerType = MarkerType.Circle;
            skeleton.MarkerSize = 10;

            AnimationPlot.Series.Add(skeleton );

            var worker = new BackgroundWorker { WorkerSupportsCancellation = true };

            foreach (Channel node in item.ChannelAnim)
            {
                double x = 0;
                worker.DoWork += (s, e) =>
                {
                    while (!worker.CancellationPending)
                    {
                        x += 0.02;
                        if (skeleton.Points.Count > 0)
                        {
                            skeleton.Points.Remove(ss.Points.First());
                        }

                        skeleton.Points.Add(new DataPoint(50, 50 + (30 + x) * Math.Sin(x)));
                        AnimationPlot.RefreshPlot(true);
                        MessageBox.Show("");
                        Thread.Sleep(25);

                    }
                };

            }
            worker.RunWorkerAsync();


        }
    }

基本上只是一个上下移动的点。事件只触发一次的问题,所以我看不到动作而是初始位置。这不是绑定问题,而是事件有问题。我试过改变顺序但没有成功。

顺便说一句,这工作正常:(上下两个点)

    ScatterSeries ss = new ScatterSeries();
    ScatterSeries ss2 = new ScatterSeries();
    public void TemporalMethod(object param)
    {
        ss.MarkerType = MarkerType.Circle;
        ss.MarkerSize = 20;
        ss.MarkerFill = OxyColors.Violet;
        MyModel.Series.Add(ss);

        ss2.MarkerType = MarkerType.Triangle;
        ss2.MarkerSize = 20;
        ss2.MarkerFill = OxyColors.Green;
        MyModel.Series.Add(ss2);

        var worker = new BackgroundWorker { WorkerSupportsCancellation = true };
        double x = 0;
        double y = 0;

        worker.DoWork += (s, e) =>
        {
            while (!worker.CancellationPending)
            {
                x += 0.02;
                y += 0.001;
                if (ss.Points.Count > 0)
                {
                    ss.Points.Remove(ss.Points.First());
                }
                if (ss2.Points.Count > 0)
                {
                    ss2.Points.Remove(ss2.Points.First());
                }

                ss.Points.Add(new DataPoint(50, 50 + (30 + x) * Math.Sin(x)));
                ss2.Points.Add(new DataPoint(55, 50 + 30 * Math.Sin(x + y)));
                MyModel.RefreshPlot(true);

                Thread.Sleep(25);
            }
        };

        worker.RunWorkerAsync();
    }

编辑:解决方案很愚蠢,我错误地写了

                            if (skeleton.Points.Count > 0)
                        {
                            skeleton.Points.Remove(ss.Points.First());
                        }

代替:

                            if (skeleton.Points.Count > 0)
                        {
                            skeleton.Points.Remove(skeleton.Points.First());
                        }

我不知道为什么,但我没有任何错误。

4

0 回答 0