大家好,我试图总结并提出基本问题和我的想法,但到目前为止还行不通:S
基本上我的问题是:
用户将元素添加在一起,我想根据这些数字创建一个新元素,以便可以为用户定义的元素创建一个新路径。假设我有一个正方形和一个三角形。用户将其组合成一座房子。现在我想让房子成为用户的一个元素。为此,我需要元素的路径,如何创建它?
My Idea使用的图形元素基于路径字符串。因此,我希望将它们转换为稍后可以使用的几何元素。我使用提供的代码安德烈·梅内塞斯在下面的答案中,代码复制在这里:
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;
}
我这样做是为了得到一个几何图形来遵循示例这个链接描述了。上述问题是我无法访问新几何元素的 x 或 y 位置,那么如何指定这个位置?
对于我认为的职位这个链接可能是一个解决方案,只是还没有发挥作用? :)
完成此操作后,我将其添加到基于之前链接的几何组中,以便我可以获得新元素的路径。但几何组以 0 作为边界。
因此,为了使其工作,我需要为各个几何元素定义 x 和 y,然后这可能会解决 geomtrygroup 问题,或者我必须在之后查看这个问题:) 问题已经存在太久了:|
下面的文字是老问题和想法
我有一个字符串,我想在后面的代码中将其转换为几何形状。所以我在 Stackoverflow 上找到了这个WPF C# 路径:如何在代码中(而不是在 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);
现在我正在尝试将路径转换为几何形状。
Extra
我的想法是做同样的事情这个链接描述了。其中示例显示:
var blackRectGeometry = new RectangleGeometry();
Rect rct = new Rect();
rct.X = 80;
rct.Y = 167;
rct.Width = 150;
rct.Height = 30;
blackRectGeometry.Rect = rct;
但不是长方形我想用一个拍打的任意形状h,但仍然可以设置信息,例如坐标 and size.
Extra
我正在考虑定义一个用户控件,其中包含一条看起来像这样的路径:
<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>
但在几何方面还无法以任何方式使用它。有人知道使用这种方法吗?任何方法都值得赞赏! :)
额外额外:)
是否可以从 uielements 创建几何形状,以便渲染的用户控件可以转换为几何路径?
Progress
I found 这个链接我可以在其中从路径创建几何图形。路径具有宽度和高度属性。
但我在几何或路径中没有的属性如下:
- 画布.左
- 帆布.顶部
- Canvas.ZIndex(我认为当我将其添加到 GeometryGroup 时这是可能的)
似乎这可以通过 Path.Data 的bounds 属性来完成。但不是 ZIndex。所以这个还是需要用geometryGroup来测试,需要将Geometry添加到GeometryGroup中。