我的代码需要一些帮助 . combineShape.Freeze()函数看起来效果不佳 .
在这种情况下,我有两个单选按钮,名为rbtPoint和rbtLine . 当我单击rbtPoint并单击画布时,它将创建一个点,现在我必须在画布中创建2个点(ellipse1,ellipse2),在此步骤中我已经能够在画布周围拖动Point(ellipse1或elipse2) . 然后当我点击rbtLine然后点击第一个点(ellipse1)它会将点的颜色从红色变为蓝色,然后当我再次点击第二个点(ellipse2)时,它也会将颜色变为红色,它将会创建从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()函数后,当我再次单击其中一个Point并拖动它时,它只移动点击的点,而不是鼠标移动后的其他点和线 .
如何解决这个问题呢?我发现很多相关的文章,但大多数文章他们在XAML中执行代码而不是代码 . 或者还有其他方法可以解决这个问题吗?我需要建议这个问题,
感谢您的帮助,代码大师......
Fikri
1 回答
听起来像你真的不想冻结它 . Freeze()使几何体不可更改,这正是它不会改变的原因 .
唯一的另一种选择是执行类似clone()冻结几何体的操作,然后删除冻结的几何体 .