WPF C# 路径:如何从带有路径数据的字符串获取到代码中的几何图形(不在 XAML 中) [英] WPF C# Path: How to get from a string with Path Data to Geometry in Code (not in XAML)

查看:16
本文介绍了WPF C# 路径:如何从带有路径数据的字符串获取到代码中的几何图形(不在 XAML 中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在代码中生成一个 WPF Path 对象.

I want to generate a WPF Path object in Code.

在 XAML 中,我可以这样做:

In XAML I can do this:

 <Path Data="M 100,200 C 100,25 400,350 400,175 H 280">

如何在代码中做同样的事情?

How can I do the same in Code?

 Path path = new Path();
 Path.Data = "foo"; //This won't accept a string as path data.

是否有可用的类/方法将带有 PathData 的字符串转换为 PathGeometry 或类似的?

Is there a class/Method available that converts the string with PathData to PathGeometry or similar?

肯定会以某种方式解析 XAML 并转换数据字符串?

Surely somehow the XAML gets parsed and the Data-string converted?

推荐答案

var path = new Path();
path.Data = Geometry.Parse("M 100,200 C 100,25 400,350 400,175 H 280");

Path.Data 是几何类型.使用 Reflector JustDecompile (eff Red Gate),我查看了 Geometry 的 TypeConverterAttribute 定义(xaml 序列化程序使用它来转换 stringGeometry).这将我指向了 GeometryConverter.查看实现,我看到它使用 Geometry.Parse 将路径的字符串值转换为 Geometry 实例.

Path.Data is of type Geometry. Using Reflector JustDecompile (eff Red Gate), I looked at the definition of Geometry for its TypeConverterAttribute (which the xaml serializer uses to convert values of type string to Geometry). This pointed me to the GeometryConverter. Checking out the implementation, I saw that it uses Geometry.Parse to convert the string value of the path to a Geometry instance.

这篇关于WPF C# 路径:如何从带有路径数据的字符串获取到代码中的几何图形(不在 XAML 中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆