2015-10-16 68 views
0

我正在使用C#WPF在觸摸屏上繪製10條多段線。然而,它得到了一個錯誤我如何在畫布中添加多於1條折線?

「之爭expception是未處理的類型 ‘System.ArgumentException’未處理的異常發生在PresentationCore.dll中

其他信息:指定的Visual已經是另一個 視覺孩子或CompositionTarget的根。「

在私人無效線路

canvas.Children.Add(freeline);canvas_TouchDown 時,我提請第二行。我能成功地畫了一條線,但 當我開始畫第二行中,它得到了提到的錯誤

public partial class MainWindow : Window 
{ 
    //List<Point> list = new List<Point>(); 
    Polyline freeline = new Polyline(); 

    bool isMouseDown = false; //mouse 

    Color[] colours = new Color[10] {Colors.White, Colors.Yellow, 
     Colors.Green,Colors.LightBlue, Colors.LightGreen, 
     Colors.LightCyan, Colors.LightGray, Colors.LightPink, 
     Colors.Purple, Colors.Red}; 
    // Store the active lines, each of which corresponds to a place 
    // the user is currently touching down. 
    Dictionary<int, Line> movingLines = new Dictionary<int, Line>(); 
    Line line = new Line(); 
    int counter = 0; 



    public MainWindow() 
    { 


     InitializeComponent(); 


    } 
    private void canvas_TouchDown(object sender, TouchEventArgs e) 
    { 
     counter = (counter + 1) % 10; 
     Line line = new Line(); 
     // line.StrokeThickness = e.GetTouchPoint().Size; 
     // line.Stroke = new SolidColorBrush(colours[counter]); 

     // Position the line at the touch-down point. 
     TouchPoint touchPoint = e.GetTouchPoint(canvas); 
     line.X1 = touchPoint.Position.X; 
     line.Y1 = touchPoint.Position.Y; 
     line.X2 = line.X1; 
     line.Y2 = line.Y1; 
     movingLines[e.TouchDevice.Id] = line; 
     // Add the line to the Canvas. 
     //canvas.Children.Add(line); 
     // list_store_point_touch(touchPoint.Position.X, touchPoint.Position.Y); 

      canvas.Children.Add(freeline); 
      freeline.StrokeThickness = 4; 
      freeline.Stroke = new SolidColorBrush(colours[counter]); 


     //e.GetTouchPoint() 

     //for (int counter = 0; counter < touchPoint.Position.X ; counter++) 
     //{ 
     // canvas.Children.Add(freeline); 
     // freeline.StrokeThickness = 4; 
     // freeline.Stroke = new SolidColorBrush(colours[counter]); 
     //} 


     /* for (int i = 0; i < handwritings.Points.Count - 1; i++) 
     { 
      drawingContext.DrawLine(new Pen(Brushes.Yellow, 3), 
       handwritings.Points[i], handwritings.Points[i + 1]); 
     } */ 

    } 





    private void canvas_TouchMove(object sender, TouchEventArgs e) 
    { 

     // Get the line that corresponds to the current touch-down. 
     line = movingLines[e.TouchDevice.Id]; 

     // Move it to the new touch-down point. 
     TouchPoint touchPoint = e.GetTouchPoint(canvas); 
     line.X2 = touchPoint.Position.X; 
     line.Y2 = touchPoint.Position.Y; 
     list_store_point_touch(touchPoint.Position.X, touchPoint.Position.Y); 

    } 


    private void canvas_TouchUp(object sender, TouchEventArgs e) 
    { 
     movingLines.Remove(e.TouchDevice.Id); 

    } 

    private void list_store_point_touch(double X, double Y) 
    { 
     Point point = new Point(X,Y);    
     freeline.Points.Add(point); 

    } 


    private void canvas_MouseDown(object sender, MouseButtonEventArgs e) 
    { 
     isMouseDown = true; 

     counter = (counter + 1) % 10; 
     line = new Line(); 
     line.StrokeThickness = 4; 


     line.Stroke = new SolidColorBrush(colours[counter]); 

     // Position the line at the mouse down point. 
     Point mousePoint = e.GetPosition(canvas); 
     line.X1 = mousePoint.X; 
     line.Y1 = mousePoint.Y; 
     line.X2 = line.X1; 
     line.Y2 = line.Y1; 
     //Add the line to the Canvas. 


      canvas.Children.Add(freeline); 
      freeline.StrokeThickness = 4; 
      freeline.Stroke = new SolidColorBrush(colours[counter]); 

     //canvas.Children.Add(line); 
    } 
+0

如果其中一個答案解決了您的問題,請標記爲答案。 – cleftheris

回答

0

不要重複使用相同的Polyline實例。
刪除行:

Polyline freeline = new Polyline();

當你需要畫又一Polyline,創造新的和繪製。

0

該錯誤是不言自明的,您將多次添加相同的freeline到VisualTree。您需要使用某種工廠方法才能在添加到canvas.Children集合之前始終獲得Polyline的新實例

相關問題