Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF,将Path.DataProperty转换为段对象_Wpf_Path_Geometry_Line_Segment - Fatal编程技术网

WPF,将Path.DataProperty转换为段对象

WPF,将Path.DataProperty转换为段对象,wpf,path,geometry,line,segment,Wpf,Path,Geometry,Line,Segment,我想知道是否有一种工具可以将路径数据(如“M 0 0 l 10 10”)转换为其等效的直线/曲线段代码 目前我正在使用: string pathXaml = "<Path xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" Data=\"M 0 0 l 10 10\"/>"; Path p

我想知道是否有一种工具可以将路径数据(如“M 0 0 l 10 10”)转换为其等效的直线/曲线段代码

目前我正在使用:

string pathXaml = "<Path xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\" Data=\"M 0 0 l 10 10\"/>";
Path path = (Path)System.Windows.Markup.XamlReader.Load(pathXaml);
string pathXaml=”“;
Path Path=(Path)System.Windows.Markup.XamlReader.Load(pathXaml);

在我看来,调用XamlParser比显式创建线段慢得多。但是,手动转换许多路径非常繁琐。

没有内置的工具可以从几何体微型语言生成C#或VB代码,但您可以按如下方式创建一个:

  • 发出用于创建新路径几何体的C#或VB代码
  • 调用PathFigureCollection.Parse,对路径字符串进行分析。这将返回一个
    PathFigureCollection
    实例
  • 在PathFigureCollection上迭代。对于每个图形:
    • 编写C#或VB代码,用于新建PathFigure对象并将其添加到PathGeometry.Figures集合
    • 迭代地物的Segments集合。对于每个段,分析其类型并发出与类型相关的代码,以便创建适当类型的PathSegment,设置其属性并将其添加到当前PathFigure
这是否比手动转换路径更繁琐,只有您可以决定,不过。。。这可能取决于需要处理多少种不同类型的线段(即路径字符串中出现多少种不同类型的线段),因为必须为线段、圆弧段等编写单独的代码


编辑:感谢Anvaka在评论中通过提请我注意PathFigureCollection.Parse简化了原始答案。此程序将进行转换:

Hi itowlson,问题的作者似乎使用了XamlReader,这对于此类操作来说确实很慢。相反,您可以建议他使用Geometry.Parse()方法。这比XamlReader快。顺便说一句,在您的场景中,您还可以省略转换器,并使用PathFigureCollection.Parse():)。。。我仍然认为你的答案是正确的。安瓦卡:孩子,我的脸红了吗。我甚至没有意识到这些方法是公开的——我认为获得它们的唯一方法是通过转换器。我会更新的——非常感谢!(关于XamlReader vs string解析,看起来他在我写作时编辑了这个问题——在原始版本中,他直接设置了代码中字符串的Path属性。)感谢所有的答案,是的,我编辑了这个问题,我程序中的代码略有不同,但想法是一样的。我将实现建议的优化。