【问题标题】:How to set PathIcon from code behind?如何从后面的代码中设置 PathIcon?
【发布时间】:2021-05-11 01:14:16
【问题描述】:

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

【问题讨论】:

    标签: c# winrt-xaml windows-8.1


    【解决方案1】:

    试试,

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

    希望对你有帮助:)

    【讨论】:

    • 这很棒。谢谢!
    【解决方案2】:

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

    【讨论】:

    • 我一直在寻找将 SVG 转换为几何图形的解决方案已经有一段时间了。你的回答拯救了我的一天!我从来没有意识到有这个助手类存在。
    猜你喜欢
    • 2014-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-03
    • 1970-01-01
    • 2012-09-20
    • 1970-01-01
    相关资源
    最近更新 更多