我如何在画布中添加超过1条折线

本文关键字:1条 折线 添加 布中 | 更新日期: 2023-09-27 18:18:01

我正在使用c# WPF在触摸屏上绘制10条折线。然而,它得到了一个错误

"Argument exception was unhandled类型的未处理异常的系统。在PresentationCore.dll中出现ArgumentException

附加信息:指定的可视化对象已经是另一个可视化对象的子对象Visual或CompositionTarget的根。"

表示

canvas.Children.Add(freeline); in private void 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);
    }

我如何在画布中添加超过1条折线

不要重复使用相同的Polyline实例
删除行:

Polyline freeline = new Polyline();

当你需要绘制另一个Polyline时,创建一个新的并绘制它

这个错误是不言自明的,您将相同的freeline多次添加到VisualTree中。您需要使用某种工厂方法,以便总是在添加到canvas.Children集合之前获得Polyline的新实例