Convert path to geometric shape

别等时光非礼了梦想. 提交于 2019-11-30 19:46:59

Some time ago, while searching for a solution for this and I ended up creating this function. Maybe it will help you.

    public static Geometry PathMarkupToGeometry(string pathMarkup)
    {
        try
        {
            string xaml =
            "<Path " +
            "xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>" +
            "<Path.Data>" + pathMarkup + "</Path.Data></Path>";
            var path = XamlReader.Load(xaml) as Path;
            // Detach the PathGeometry from the Path
            if (path != null)
            {
                Geometry geometry = path.Data;
                path.Data = null;
                return geometry;
            }
            return null;
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex);
        }
        return null;
    }

Then I call this function like this:

     var arrow = new Path
     {
        Data = DesignHelpers.PathMarkupToGeometry("M-1,0 L0,1 L1,0"),
        Fill = new SolidColorBrush(Colors.Black),
        Stretch = Stretch.Fill,
        Height = 12,
        Width = 18,
        HorizontalAlignment = HorizontalAlignment.Center
    };

I don't know if this will fit your needs exactly but maybe it can help.

steve cook

Geometry.Parse (and as far as I can tell, it's back-end StreamGeometry) is indeed missing from the Windows Phone platform, but according to this page: http://msdn.microsoft.com/en-us/library/ms752293(v=vs.110).aspx

WPF provides two classes that provide mini-languages for describing geometric paths: StreamGeometry and PathFigureCollection.

PathFigureCollection is available for Windows Phone, so this looks like the place to check:

http://msdn.microsoft.com/en-us/library/windowsphone/develop/system.windows.media.pathfigure(v=vs.105).aspx

Now you just need to be able to create PathGeometry from the XAML-style markup. It looks like there are examples here for generating a Path from the XAML syntax:

Convert XAML PathGeometry to WPF PathGeometry

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

Basically something like

string data = "M 100,200 C 100,25 400,350 400,175 H 280";
Path path = XamlReader.Load("<Path Data='" + data + "'/>") as Path;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!