如何从代码后面设置PathIcon

本文关键字:设置 PathIcon 代码 | 更新日期: 2023-09-27 18:17:03

如何在后台代码中编写以下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>

如何从代码后面设置PathIcon

试试,

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

希望这对你有帮助

In Resource Dictionary (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"]);