Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在画布中添加多条多段线?_C#_Wpf_Canvas_Polyline - Fatal编程技术网

C# 如何在画布中添加多条多段线?

C# 如何在画布中添加多条多段线?,c#,wpf,canvas,polyline,C#,Wpf,Canvas,Polyline,我正在使用C#WPF在触摸屏上绘制10条多段线。然而,它却犯了一个错误 “未处理参数ExpException类型的未处理异常 PresentationCore.dll中出现“System.ArgumentException” 其他信息:指定的Visual Studio已是另一个Visual Studio的子级 可视或合成目标的根。“ 排队 canvas.Children.Add(自由线)在私人空间canvas\u着陆 当我画第二条线的时候。我可以成功地画出一条线,但是 当我开始画第二条线时,它得

我正在使用C#WPF在触摸屏上绘制10条多段线。然而,它却犯了一个错误

“未处理参数ExpException类型的未处理异常 PresentationCore.dll中出现“System.ArgumentException”

其他信息:指定的Visual Studio已是另一个Visual Studio的子级 可视或合成目标的根。“

排队

canvas.Children.Add(自由线)在私人空间
canvas\u着陆
当我画第二条线的时候。我可以成功地画出一条线,但是 当我开始画第二条线时,它得到了上面提到的错误

公共部分类主窗口:窗口
{
//列表=新列表();
多段线自由线=新多段线();
bool isMouseDown=false;//鼠标
颜色[]颜色=新颜色[10]{Colors.White,Colors.yellows,
颜色。绿色,颜色。浅蓝色,颜色。浅绿色,
颜色。浅青色,颜色。浅灰色,颜色。浅粉色,
颜色。紫色,颜色。红色};
//存储活动行,每个行对应一个位置
//用户当前正在触地。
字典移动行=新字典();
行=新行();
int计数器=0;
公共主窗口()
{
初始化组件();
}
私有void canvas_接地(对象发送方,TouchEventArgs e)
{
计数器=(计数器+1)%10;
行=新行();
//line.StrokeThickness=e.GetTouchPoint().Size;
//线条笔划=新的SolidColorBrush(颜色[计数器]);
//将管路放置在触地点处。
接触点接触点=e.GetTouchPoint(画布);
line.X1=接触点.Position.X;
line.Y1=接触点.Position.Y;
line.X2=line.X1;
line.Y2=line.Y1;
movingLines[e.TouchDevice.Id]=线路;
//将线条添加到画布。
//canvas.Children.Add(行);
//列表\存储\点\触摸(touchPoint.Position.X,touchPoint.Position.Y);
canvas.Children.Add(自由线);
freeline.StrokeThickness=4;
freeline.Stroke=新的SolidColorBrush(颜色[计数器]);
//e、 GetTouchPoint()
//对于(int counter=0;counter
不要重复使用相同的
多段线
实例。
删除该行:

Polyline freeline=new Polyline();


当您需要绘制另一条
多段线时,创建一条新的多段线并绘制它。

错误是不言自明的,您多次将相同的
自由线添加到VisualTree中。您需要使用某种工厂方法,以便在添加到
画布之前始终获得
多段线的新实例ldren
collection

如果以下答案之一解决了您的问题,请标记为答案。
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);
    }