如何使用C#构建PathGeometry?

如何使用C#构建PathGeometry?,c#,wpf,xaml,windows-runtime,windows-phone-8.1,C#,Wpf,Xaml,Windows Runtime,Windows Phone 8.1,我在Windows Phone 8.1应用程序中使用Windows Runtime运行以下XAML: <Canvas Name="drawSplice" Grid.Row="1" Grid.Column="0" Height="80" Width="80" Background="White"> <Path Stroke="Black"

我在
Windows Phone 8.1
应用程序中使用
Windows Runtime
运行以下
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>

可以使用XamlReader从字符串中解析和加载Xaml对象。您需要一个完整的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);
//我们要创建的路径数据
字符串路径xaml=“M 10,10 C 10,50 65,10 65,70”;
//路径对象的Xaml容器。
//这还可以设置其他属性,如笔划和笔划厚度
字符串xaml=“”+
“+pathXaml+”;
//将Xaml字符串读入路径对象
Windows.UI.Xaml.Shapes.Path Path=XamlReader.Load(Xaml)为Windows.UI.Xaml.Shapes.Path;
//设置上面跳过的其他属性
path.StrokeThickness=3;
path.Stroke=新的SolidColorBrush(Colors.Blue);
//把它添加到我们的画布上
drawSplice.Children.Add(路径);
还可以从对象向上构建路径。Data是一个PathGeometry,它包含一个PathFigure(在您的例子中是一个,但可以更多),其中包含PathSegments(在您的例子中是一个BezierSegments)。使用标记更容易。要了解其工作原理,可以从标记或XamlLoader创建路径,然后检查其集合以了解其构建方式


请注意,Xaml中的路径与包含的图像不匹配。给定的Xaml路径是一个三次贝塞尔曲线,而不是一个椭圆。如果您想要椭圆,可以使用椭圆,或者从椭圆等高线或弧段构建路径。

您可以使用此选项。这将对你有帮助

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);
string data=“M 10,10 C 10,50 65,10 65,70”;
字符串xaml=“”;
var LoadPath=Windows.UI.Xaml.Markup.XamlReader.Load(Xaml);
Windows.UI.Xaml.Shapes.Path Path=加载路径为Windows.UI.Xaml.Shapes.Path;
path.StrokeThickness=3;
path.Stroke=新的SolidColorBrush(Windows.UI.Colors.Black);
drawSplice.Children.Add(路径);

我使用了以下技术:

var b = new Binding
{
   Source = "M 10,10 C 10,50 65,10 65,70"
};
BindingOperations.SetBinding(path, Path.DataProperty, b);
而且它在Windows Phone 8.1上运行得非常好

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