c# - Windows Phone 7 : How to parse Bezier Path string like in XAML?

标签 c# xaml silverlight windows-phone-7 bezier

我需要解析贝塞尔路径字符串,但显然是 System.Windows.Media.Geometry .Net CF 框架中的版本没有 Parse()在它的正常对应物中可用的方法。但是,输入以下 XAML 确实有效,因此系统必须有一种方法来解析路径数据字符串。

<Path Stroke="Blue" Data="M 0 0 Q 10 10 20 0"/>

关于我如何使用 XAML 外部的自定义字符串自行启动此解析的任何线索?

我当然也可以尝试使用正则表达式等编写我自己的解析器,但我不想自己处理这个问题,因为框架显然能够做到这一点。

更新

使用建议的 XAMLReader 时,当我将 StrokeThickness 设置为新创建的 Path 时,我得到了一个奇怪的异常:

path.StrokeThickness = strokeWidth; //ArgumentException ??? (strokeWidth = 6)

当我更改代码路径以使用我的手动解析器呈现时,一切正常。 我在这里错过了什么吗?除了解析器之外没有任何变化。

手动生成数据:

        //"M {0} {1} Q {2} {3} {4} {5}"

        String regex_input = @"M (\d+) (\d+) Q (\d+) (\d+) (\d+) (\d+)";
        Regex regex = new Regex(regex_input);
        Match match = regex.Match(pathData);

        int startx = int.Parse(match.Groups[1].Value);
        int starty = int.Parse(match.Groups[2].Value);

        int controlx = int.Parse(match.Groups[3].Value);
        int controly = int.Parse(match.Groups[4].Value);

        int endx = int.Parse(match.Groups[5].Value);
        int endy = int.Parse(match.Groups[6].Value);

        PathGeometry geo = new PathGeometry();
        PathFigure figure = new PathFigure();
        figure.StartPoint = new Point(startx, starty);

        QuadraticBezierSegment quad = new QuadraticBezierSegment();
        quad.Point1 = new Point(controlx, controly);
        quad.Point2 = new Point(endx, endy);

        figure.Segments.Add(quad);

        geo.Figures.Add(figure);

        path.Data = geo;

使用 XamlReader

        String formattedXAMLInput = String.Format("<Path xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' Stroke='Black' Data='{0}'/>", pathData);
        Path xamlpath = (Path)XamlReader.Load(formattedXAMLInput);
        Geometry xamldata = xamlpath.Data;
        path.Data = xamldata;

最佳答案

我们目前不公开路径迷你语言解析器的 API。它位于 XAML 解析器的内部。

但是,您可以使用 XamlReader 基于迷你语言字符串动态创建 Path 对象:

Path path = XamlReader.Load("<Path xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' Stroke='Blue' Data='M 0 0 Q 10 10 20 0'/>") as Path;

请注意,这实际上不会通过 API 向您公开几何的详细信息,但您可以在您的应用中显示生成的路径。

关于c# - Windows Phone 7 : How to parse Bezier Path string like in XAML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5596506/

相关文章:

xaml - Xamarin 标签单击更改复选框状态仅 Xaml

xaml - Android 中不出现 ToolbarItem 图标

c# - 为什么我会收到此消息 : "items collection must be empty before using itemssource" in a treeview?

c# - 使用 Silverlight 获取列表框中多个选定项的索引

silverlight - WP7 MediaElement 下载问题

c# - Windows 窗体的可停靠自动隐藏面板控件

c# - 当相关页面从后台堆栈中移除时,从内存中清除 View 模型 - WP8.1

c# - CookieAuthentication cookie 在应用程序池回收后无效

silverlight - Caliburn Entity DataBinding 有趣

c# - 为什么我的 C# IS 语句不起作用?