本文介绍了将路径转换为几何形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 大家好我试图总结,并配有基本的问题,我的想法不工作至今:• Basicly我的问题是:用户添加元素结合在一起,我想创建基于这些数据,使得新的路径可以定义元素的用户作出了新的元素。可以说我有一个正方形和三角形。用户结合这房子。现在,我想使房子为用户的元素。为此,我需要的元素的路径,我怎么创造的? 我的想法 图元件,其使用基于路径串。因此,我希望这些转换为我以后可以用几何元素。我用安德烈·梅内塞斯提供可在下面的答案,这里的代码复制代码: 公共静态几何PathMarkupToGeometry(ShieldGearViewModel shieldGearModelRec) {串pathMarkup = shieldGearModelRec.Gear.Path; 试 {串XAML = <路径+ 的xmlns =HTTP://schemas.microsoft.com/winfx/2006/xaml /演示'>中+ < Path.Data>中+ pathMarkup +< /Path.Data>< /路径>中; VAR路径= System.Windows.Markup.XamlReader.Load(XAML)为System.Windows.Shapes.Path; //从分离路径如果的PathGeometry { path.Height = shieldGearModelRec.Gear.Height(路径!= NULL); path.Width = shieldGearModelRec.Gear.Width; path.Fill =新的SolidColorBrush(Colors.Green); path.Stretch = Stretch.Fill; 几何形状= path.Data; //测试不工作,则抛出异常 //矩形transRect =新的矩形(shieldGearModelRec.Gear.x,shieldGearModelRec.Gear.y,shieldGearModelRec.Gear.Width,shieldGearModelRec.Gear.Height); //geometry.Transform.TransformBounds(transRect); path.Data = NULL; 返回几何形状; } 返回NULL; } 赶上(异常前) {的Debug.WriteLine(除息); } 返回NULL; } 这我得到一个几何体,以遵循的这个链接描述。与上面的问题是,我不能访问新的几何元素的x或y位置,让我怎么指定这个位置? 有关我觉得位置此链接可能是一个解决方案,只是还没有得到它的工作了吗? :) 当这样做我把它添加到基于链路之前,这样我可以得到一个路径,为新元素的geometrygroup。但是geometrygroup 0作为边界。 因此,对于这个工作,我需要定义x和y为单个几何元素,然后这可能会解决这个问题geomtrygroup或以后我就看这个:)问题一直站立TOOOO不久后:| 文字下面是一个老问题与思考 我有一个字符串,我想转换成一个几何形状在后面的代码。所以,我发现这个#2的 WPF C#路径:如何从路径数据串获得代码(不XAML) 此链接表明,人们可以用解析用下面的代码字符串转换为路径: VAR路径=新路径() ; path.Data = Geometry.Parse(M 100,200Ç100,25 400350 400175ħ280); 不过解析不可用在Windows Phone。我的其他方面的努力一直没有解决的问题。我试着用的PathGeometry,但似乎没有可以设置一个字符串作为路径? 所以我的问题是如何将一个字符串转换为几何形状在后面的代码不绑定在视图的元素。 第一步 所以我成功地与创建路径以下 VAR pathTesting =新System.Windows.Shapes.Path(); 变种B =新System.Windows.Data.Binding {来源= DecorationOnShield [I] .Gear.Path }; System.Windows.Data.BindingOperations.SetBinding(pathTesting,System.Windows.Shapes.Path.DataProperty,B); 现在我想将路径转换为几何形状。 额外 我的想法是做一样的这个链接描述。凡示例所示: VAR blackRectGeometry =新RectangleGeometry(); 矩形RCT =新的矩形(); rct.X = 80; rct.Y = 167; rct.Width = 150; rct.Height = 30; blackRectGeometry.Rect = RCT; 但不是的长方形的我想用一个*任意形状拍拍* H的形式,但仍然能够设置信息,比如坐标的和的尺寸的 额外 我想定义它包括一个路径的用户控件只是看起来像这样的: <用户控件X:类=UndoRedoShield.View.Geometry的xmlns =http://schemas.microsoft.com/winfx/2006/xaml/演示的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的命名空间:System.Windows.Interactivity;装配=系统。 Windows.Interactivity的xmlns:CMD =CLR的命名空间:GalaSoft.MvvmLight.Command;装配= GalaSoft.MvvmLight.Extras.WP8 MC:可忽略=D画布。左={结合Gear.x}Canvas.Top ={结合Gear.y}> <路径数据={结合Gear.Path}填充={结合Gear.Color}拉伸=填充UseLayoutRounding =FALSEHEIGHT ={结合Gear.Height}WIDTH ={结合Gear.Width}不透明度={结合Gear.Opacity}> < Path.RenderTransform> < ScaleTransform的ScaleX ={结合Gear.Scale}的scaleY ={结合Gear.Scale}/> < /Path.RenderTransform> < /路径和GT; < /用户控件> 但一直没能在几何学方面的任何方式来使用它。任何人都有使用这种方法的任何想法?任何方法表示赞赏! :) 额外多余的:) 是否有可能建立一个几何塑造出UI元素,如认为其显示的可转换为几何路径一个用户控件? 进度 我发现的此链接我在哪里可以创建一个路径的几何形状。该路径具有属性的宽度和高度 但是属性我不几何形状或路径具有如下: Canvas.Left Canvas.Top Canvas.ZIndex(我认为这是可能的,当我它添加到GeometryGroup) 好像这可以通过Path.Data的bounds属性来完成。但不是zIndex的。因此,这仍然需要与geometryGroup进行测试,和几何需要被添加到GeometryGroup。 解决方案 前段时间,同时寻找一个解决方案,我结束了创建这个功能。也许这会帮助你。 公共静态几何PathMarkupToGeometry(字符串pathMarkup) {试 {串XAML = <路径+ 的xmlns =HTTP://schemas.microsoft.com/winfx/2006/xaml/presentation'> + < Path.Data>中+ pathMarkup +< /Path.Data>< /路径>中; VAR路径= XamlReader.Load(XAML)为路径; (!PATH = NULL) //从分离路径如果的PathGeometry {几何形状= path.Data; path.Data = NULL; 返回几何形状; } 返回NULL; } 赶上(异常前) {的Debug.WriteLine(除息); } 返回NULL; } 然后,我这样调用这个函数: VAR箭头=新路径 {数据= DesignHelpers.PathMarkupToGeometry(M-1,0 L0,1 L1,0) , =填充新的SolidColorBrush(Colors.Black),弹力= Stretch.Fill,高度= 12,宽度= 18,的HorizontalAlignment = HorizontalAlignment.Center }; 我不知道这是否会满足您的需求究竟但也许它可以帮助。 Hey everyone I have tried to sum up and come with the basic question and my idea which does not work so far :SBasicly my question is:The user adds elements together, and I want to create a new element based on these figures, such that a new path can be made for the users defined element. Lets say I have a square and a triangle. The user combines this to a house. Now I want to make the house an element for the user. For this I need the path of the element, how do I create this?My IdeaThe figure elements which are used are based on path strings. Therefore I want these to be converted to a geometry element that I can use later on. I use the code supplied by André Meneses in the answer below, the code replicated here: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; }This I do to get a Geometry to follow the example from this link describes. The problem with the above is that I cannot access x or y positions, of the new geometry element, so how do I specify this position?For the position I think this link might be a solution, just haven't gotten it working yet? :)When this is done I add it to a geometrygroup based on the before link, such that I can get a path, for the new element. But the geometrygroup has 0 as bounds.So for this to work I need to define x and y for the individual geometry elements, and then this might solve the geomtrygroup problem or I then have to look at this after :) Question has been standing for toooo long :|TEXT BELOW IS THE OLD QUESTION AND THOUGHTSI have a string that I would like to convert to a geometric shape in the code behind. So I found this on Stackoverflow WPF C# Path: How to get from a string with Path Data to Geometry in Code (not in XAML)This link suggests that one can convert a string to path using parse with the following code:var path = new Path();path.Data = Geometry.Parse("M 100,200 C 100,25 400,350 400,175 H 280");However parse is not available on Windows Phone. My other efforts has not solved the issue. I tried with pathGeometry but did not seem to be possible to set a string as the path?So my problem is how to convert a string to a geometric shape in code behind not binding to an element on view.First StepSo I succeeded in creating a path with the followingvar 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);Now I am trying to convert the path to a geometric shape.ExtraMy idea is to do the same as this link describes. Where the example shows :var blackRectGeometry = new RectangleGeometry();Rect rct = new Rect();rct.X = 80;rct.Y = 167;rct.Width = 150;rct.Height = 30;blackRectGeometry.Rect = rct;But instead of rectangle I would like to use an *arbitrary shape in form of a pat*h, but still be able to set information such as coordinates and size.ExtraI was thinking of defining a usercontrol which consisted of a path only looking like this:<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>But have not been able to use it in any way regarding geometry. Anyone have any idea using this method? Any method is appreciated ! :)extra extra :)Is it possible to create a geometric shape out of uielements, such that a usercontrol that is rendered Can be converted to a geometric Path?ProgressI found this link Where I can create a geometry from a path. The path has the property width and height.But the properties I do not have in geometry or path is the following:Canvas.LeftCanvas.TopCanvas.ZIndex (I think this is possible when I add it to a GeometryGroup)Seems like this can be done through the bounds property of the Path.Data. But not ZIndex. So this still needs to be tested with geometryGroup, and the Geometry needs to be added to the GeometryGroup. 解决方案 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. 这篇关于将路径转换为几何形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-11 02:20