从代码 (C#) 使用 FontIcon Glyph

using FontIcon Glyph from code (C#)

我尝试添加一些来自 FontIcon class 的基本提供的图标,用于通用 windows 10 个应用程序(我们主要在应用程序栏中看到的应用程序)。 运行宁这段代码时,它运行没有任何问题,但实际上它显示了一些边框方块,例如无法识别的表情符号。

Button infoButton = new Button();
infoButton.Content = new FontIcon
{
    FontFamily = new FontFamily("Segoe MDL2 Assets"),
    Glyph = "",
    Foreground = new SolidColorBrush(Colors.WhiteSmoke)
};

这就是 XAML 和 C# 如何处理 Unicode 字符的问题。当你在 XAML 代码中使用它时,它类似于 Glyph = "",但当你在 C# 代码中使用它时,它应该像这样:Glyph = "\uE946".

这个细节没有具体的文档,但是MSDN & SO论坛上的一些案例有相同的实现:AppBarButton.Icon doesn't change on runtime.

在后面的C#代码中,我们需要使用转义字符。

或在XAML:

<Button>
    <Button.Content>
        <FontIcon 
            FontFamily="Segoe MDL2 Assets" 
            Glyph="&#xE946;"/>
    </Button.Content>
</Button>