我需要一些帮助来使用我的代码。 combineShape.Freeze()函数看起来不太好。在代码中使用GeometryGroup创建组合形状
在这种情况下,我有两个单选按钮,名为rbtPoint和rbtLine。当我单击rbtPoint并单击画布时,它将创建一个点,现在我必须在画布中创建2个点(ellipse1,ellipse2),在此步骤中,我已经可以在画布周围拖动Point(椭圆1或椭圆2) 。然后当我点击rbtLine然后点击第一个点(ellipse1)它会将点的颜色从红色变为蓝色,然后当我再次点击第二个点(椭圆2)时,它也会将颜色变成红色,并且它会创建从ellipse1到ellipse2的连接蓝线。 之后,使用GeometryGroup()函数将直线和2个点合并为1个形状。我看到的代码从这个链接:
http://stackoverflow.com/questions/16417950/combining-linegeometry-with-ellipsegeometry-in-code-not-xaml
这里是我的代码来创建功能:
private void myCanvas_MouseDown(object sender, MouseButtonEventArgs e) { mouseDown = true; initialPoint = e.GetPosition(sender as IInputElement); if (rbtPoint.IsChecked == true) { if (e.OriginalSource is Ellipse) { captured = true; shapePoint = e.GetPosition(sender as IInputElement); clickedEllipse = (Ellipse)e.OriginalSource; uielement = (UIElement)e.OriginalSource; } else { shapeEllipse = new Ellipse(); (shapeEllipse as Ellipse).Width = 30; (shapeEllipse as Ellipse).Height = 30; (shapeEllipse as Ellipse).StrokeThickness = 5; (shapeEllipse as Ellipse).Fill = Brushes.Red; Canvas.SetLeft(shapeEllipse, initialPoint.X); Canvas.SetTop(shapeEllipse, initialPoint.Y); myCanvas.Children.Add(shapeEllipse); } } }
private void myCanvas_MouseUp(object sender, MouseButtonEventArgs e) { Mouse.Capture(null); captured = false; mouseDown = false; if (rbtLine.IsChecked == true) { if (e.OriginalSource is Ellipse) { captured = true; initialPoint = e.GetPosition(sender as IInputElement); clickedEllipse = (Ellipse)e.OriginalSource; uielement = (UIElement)e.OriginalSource; poin = e.MouseDevice.GetPosition(uielement); clickedEllipse.Fill = Brushes.Blue; if (firstpoint.X != 0 && firstpoint.Y != 0) { lineEndPoint = initialPoint; } else { firstpoint = initialPoint; } } if (lineEndPoint.X != 0 && lineEndPoint.Y != 0) { Point p = e.GetPosition(sender as IInputElement); shapeLine = new Line(); (shapeLine as Line).X1 = firstpoint.X; (shapeLine as Line).Y1 = firstpoint.Y; (shapeLine as Line).X2 = lineEndPoint.X; (shapeLine as Line).Y2 = lineEndPoint.Y; (shapeLine as Line).Stroke = Brushes.Blue; (shapeLine as Line).StrokeThickness = 5; //(shape as Line).Fill = Brushes.Blue; //Canvas.SetLeft(shape, firstpoint.X); //Canvas.SetTop(shape, firstpoint.Y); myCanvas.Children.Add(shapeLine); lineCreated = true; var ellipse1 = new EllipseGeometry(firstpoint, firstpoint.X, firstpoint.Y); var ellipse2 = new EllipseGeometry(lineEndPoint, lineEndPoint.X, lineEndPoint.Y); var line = new LineGeometry(this.firstpoint, this.lineEndPoint); GeometryGroup combinedShape = new GeometryGroup(); combinedShape.Children.Add(ellipse1); combinedShape.Children.Add(ellipse2); combinedShape.Children.Add(line); combinedShape.Freeze(); } } }
后,我跑combinedShape.Freeze()函数,当我再次点击一个点并拖动它,它只会移动点击点,而没有其他点和线跟随我的鼠标移动。
如何解决这个问题?我发现许多相关的文章,但他们中的大多数人在XAML中执行代码而不是代码隐藏。或者还有其他解决方法吗? 我需要建议,对于这个问题,
感谢你的帮助,代码大师..
Fikri
您是否检查了这篇文章? https://msdn.microsoft.com/en-us/library/ms750593%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396 –