本文介绍了转换XAML的PathGeometry到WPF的PathGeometry的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想这是由线段的的PathGeometry。 所以,我用这首代码,但它的错误。 的PathGeometry TEMP =(的PathGeometry)Geometry.Parse(< PathGeometry.Figures>+ <的PathFigure StartPoint可以= \193.5, 283.5\isClosed返= \True\>中+ < PathFigure.Segments>+ <线段点= \418.5,283.5\/ >中+ <线段点= \418.5,508.5\/>中+ <线段点= \193.5,508.5\/> + <线段点= \193.5,283.5\/>中+ < /PathFigure.Segments>+ < /&的PathFigure GT; + < /PathGeometry.Figures>); 如果我用这个第二个代码,这不是错误,但它不包括线段的。结果将PolyLineSegment但我想线段。 的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的新路径=(System.Windows.Shapes.Path)System.Windows.Markup。 XamlReader.Parse(<路径的xmlns =HTTP://schemas.microsoft.com/winfx/2006/xaml/presentation'宽= '20'高度= '80'拉伸=补补='#FF000000'数据='M 20,25.2941L 20,29.4118L 15.9091,29.4118L 15.9091,40L 12.2727,40L 12.2727,29.4118L 2.54313e-006,29.4118L 2.54313e-006,25.6985L 13.4872,7.62939e-006L 15.9091,7.62939e -006L 15.9091,25.2941L 20,25.2941 ZM 12.2727,25.2941L 12.2727,5.28493L 2.09517,25.2941L 12.2727,25.2941 ZM 20,65.2941L 20,69.4118L 15.9091,69.4118L 15.9091,80L 12.2727,80L 12.2727,69.4118L -5.08626电子006,69.4118L -5.08626e-006,65.6985L 13.4872,40L 15.9091,40L 15.9091,65.2941L 20,65.2941 ZM 12.2727,65.2941L 12.2727,45.2849L 2.09517,65.2941L 12.2727,65.2941 Z'的Horizo​​ntalAlignment ='左' VerticalAlignment ='顶'保证金='140,60,0,0/>中); LayoutRoot.Children.Add(的新路径); 如果您正在使用的代码背后,是有要分析一个XAML代码片段的原因是什么?您可以按如下编程创建路径: 路径path =新路径(); 的PathGeometry几何=新的PathGeometry(); 的PathFigure图=新的PathFigure(); figure.StartPoint =新的点(10,10); figure.Segments.Add(新线段() {点=新的点(20,20)}); //例如添加更多的细分这里 geometry.Figures.Add(图) path.Data =几何; 一个路径由一几何形状,它是由数字,这是由段的 如果你想使用简化路径数据在后面的代码,你可以使用通用值转换器: http://www.scottlogic.co.uk/博客/科林/ 2010/07 / A-普世价值的转换器换WPF / I want the PathGeometry that consist of LineSegment.So, I use this first code but it's error.PathGeometry temp = (PathGeometry)Geometry.Parse( "<PathGeometry.Figures>" + "<PathFigure StartPoint=\"193.5,283.5\" IsClosed=\"True\">" + "<PathFigure.Segments>" + "<LineSegment Point=\"418.5,283.5\" />" + "<LineSegment Point=\"418.5,508.5\" />" + "<LineSegment Point=\"193.5,508.5\" />" + "<LineSegment Point=\"193.5,283.5\" />" + "</PathFigure.Segments>" + "</PathFigure>" + "</PathGeometry.Figures>");If I use this second code, it's not error but it doesn't consist of LineSegment. The result will be PolyLineSegment but I want 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");How do I convert XAML PathGeometry to WPF PathGeometry?Thanks 解决方案 Your code for parsing the XAML is incorrect, you need to use a XAML reader and cast the result to the required type. e.g.:System.Windows.Shapes.Path newPath = (System.Windows.Shapes.Path)System.Windows.Markup.XamlReader.Parse("<Path xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' Width='20' Height='80' Stretch='Fill' Fill='#FF000000' Data='M 20,25.2941L 20,29.4118L 15.9091,29.4118L 15.9091,40L 12.2727,40L 12.2727,29.4118L 2.54313e-006,29.4118L 2.54313e-006,25.6985L 13.4872,7.62939e-006L 15.9091,7.62939e-006L 15.9091,25.2941L 20,25.2941 Z M 12.2727,25.2941L 12.2727,5.28493L 2.09517,25.2941L 12.2727,25.2941 Z M 20,65.2941L 20,69.4118L 15.9091,69.4118L 15.9091,80L 12.2727,80L 12.2727,69.4118L -5.08626e-006,69.4118L -5.08626e-006,65.6985L 13.4872,40L 15.9091,40L 15.9091,65.2941L 20,65.2941 Z M 12.2727,65.2941L 12.2727,45.2849L 2.09517,65.2941L 12.2727,65.2941 Z ' HorizontalAlignment='Left' VerticalAlignment='Top' Margin='140,60,0,0'/>");LayoutRoot.Children.Add(newPath);If you are using code-behind, is there any reason you want to parse a XAML snippet? You can programmatically create a path as follows: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)});// e.g. add more segments heregeometry.Figures.Add(figure);path.Data = geometry;A path is composed of a geometry, which is composed of figures, which are composed of segments!If you want to use the simplified path data in code behind you could use a universal value converter:http://www.scottlogic.co.uk/blog/colin/2010/07/a-universal-value-converter-for-wpf/ 这篇关于转换XAML的PathGeometry到WPF的PathGeometry的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-02 21:14