将Bezier曲线段添加到Canvas路径中

5
我目前正在尝试为我的WPF应用程序添加一个BezierSegment到画布中。我得到一个编译时错误,指出无法进行不正确的转换:

参数1:无法将“System.Windows.Media.PathGeometry”转换为“System.Windows.UIElement”

这是我目前的代码:

//bezier curve it 
BezierSegment curve = new BezierSegment(startPoint, endPoint,controlPoint,false);

// Set up the Path to insert the segments
PathGeometry path = new PathGeometry();

PathFigure pathFigure = new PathFigure();
pathFigure.StartPoint = hs.LeStartingPoint;
pathFigure.IsClosed = true;
path.Figures.Add(pathFigure);

pathFigure.Segments.Add(curve);
System.Windows.Shapes.Path p = new Path();
p.Data = path;
this.mainWindow.MyCanvas.Children.Add(path);

任何帮助都将不胜感激!
2个回答

6

您必须向Canvas中添加p (Path),而不是path (PathGeometry)。

BezierSegment curve = new BezierSegment(new Point(11,11), new Point(22,22), new Point(15,15), false);           

// Set up the Path to insert the segments
PathGeometry path = new PathGeometry();

PathFigure pathFigure = new PathFigure();
pathFigure.StartPoint = new Point(11, 11);
pathFigure.IsClosed = true;
path.Figures.Add(pathFigure);

pathFigure.Segments.Add(curve);
System.Windows.Shapes.Path p = new Path();
p.Stroke = Brushes.Red;
p.Data = path;

MyCanvas.Children.Add(p); // Here

2

我现在不在能够检查的机器附近,但我认为你已经接近成功了。你需要添加创建的System.Windows.Shapes.Path对象(你目前没有使用它),以及一些参数来使线条实际渲染:

 System.Windows.Shapes.Path p = new Path();
 p.Data = path;
 p.Fill = System.Windows.Media.Brushes.Green;
 p.Stroke = System.Windows.Media.Brushes.Blue;
 p.StrokeThickness = 1;

 this.MyCanvas.Children.Add(p);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接