8

我想在 XAML中使用很棒的字体( 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;";

有任何想法吗?

4

2 回答 2

7

try the following :

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

and if you want to preview your textblock XAML use

t_out.Text = XamlWriter.Save(textBlock);
于 2013-05-07T00:22:29.467 回答
0

您需要直接引用 Font Awesome unicode,因为手动实现可能会导致错误。这是一个对 Font 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";
   }
}
于 2018-10-16T16:45:59.780 回答