将XAML PathGeometry转换为WPF PathGeometry

我想要由LineSegment组成的PathGeometry。

所以,我使用第一个代码,但它是错误的。

PathGeometry temp = (PathGeometry)Geometry.Parse( "" + "" + "" + "" + "" + "" + "" + "" + "" + ""); 

如果我使用第二个代码,它不是错误,但它不包含LineSegment。 结果将是PolyLineSegment,但我想要LineSegment。

 PathGeometry temp = (PathGeometry)Geometry.Parse( "M29,329L30,331L31,334L33,336L34,338L36,341L38,343L39,345L41,348L44,352L46,353L47,355L48,356L49,357L49,357L50,358L50,358L51,357L50,356L51,354L51,350L53,342L54,334L58,320L60,315L61,311L63,308L63,306L64,304L65,303L65,302L66,301L66,301L66,301L66,301L66,301L66,301L66,301"); 

如何将XAML PathGeometry转换为WPF PathGeometry?

谢谢

您解析XAML的代码不正确,您需要使用XAML阅读器并将结果转换为所需类型。 例如:

 System.Windows.Shapes.Path newPath = (System.Windows.Shapes.Path)System.Windows.Markup.XamlReader.Parse(""); LayoutRoot.Children.Add(newPath); 

如果您使用代码隐藏,是否有任何理由要解析XAML代码段? 您可以按编程方式创建路径,如下所示:

 Path path = new Path(); PathGeometry geometry = new PathGeometry(); PathFigure figure = new PathFigure(); figure.StartPoint = new Point(10,10); figure.Segments.Add(new LineSegment() { Point = new Point (20, 20) }); // eg add more segments here geometry.Figures.Add(figure); path.Data = geometry; 

路径由几何体组成,由图形组成,由图形组成!

如果要在代码中使用简化路径数据,可以使用通用值转换器:

http://www.scottlogic.co.uk/blog/colin/2010/07/a-universal-value-converter-for-wpf/