Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
962 views
in Technique[技术] by (71.8m points)

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

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;
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...