【问题标题】:How can i add more than 1 polyline in canvas?如何在画布中添加超过 1 条折线?
【发布时间】:2016-01-14 21:02:32
【问题描述】:

我正在使用 C# WPF 在触摸屏上绘制 10 条折线。然而,它出错了

"Argument expception was unhandled an unhandled exception of type PresentationCore.dll 中发生“System.ArgumentException”

附加信息:指定的视觉对象已经是另一个视觉对象的子对象 视觉对象或 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);
    }

【问题讨论】:

  • 如果以下答案之一解决了您的问题,请标记为答案。

标签: c# wpf canvas polyline


【解决方案1】:

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

Polyline freeline = new Polyline();

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

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2013-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-06
      • 1970-01-01
      • 1970-01-01
      • 2020-04-20
      • 1970-01-01
      相关资源
      最近更新 更多