嘿大家我试图总结并提出基本问题和我迄今为止无效的想法:S
基本上我的问题是:用户将元素添加到一起,并且我想基于这些图创建新元素,以便可以为用户定义的元素创建新路径.可以说我有一个正方形和一个三角形.用户将其与房屋相结合.现在我想让房子成为用户的元素.为此,我需要元素的路径,我该如何创建它?
我的想法 使用的图形元素基于路径字符串.因此,我希望将这些转换为我稍后可以使用的几何元素.我在下面的答案中使用了AndréMeneses提供的代码,代码在这里复制:
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); …Run Code Online (Sandbox Code Playgroud)