如何在 WPF 中使用 Segoe MDL2 Assets 字体以编程方式创建文本块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34724792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:37:21  来源:igfitidea点击:

How to programmatically create textblock using Segoe MDL2 Assets Font in WPF

c#wpfxaml

提问by Percy

I guess this should be easy, but instead of the icon I require, I get a bunch of square boxes.

我想这应该很容易,但是我得到的不是我需要的图标,而是一堆方框。

Originally I was hard coding a menu in xaml:

最初我是在 xaml 中硬编码菜单:

code omitted
<ListBoxItem Name="menuHome" >
<StackPanel Orientation="Horizontal">
    <TextBlock FontFamily="Segoe MDL2 Assets" Text="&#xE10F;" FontSize="16" VerticalAlignment="Center" />
    <TextBlock Text="Home" FontSize="16" VerticalAlignment="Center" Padding="15,0,0,0"/>
</StackPanel>
</ListBoxItem>
code omitted

I now have to dynamically create this menu so I have the following:

我现在必须动态创建这个菜单,所以我有以下内容:

ListBoxItem menuHome = new ListBoxItem();
StackPanel menuHomeStackPanel = new StackPanel() { Orientation = Orientation.Horizontal };
menuHomeStackPanel.Children.Add(new TextBlock() { FontFamily = new FontFamily("Segoe MDL2 Assets"), FontSize = 16, VerticalAlignment = VerticalAlignment.Center, Text = "&#xE10F;" });
menuHomeStackPanel.Children.Add(new TextBlock() { FontSize = 16, VerticalAlignment = VerticalAlignment.Center, Text = "Home", Padding = new Thickness(15, 0, 0, 0) });
menuHome.Content = menuHomeStackPanel;
menuHome.Name = "menuHome";
IconsListBox.Items.Add(menuHome);

This almost gives me the same except for the Segoe MDL2 Assets font which can be seen in the screenshot below:

除了 Segoe MDL2 Assets 字体外,这几乎给了我相同的效果,可以在下面的屏幕截图中看到:

screenshot

截屏

Any ideas - probably simple...?

任何想法 - 可能很简单......?

回答by 15ee8f99-57ff-4f92-890c-b56153

&#xE10F;is an XML character entity. In your original XAML, it was naturally parsed by an XML parser, which, playing by XML parsing rules, converted that substring to a single sixteen-bit Unicode character. By the time the framework created the TextBlockcontrol, &#xE10F;had long since been converted to a single Unicode character.

&#xE10F;是一个 XML 字符实体。在您的原始 XAML 中,它自然由 XML 解析器解析,该解析器根据 XML 解析规则将该子字符串转换为单个 16 位 Unicode 字符。到框架创建TextBlock控件时,&#xE10F;早已转换为单个 Unicode 字符。

But now you've got this C# code. This string is being parsed by the C# parser, whose rules call that a perfectly ordinary string of eightUnicode characters.

但是现在你已经有了这个 C# 代码。这个字符串由 C# 解析器解析,它的规则称之为一个完美的普通字符串,由八个Unicode 字符组成。

Text = "&#xE10F;"

You're passing it eight characters of stuff only an XML parser can make any sense of -- but there's no XML parser involved any more. The control displays those eight characters in your chosen font, and it happens that the font has no glyphs for them, so it displays eight empty boxes.

您向它传递了只有 XML 解析器才能理解的 8 个字符——但不再涉及 XML 解析器。该控件以您选择的字体显示这八个字符,而该字体恰好没有用于它们的字形,因此它显示八个空框。

C# has an equivalent notation for specifying Unicode characters in a string literal. Instead of the &#xprefix and the ';' postfix, you just prefix a four-digit hex character code with \x. Same hex code, but a different way to tell this particular parser (well, technically, the lexer -- but let's not go there just now) that those four hex characters mean something special.

C# 具有用于在字符串文字中指定 Unicode 字符的等效符号。而不是&#x前缀和';' 后缀,您只需在四位十六进制字符代码前加上\x. 相同的十六进制代码,但用不同的方式告诉这个特定的解析器(好吧,从技术上讲,词法分析器——但我们现在不要去那里)这四个十六进制字符意味着一些特殊的东西。

So try this instead:

所以试试这个:

Text = "\xE10F" 

But either of these would work too; C# has more ways to express integers outside of a string literal:

但其中任何一个都可以;C# 有更多的方法来表达字符串文字之外的整数:

Text = Char.ConvertFromUtf32(0xE10F);

//  Same value, in base 10
Text = Char.ConvertFromUtf32(57615);