有没有办法将数据添加到 DrawingVisual

Is there a way to add data to DrawingVisual

我有一个带有多个 DrawingVisual 对象的自定义绘制控件(覆盖 FrameworkElement)。我保留了一个视觉列表并覆盖 GetVisualChildVisualChildrenCount。性能很重要,所以大部分都用BitmapCache.

其中一个视觉对象将每 50 毫秒更新一次新数据。它绘制了一条机器在现实世界中所走的路径,所以每 50 毫秒。有一条新线要画,保持旧线不变。

以良好的性能绘制它的最佳方法是什么,而不是重新绘制现有的机器路径,而只是添加另一条线?似乎一旦您使用 RenderOpen 在视觉对象中绘制某些内容,您就无法修改它。我试过 visual.Drawing.Append() 但它似乎什么也没画。

有没有办法向 DrawingVisual 添加新数据?如果没有,用什么代替?

也许创建一个您绘制的 RenderTargetBitmap,然后在调用 OnRender 时执行 Context.DrawImage

using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace WpfApplication13
{
    public class PanelTest : FrameworkElement
    {
        public RenderTargetBitmap _renderTargetBitmap = null;
        public System.Windows.Threading.DispatcherTimer _Timer = null;
        public int _iYLoc = 0;
        private Pen _pen = null;

        protected override void OnRender(DrawingContext drawingContext)
        {
            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                drawingContext.DrawImage(_renderTargetBitmap, new Rect(0, 0, 250, 250));
            }


            base.OnRender(drawingContext);
        }

        public PanelTest() :base()
        {
            _renderTargetBitmap = new RenderTargetBitmap(250, 250, 96, 96, System.Windows.Media.PixelFormats.Pbgra32);
            _pen = new Pen(Brushes.Red, 1);
            _Timer = new System.Windows.Threading.DispatcherTimer();
            _Timer.Interval = new TimeSpan(0, 0, 0, 0, 50);
            _Timer.Tick += _Timer_Tick;
            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                _Timer.Start();
            }
        }

        private void _Timer_Tick(object sender, EventArgs e)
        {

            DrawingVisual vis = new DrawingVisual();
            DrawingContext con = vis.RenderOpen();
            con.DrawLine(_pen, new Point(0, _iYLoc), new Point(250, _iYLoc));
            _iYLoc++;

            con.Close();

            _renderTargetBitmap.Render(vis);
        }
    }

}