将路径转换为几何形状

嘿大家我试图总结并提出基本问题和我的想法到目前为止无效:S

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

我的想法使用的图形元素基于路径字符串。 因此,我希望将这些转换为我稍后可以使用的几何元素。 我在下面的答案中使用了AndréMeneses提供的代码,代码在这里复制:

public static Geometry PathMarkupToGeometry(ShieldGearViewModel shieldGearModelRec) { string pathMarkup = shieldGearModelRec.Gear.Path; try { string xaml = "" + "" + pathMarkup + ""; 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; } 

我这样做是为了让Geometry遵循这个链接描述的例子。 上面的问题是我无法访问新几何元素的x或y位置,那么如何指定这个位置呢?

对于这个职位,我认为这个链接可能是一个解决方案,但还没有让它运作起来呢? 🙂

完成后,我将它添加到基于before链接的几何组中,这样我就可以获得新元素的路径。 但几何组的边界为0。 因此,为了实现这一点,我需要为各个几何元素定义x和y,然后这可能会解决几何问题,或者我必须在看完之后看看这个问题:)问题一直存在太久了:

下面的文字是旧问题和思考

我有一个字符串,我想在后面的代码中转换为几何形状。 所以我在Stackoverflow WPF C#Path上找到了这个:如何从Code中的Path Data到Geometry的字符串(不在XAML中)

此链接表明可以使用以下代码使用parse将字符串转换为路径:

 var path = new Path(); path.Data = Geometry.Parse("M 100,200 C 100,25 400,350 400,175 H 280"); 

但是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; 

但是我想使用pat h forms任意形状而不是矩形 ,但仍然可以设置坐标大小等信息

额外

我在考虑定义一个usercontrol,它包含一个只有这样的路径:

        

但是无法以任何方式使用它来处理几何。 任何人都有使用这种方法的想法吗? 任何方法表示赞赏! 🙂

额外的额外:)

是否可以从uielements中创建几何形状,以便渲染的用户控件可以转换为几何路径?

进展

我找到了这个链接,我可以从路径创建几何体。 路径具有属性宽度和高度。

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

  1. Canvas.Left
  2. Canvas.Top
  3. Canvas.ZIndex(我认为当我将它添加到GeometryGroup时这是可能的)

看起来像这样可以通过Path.Data的bounds属性来完成。 但不是ZIndex。 所以这仍然需要使用geometryGroup进行测试,并且需要将Geometry添加到GeometryGroup。

前段时间,在为此寻找解决方案时,我最终创建了这个function。 也许它会对你有所帮助。

  public static Geometry PathMarkupToGeometry(string pathMarkup) { try { string xaml = "" + "" + pathMarkup + ""; 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; } 

然后我这样调用这个函数:

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

我不知道这是否完全符合您的需求,但也许它可以提供帮助。

Geometry.Parse(据我所知,它的后端StreamGeometry)确实从Windows Phone平台中遗漏了,但是根据这个页面: http : //msdn.microsoft.com/en-us/library/ms752293 (v = vs.110)的.aspx

WPF提供了两个类,它们提供了用于描述几何路径的迷你语言:StreamGeometry和PathFigureCollection。

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

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

现在,您只需要能够从XAML样式的标记创建PathGeometry。 看起来这里有一些示例用于从XAML语法生成Path:

将XAML PathGeometry转换为WPF PathGeometry

Windows Phone 7:如何在XAML中解析Bezier Path字符串?

基本上就像是

 string data = "M 100,200 C 100,25 400,350 400,175 H 280"; Path path = XamlReader.Load("") as Path;