如何从代码后面设置PathIcon?

时间:2014-08-07 17:10:09

标签: c# winrt-xaml windows-8.1

如何在代码后面编写以下XAML?我在理解它的PathIcon Data部分时遇到了问题。

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

2 个答案:

答案 0 :(得分:3)

尝试,

this.appBarButton.Icon = new PathIcon(){ Data = PathMarkupToGeometry(Your xaml data string)};

在这种情况下: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;
}

希望这可以帮助你:)

答案 1 :(得分:1)

在资源字典 (xaml) 中:

   <x:String x:Key="LargePathIcon">M3 20V3H20V20H3ZM19 4H4V19H19V4Z</x:String>

在 .cs 中

var icon = new PathIcon();
icon.Data = (Geometry) XamlBindingHelper.ConvertValue(typeof(Geometry), (string) App.Current.Resources["LargePathIcon"]);