8

嘿,我试图总结并提出基本问题和我的想法到目前为止还行不通的每个人: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);
                //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# Path 上找到了这个:How to get from a string with Path Data to Geometry in Code (not in XAML)

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

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;

但是我想使用路径形式的任意形状而不是矩形,但仍然能够设置坐标大小等信息。

额外的

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

<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 中创建几何形状,以便渲染的用户控件可以转换为几何路径?

进步

我找到了这个链接Where I can create a geometry from a path。路径具有属性宽度和高度。

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

  1. 画布.左
  2. 帆布.顶部
  3. Canvas.ZIndex(我认为当我将它添加到 GeometryGroup 时这是可能的)

似乎这可以通过 Path.Data 的 bounds 属性来完成。但不是 ZIndex。所以这个还是需要用geometryGroup进行测试,需要将Geometry加入GeometryGroup。

4

2 回答 2

7

前段时间,在为此寻找解决方案时,我最终创建了此功能。也许它会帮助你。

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

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

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

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

于 2014-08-07T15:11:08.047 回答
3

Windows Phone 平台确实缺少 Geometry.Parse(据我所知,它是后端 StreamGeometry),但根据此页面: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 语法生成路径:

将 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("<Path Data='" + data + "'/>") as Path;
于 2014-04-23T02:57:35.683 回答