C# 将路径转换为几何形状

C# 将路径转换为几何形状,c#,windows-phone-8,path,geometry,pathgeometry,C#,Windows Phone 8,Path,Geometry,Pathgeometry,大家好,我试着总结一下,提出了一个基本问题和我的想法,但到目前为止还不起作用:S 基本上,我的问题是: 用户将元素添加到一起,我想基于这些图创建一个新元素,这样就可以为用户定义的元素创建一个新路径。假设我有一个正方形和一个三角形。用户将其合并到一个房子中。现在我想让房子成为用户的一个元素。为此,我需要元素的路径,如何创建它 我的想法 使用的地物元素基于路径字符串。因此,我希望将它们转换为稍后可以使用的几何体元素。我使用以下答案中提供的代码,代码在此处复制: public static Geome

大家好,我试着总结一下,提出了一个基本问题和我的想法,但到目前为止还不起作用:S

基本上,我的问题是: 用户将元素添加到一起,我想基于这些图创建一个新元素,这样就可以为用户定义的元素创建一个新路径。假设我有一个正方形和一个三角形。用户将其合并到一个房子中。现在我想让房子成为用户的一个元素。为此,我需要元素的路径,如何创建它

我的想法 使用的地物元素基于路径字符串。因此,我希望将它们转换为稍后可以使用的几何体元素。我使用以下答案中提供的代码,代码在此处复制:

public static Geometry PathMarkupToGeometry(ShieldGearViewModel shieldGearModelRec)
    {
        string pathMarkup = shieldGearModelRec.Gear.Path;
        try
        {
            string xaml =
            "<Path " +
            "xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>" +
            "<Path.Data>" + pathMarkup + "</Path.Data></Path>";
            var path = System.Windows.Markup.XamlReader.Load(xaml) as System.Windows.Shapes.Path;
            // Detach the PathGeometry from the Path
            if (path != null)
            {
                path.Height = shieldGearModelRec.Gear.Height;
                path.Width = shieldGearModelRec.Gear.Width;
                path.Fill = new SolidColorBrush(Colors.Green);
                path.Stretch = Stretch.Fill;
                Geometry geometry = path.Data;
                //Test not working, exception is thrown
                //Rect transRect = new Rect(shieldGearModelRec.Gear.x, shieldGearModelRec.Gear.y, shieldGearModelRec.Gear.Width, shieldGearModelRec.Gear.Height);
                //geometry.Transform.TransformBounds(transRect);
                path.Data = null;
                return geometry;
            }
            return null;
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex);
        }
        return null;
    }
但是,Windows Phone上不提供解析。我的其他努力并没有解决这个问题。我尝试使用pathGeometry,但似乎无法将字符串设置为路径

所以我的问题是如何在代码隐藏中将字符串转换为几何形状,而不绑定到视图中的元素

第一步 因此,我成功地创建了一个具有以下内容的路径

var pathTesting = new System.Windows.Shapes.Path();
var b = new System.Windows.Data.Binding
{
    Source = DecorationOnShield[i].Gear.Path
};
System.Windows.Data.BindingOperations.SetBinding(pathTesting, System.Windows.Shapes.Path.DataProperty, b);
现在我尝试将路径转换为几何形状

额外的

我的想法是做同样的事情。其中示例显示:

var blackRectGeometry = new RectangleGeometry();
Rect rct = new Rect();
rct.X = 80;
rct.Y = 167;
rct.Width = 150;
rct.Height = 30;
blackRectGeometry.Rect = rct;
但我不想使用矩形,而是希望使用路径形式的任意形状,但仍然能够设置坐标和大小等信息

额外的

我正在考虑定义一个usercontrol,它由一个看起来像这样的路径组成:

<UserControl x:Class="UndoRedoShield.View.Geometry"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras.WP8"
mc:Ignorable="d"
Canvas.Left="{Binding Gear.x}" Canvas.Top="{Binding Gear.y}">


<Path Data="{Binding Gear.Path}" Fill="{Binding Gear.Color}" Stretch="Fill" UseLayoutRounding="False" Height="{Binding Gear.Height}" Width="{Binding Gear.Width}" Opacity="{Binding Gear.Opacity}">
    <Path.RenderTransform>
        <ScaleTransform ScaleX="{Binding Gear.Scale}" ScaleY="{Binding Gear.Scale}"/>
    </Path.RenderTransform>

</Path>

</UserControl>
     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
    };

但在几何学方面,我们无法以任何方式使用它。有人知道用这种方法吗?欢迎使用任何方法!:)

额外的:)

是否可以使用uielements创建几何图形,以便可以将呈现的usercontrol转换为几何路径

进步

我找到了可以从路径创建几何体的位置。路径具有属性“宽度”和“高度”

但我在几何体或路径中没有的特性如下:

  • 帆布,左边
  • 帆布,上面
  • Canvas.ZIndex(我认为将其添加到GeometryGroup时,这是可能的)
  • 看起来这可以通过Path.Data的bounds属性来完成。但不是津德克斯。因此,这仍然需要使用geometryGroup进行测试,并且需要将几何体添加到geometryGroup。

    Geometry.Parse(据我所知,它是后端StreamGeometry)确实在Windows Phone平台中缺失,但根据此页面:

    WPF提供了两个类,它们提供了用于描述 几何路径:StreamGeometry和PathFigureCollection

    PathFigureCollection适用于Windows Phone,因此这里看起来像是要检查的地方:

    现在,您只需要能够从XAML样式的标记创建PathGeometry。这里似乎有一些从XAML语法生成路径的示例:

    基本上

    string data = "M 100,200 C 100,25 400,350 400,175 H 280";
    Path path = XamlReader.Load("<Path Data='" + data + "'/>") as Path;
    
    string data=“M 100200 C 100,25 400350 400175 H 280”;
    Path Path=XamlReader.Load(“”)作为路径;
    
    不久前,我在寻找解决方案时创建了这个函数。也许对你有帮助

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

    我不知道这是否能完全满足您的需要,但可能会有所帮助。

    根据“Geometry.Parse”可以在以下平台上使用:Windows Phone 8.1、Windows Phone 8。您正在开发哪个版本的windows phone?windows phone 8可能稍后将升级到windows phone 8.1。那么Geometry.Parse不起作用了?你说的不起作用是什么意思?您的项目类型和版本不支持它,或者通过调用Parse方法您没有得到正确的结果?它不可用。我有所有更新,我可以访问除几何体以外的所有几何体对象。解析不可用。我找到了一个解决办法。所以这不是真正的问题。谢谢。也许这是一条更好的道路。但关于创建几何体,它使我能够将所有类型的变量设置为位置。由于我现在无法测试它,您说这在pathfigurecollection或pathgeometry中是可能的?您应该能够设置任何可以通过XAML设置的内容。这种技术的主要限制似乎是,在创建路径后,您无法编辑/更改路径。终于有时间对此进行测试了。而且似乎不起作用。您引用的链接似乎是您可以访问xaml代码以及您引用的方式,但我不想指定xaml代码。我在问题中显示的第一步是获取一个路径,该路径绑定到System.Windows.Shapes.path类型的变量。有了这个,我想创建一个图形或几何体。但是你建议的链接和解决方案似乎不可能实现,XAML代码只是路径描述的包装。我已经更新了这个示例。谢谢,我今晚会测试它。我仍然在测试您提供的代码,而且它似乎可以工作。但是,我还需要指定元素的x、y和z位置。通常使用画布属性完成。你有办法控制这一切吗?bounds属性中有x和y属性,但我的调试器说它们只是返回值。声明arrow元素后,可以执行以下操作:
    Canvas.SetTop(arrow,10)
    Canvas.SetLeft(箭头,20)
    Canvas.SetZIndex(箭头,30)
    然后你必须把arrow元素放到画布中,这样才能工作,很明显……好吧,那么我想我是弄错了。因为我想把它们插入到一个geometrygroup中,以便在WPF中创建一个元素,像这样的链接似乎我还没有找到解决问题的正确方法,该死:)但是路径是由一个geometrygroup组成的,在这里你已经有了路径。为什么要创建一个
         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
        };