XAML TextBlock以编程方式设置特殊字符

本文关键字:设置 特殊字符 方式 编程 TextBlock XAML | 更新日期: 2023-09-27 18:12:27

我想在XAML中使用字体awesome (http://fortawesome.github.io/Font-Awesome/design.html)。

我已经能够很容易地通过直接XAML工作,通过创建一个字体文件夹并在那里添加字体,然后在XAML:

<TextBlock FontFamily="Fonts/#FontAwesome">&#xf000;</TextBlock>

显示马提尼酒杯图标。

然而,当以编程方式添加它时,它只是显示一个无效的符号,如:[],我尝试了以下操作:

XAML:

<TextBlock Name="textBlock"></TextBlock>
c#

:

textBlock.FontFamily = new FontFamily("Fonts/#FontAwesome");
textBlock.Text = HttpUtility.HtmlDecode("&#xf000;");

和以下返回字面值字符串:

textBlock.FontFamily = new FontFamily("Fonts/#FontAwesome");
textBlock.Text = "&#xf000;";

任何想法?

XAML TextBlock以编程方式设置特殊字符

尝试以下操作:

textBlock.FontFamily= new FontFamily(new Uri("pack://application:,,,/"), @"/Fonts/#FontAwesome"); // you should well reference your font else you will get a square
textBlock.Text = "'uf000";// 'u (unicode escape char) instead of &#x

如果你想预览你的文本块XAML使用

t_out.Text = XamlWriter.Save(textBlock);

您需要直接引用Font Awesome unicode,因为手动实现可能会导致错误。这里有一个链接,可以帮助字体Awesome类:https://github.com/fzany/Font-Awesome-Cheat-Charp .

你可以这样做:

textBlock.Text = FontAwesome.Solid.Address_Book;

这是一个修剪:访问完整代码的链接。

public class FontAwesome
{
    public static class Solid
    {
        public static string Ad = "'uf641";
        public static string Address_Book = "'uf2b9";
        public static string Address_Card = "'uf2bb";
        public static string Adjust = "'uf042";
        public static string Air_Freshener = "'uf5d0";
        public static string Align_Center = "'uf037";
        public static string Align_Justify = "'uf039";
        public static string Align_Left = "'uf036";
        public static string Align_Right = "'uf038";
        public static string Allergies = "'uf461";
        public static string Ambulance = "'uf0f9";
        public static string American_Sign_Language_Interpreting = "'uf2a3";
        public static string Anchor = "'uf13d";
        public static string Angle_Double_Down = "'uf103";
        public static string Angle_Double_Left = "'uf100";
        public static string Angle_Double_Right = "'uf101";
        public static string Angle_Double_Up = "'uf102";
        public static string Angle_Down = "'uf107";
        public static string Angle_Left = "'uf104";
        public static string Angle_Right = "'uf105";
        public static string Angle_Up = "'uf106";
        public static string Angry = "'uf556";
        public static string Ankh = "'uf644";
        public static string Apple_Alt = "'uf5d1";
        public static string Archive = "'uf187";
        public static string Archway = "'uf557";
        public static string Arrow_Alt_Circle_Down = "'uf358";
        public static string Arrow_Alt_Circle_Left = "'uf359";
        public static string Arrow_Alt_Circle_Right = "'uf35a";
        public static string Arrow_Alt_Circle_Up = "'uf35b";
        public static string Arrow_Circle_Down = "'uf0ab";
        public static string Arrow_Circle_Left = "'uf0a8";
        public static string Arrow_Circle_Right = "'uf0a9";
        public static string Arrow_Circle_Up = "'uf0aa";
        public static string Arrow_Down = "'uf063";
        public static string Arrow_Left = "'uf060";
        public static string Arrow_Right = "'uf061";
        public static string Arrow_Up = "'uf062";
        public static string Arrows_Alt = "'uf0b2";
        public static string Arrows_Alt_H = "'uf337";
        public static string Arrows_Alt_V = "'uf338";
        public static string Assistive_Listening_Systems = "'uf2a2";
        public static string Asterisk = "'uf069";
   }
}