2011-03-24 13 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,你可能會根據自己的需要進行調整。