C# 如何从代码隐藏设置路径图标?

C# 如何从代码隐藏设置路径图标?,c#,winrt-xaml,windows-8.1,C#,Winrt Xaml,Windows 8.1,如何在代码隐藏中编写以下XAML?我在理解其中的PathIcon数据部分时遇到问题 <AppBarToggleButton Label="PathIcon" Click="AppBarButton_Click"> <AppBarToggleButton.Icon> <PathIcon Data="F1 M 20,20L 24,10L 24,24L 5,24"/> </AppBarToggleButton.Icon>

如何在代码隐藏中编写以下XAML?我在理解其中的PathIcon数据部分时遇到问题

<AppBarToggleButton Label="PathIcon" Click="AppBarButton_Click">
    <AppBarToggleButton.Icon>
        <PathIcon Data="F1 M 20,20L 24,10L 24,24L 5,24"/>
    </AppBarToggleButton.Icon>
</AppBarToggleButton>

试试看

在这种情况下:xaml数据字符串是
F1 M 20,20L 24,10L 24,24L 5,24

Geometry PathMarkupToGeometry(string pathMarkup)
{
        string xaml =
        "<Path " +
        "xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>" +
        "<Path.Data>" + pathMarkup + "</Path.Data></Path>";
        var path = XamlReader.Load(xaml) as Windows.UI.Xaml.Shapes.Path;
        // Detach the PathGeometry from the Path
        Geometry geometry = path.Data;
        path.Data = null;
        return geometry;
}
Geometry路径标记uptogeometry(字符串路径标记)
{
字符串xaml=
"" +
“+pathMarkup+”;
var path=XamlReader.Load(xaml)为Windows.UI.xaml.Shapes.path;
//从路径中分离PathGeometry
几何=路径。数据;
path.Data=null;
返回几何;
}
希望这对你有帮助:)

试试看

在这种情况下:xaml数据字符串是
F1 M 20,20L 24,10L 24,24L 5,24

Geometry PathMarkupToGeometry(string pathMarkup)
{
        string xaml =
        "<Path " +
        "xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>" +
        "<Path.Data>" + pathMarkup + "</Path.Data></Path>";
        var path = XamlReader.Load(xaml) as Windows.UI.Xaml.Shapes.Path;
        // Detach the PathGeometry from the Path
        Geometry geometry = path.Data;
        path.Data = null;
        return geometry;
}
Geometry路径标记uptogeometry(字符串路径标记)
{
字符串xaml=
"" +
“+pathMarkup+”;
var path=XamlReader.Load(xaml)为Windows.UI.xaml.Shapes.path;
//从路径中分离PathGeometry
几何=路径。数据;
path.Data=null;
返回几何;
}
希望这能帮助您:)

在资源字典(xaml)中:

在资源字典(xaml)中:


这太棒了。谢谢这太棒了。谢谢
   <x:String x:Key="LargePathIcon">M3 20V3H20V20H3ZM19 4H4V19H19V4Z</x:String>
var icon = new PathIcon();
icon.Data = (Geometry) XamlBindingHelper.ConvertValue(typeof(Geometry), (string) App.Current.Resources["LargePathIcon"]);