Windows Phone 7:如何在XAML中解析Bezier路径字符串? [英] Windows Phone 7: How to parse Bezier Path string like in XAML?

查看:125
本文介绍了Windows Phone 7:如何在XAML中解析Bezier路径字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要解析Bezier Path字符串,但显然在.Net CF框架中的 System.Windows.Media.Geometry 版本没有 Parse()方法可用于它的普通副本。但是,输入以下XAML确实有效,因此系统必须解析路径数据字符串。



< Path Stroke = BlueData =M 0 0 Q 10 10 20 0/>



XAML之外的自定义字符串?



我当然也可以尝试使用正则表达式编写我自己的解析器,但我不想自己处理这个问题,因为框架很清晰

更新



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

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

当我使用手动解析器更改代码路径进行渲染时,一切正常。
我在这里错过了什么吗?

手动生成数据:

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

字符串regex_input = @M(\ d +)(\d +)Q \\ d +)(\ d +)(\ d +)(\ d +);
Regex regex = new Regex(regex_input);
匹配匹配= 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 =新点(controlx,controly);
quad.Point2 =新点(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);
几何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'/>)作为路径; 

请注意,这不会通过API向您揭示几何图形的细节,但您可以在您的应用中显示结果路径。


I have a need to parse Bezier Path Strings, but apparently the System.Windows.Media.Geometryversion in the .Net CF framework doesn't have the Parse() method that is available in it's normal counterpart. However, entering the following XAML does work, so there must be a way the system parses the Path Data String.

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

Any clue on how I can initiate this parsing myself with a custom string outside XAML?

I ofcourse could also try to write my own parser using regex or so, but I would prefer not to handle this myself, since the framework clearly is capable of it.

Update

When using the suggested XAMLReader, I get a strange Exception when I set a StrokeThickness to the newly created Path:

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

When I change codepath to render using my manual parser, everything works correctly. Am I missing something here? Nothing changes except the parser.

Manually Generating Data:

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

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

解决方案

We don't currently expose an API for the path mini-language parser. It's internal to the XAML parser.

You can, however, create Path objects dynamically based on mini-language strings, using the XamlReader:

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;

Note that this won't actually expose the details of the geometry through the API to you, but you can display the resulting Path in your app.

这篇关于Windows Phone 7:如何在XAML中解析Bezier路径字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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