2011-03-24 65 views
1

在我的wpf应用程序中,我想更新画布上的一行(更改开始,终点坐标)并预览更改。问题是,我添加了该行,但我只能看到初始状态和最终状态。如何更新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(); 
} 

回答

2

渲染线程具有较低的优先级调度比UI线程,所以你通过申请一次所有更改UI线程运行循环,最后渲染它得到了一枪。

您应该考虑将行绑定到数据点并更新它们。这里有一篇关于如何实现多边形的文章:http://bea.stollnitz.com/blog/?p=35,你可能会根据自己的需要进行调整。