如何快速更新WPF画布?

3
在我的wpf应用程序中,我想要更新画布上的一条线(改变起点和终点坐标)并预览更改。问题在于我添加了这条线,但只能看到初始和最终状态。
有没有办法使线条/画布实时更新自身?我想看到线条如何改变长度/位置。
例如,我收到一系列线段的起始/结束对列表。如果我循环该列表并使用来自对值的坐标更新线段的坐标,则无法看到中间状态。
我尝试将线条和画布的可见性设置为可见以强制它们更新,但不起作用。如果我只是添加新的线条,我看不到它们如何添加,只能看到最终结果。
在下面的代码中,每当我有新点时,都会从循环中调用DrawLine方法。
有什么建议吗?
public void DrawLine(List<Library2d.Point> points)
{
    PathFigure myPathFigure = new PathFigure();
    myPathFigure.StartPoint = new System.Windows.Point(points.ElementAt(0).X, points.ElementAt(0).Y);

    LineSegment myLineSegment = new LineSegment();
    myLineSegment.Point = new System.Windows.Point(points.ElementAt(1).X, points.ElementAt(1).Y);

    PathSegmentCollection myPathSegmentCollection = new PathSegmentCollection();
    myPathSegmentCollection.Add(myLineSegment);

    myPathFigure.Segments = myPathSegmentCollection;

    PathFigureCollection myPathFigureCollection = new PathFigureCollection();
    myPathFigureCollection.Add(myPathFigure);

    PathGeometry myPathGeometry = new PathGeometry();
    myPathGeometry.Figures = myPathFigureCollection;

    if (myPath == null)
    {            
    myPath = new Path();
    myPath.Stroke = Brushes.ForestGreen;
    myPath.StrokeThickness = 1;
    canvas.Children.Add(myPath);
    }

    myPath.Data = myPathGeometry;
    myPath.Visibility = Visibility.Visible;
    myPath.InvalidateMeasure();

    canvas.Visibility = Visibility.Visible;
    canvas.InvalidateMeasure();
}
1个回答

4
渲染线程的调度优先级低于UI线程,因此您可以在UI线程中运行循环以一次应用所有更改,然后最终渲染器得到机会。
您应该考虑将线绑定到数据点并更新它们。以下是有关如何为多边形实现此操作的文章:http://bea.stollnitz.com/blog/?p=35,您可能可以根据自己的需求进行调整。
更新:链接的博客现已存档,但可在github上找到:https://github.com/bstollnitz/old-wpf-blog - 多边形绑定从第32篇文章开始。

1
那个链接可能需要更新。 - LarsTech

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接