簡體   English   中英

如何從后面的代碼設置 PathIcon?

[英]How to set PathIcon from code behind?

我將如何在后面的代碼中編寫以下 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>

嘗試,

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

希望這對你有幫助:)

在資源字典 (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"]);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM