Skip to main content
 首页 » 编程设计

c#之如何从代码后面设置 PathIcon

2024年02月06日12Free-Thinker

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

请您参考如下方法:

尝试,

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

希望这对你有帮助:)