将 XAML PathGeometry 转换为 WPF PathGeometry

发布于 2024-10-09 09:08:18 字数 1261 浏览 2 评论 0原文

我想要由 LineSegment 组成的 PathGeometry。

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

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>");

如果我使用第二个代码,它不是错误,但它不包含 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?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

就像说晚安 2024-10-16 09:08:18

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

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);

如果您使用代码隐藏,您是否有任何理由想要解析 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)
});

// e.g. 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/

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 here

geometry.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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文