简体   繁体   English

如何使用C#构建PathGeometry?

[英]How to build PathGeometry using C#?

I have the following XAML in Windows Phone 8.1 app running with Windows Runtime : 我在与Windows Runtime一起Windows Runtime Windows Phone 8.1应用程序中具有以下XAML

<Canvas Name="drawSplice"
        Grid.Row="1"
        Grid.Column="0"
        Height="80"
        Width="80"
        Background="White">

        <Path Stroke="Black"
                      StrokeThickness="2"
                      Data="M 10,10 C 10,50 65,10 65,70" />
</Canvas>

It gives me the following shape: 它使我具有以下形状:

在此处输入图片说明

My question is how to generate such instance of path class using C# ? 我的问题是如何使用C#生成此类path类实例?

Windows.UI.Xaml.Shapes.Path path = new Windows.UI.Xaml.Shapes.Path();

//path.Data = "";

path.StrokeThickness = 3;
path.Stroke = new SolidColorBrush(Colors.Black);
Drawer.Children.Add(path); 

You can use a XamlReader to parse and load a Xaml object from your string. 您可以使用XamlReader来解析和加载字符串中的Xaml对象。 You'll need a full Xaml object to read in the path data, not just the data itself: 您需要一个完整的Xaml对象才能读取路径数据,而不仅仅是数据本身:

// The path data we want to create
string pathXaml = "M 10,10 C 10,50 65,10 65,70";

// A Xaml container for the path object.
// This could also set other properties like Stroke and StrokeThickness
string xaml = "<Path " +
"xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>" +
"<Path.Data>" + pathXaml + "</Path.Data></Path>";

// Read the Xaml string into a Path object
Windows.UI.Xaml.Shapes.Path path = XamlReader.Load(xaml) as Windows.UI.Xaml.Shapes.Path;

// Set other properties we skipped above
path.StrokeThickness = 3;
path.Stroke = new SolidColorBrush(Colors.Blue);

// Add it to our Canvas
drawSplice.Children.Add(path);

You could also build the path up from objects. 您也可以从对象构建路径。 The Path.Data is a PathGeometry, which contains a PathFigure (in your case one, but it could be more), which contains PathSegments (in your case a single BezierSegment). Path.Data是一个PathGeometry,它包含一个PathFigure(在您的情况下为一个,但可能更多),其中包含PathSegments(在您的情况下为单个BezierSegment)。 Using markup is easier. 使用标记更容易。 To see how this works you can create a Path from markup or XamlLoader and then examine its collections to see how it's built up. 要查看其工作原理,可以从标记或XamlLoader创建路径,然后检查其集合以了解其构建方式。

Note that the path in your Xaml doesn't match the image you included. 请注意,您Xaml中的路径与您包含的图像不匹配。 The given Xaml path is a cubic Bezier rather than an ellipse. 给定的Xaml路径是三次贝塞尔曲线而不是椭圆形。 If you want an ellipse you can use an Ellipse, or build the Path from an EllipseGeometry or ArcSegments. 如果需要椭圆,则可以使用椭圆,或者从EllipseGeometry或ArcSegments构建路径。

You can use this. 您可以使用它。 It will be helpful for you. 这将对您有所帮助。

string data = "M 10,10 C 10,50 65,10 65,70";
        string xaml = "<Path xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' Data='" + data + "'/>";
        var LoadPath = Windows.UI.Xaml.Markup.XamlReader.Load(xaml); 
        Windows.UI.Xaml.Shapes.Path path = LoadPath as Windows.UI.Xaml.Shapes.Path;
        path.StrokeThickness = 3;
        path.Stroke = new SolidColorBrush(Windows.UI.Colors.Black);
        drawSplice.Children.Add(path);

I have use the following technique: 我使用以下技术:

var b = new Binding
{
   Source = "M 10,10 C 10,50 65,10 65,70"
};
BindingOperations.SetBinding(path, Path.DataProperty, b);

and it is working perfectly on Windows Phone 8.1 . 并且它在Windows Phone 8.1上可以完美运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM