使用Font Awesome更改WPF中的字体图标

Gar*_*eth 15 c# wpf fonts xaml font-awesome

我正在使用Font Awesome的图标来渲染C#WPF应用程序中的基本字体图像.在运行期间,当我尝试更改TextBlock以显示不同的字体图标但显示unicode表示而不是字体图标时.

我已经创建了一个示例应用程序来显示它.单击任一按钮时,它会将TextBlock的Text属性替换为相应图标的unicode.项目中有一个Resources文件夹,其中FontAwesome.ttf字体文件作为构建资源,TextBlock的FontFamily属性引用该文件.

这是我的示例应用程序的源代码:

代码隐藏:

namespace FontAwesomeTest
{
public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }

    private void btnGlass_Click(object sender, RoutedEventArgs e)
    {
        tblkFontIcon.Text = "";            
    }

    private void btnMusic_Click(object sender, RoutedEventArgs e)
    {
        tblkFontIcon.Text = "";
    }

    private void btnSearch_Click(object sender, RoutedEventArgs e)
    {
        tblkFontIcon.Text = "";
    }        
}
}
Run Code Online (Sandbox Code Playgroud)

XAML:

<Window x:Class="FontAwesomeTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Font Awesome Test Window" Height="300" Width="330" Name="FontAwesomeTestWindow">
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="50"/>
        <RowDefinition Height="25"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Button Name="btnGlass" Height="35" Width="85" Click="btnGlass_Click" >Glass</Button>
    <Button Name="btnMusic" Grid.Column="1" Height="35" Width="85" Click="btnMusic_Click">Music</Button>
    <Button Name="btnSearch" Grid.Column="2" Width="85" Height="35"  Click="btnSearch_Click">Search</Button>
    <TextBlock Grid.Row="1" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="18" FontFamily="../Resources/#FontAwesome">&#xf000;</TextBlock>
    <TextBlock Grid.Row="1" Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="18" FontFamily="../Resources/#FontAwesome">&#xf001;</TextBlock>
    <TextBlock Grid.Row="1" Grid.Column="2" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="18" FontFamily="../Resources/#FontAwesome">&#xf002;</TextBlock>
    <TextBlock Name="tblkFontIcon" Grid.Row="2" Grid.ColumnSpan="3" FontSize="64" FontFamily="../Resources/#FontAwesome" HorizontalAlignment="Center" VerticalAlignment="Center">&#xf011;</TextBlock>
</Grid>
Run Code Online (Sandbox Code Playgroud)

我使用以下教程将Font Awesome合并到我的应用程序中http://www.codeproject.com/Tips/634540/Using-Font-Icons

所以从本质上讲,我如何更改Icon但是有一个Icon显示 - 而不是Unicode?

提前致谢.

H. *_*lyn 26

我知道这是一个老问题,但Font Awesome有一个名为FontAwesome.UWPFontAwesome.WPF的NuGet包.只需下载其中一个.

适用于字体的NuGet包非常棒

如果您将使用图标导入跟随命名空间到您的XAML代码中:

xmlns:fa="http://schemas.fontawesome.io/icons/"
Run Code Online (Sandbox Code Playgroud)

像这样在你的按钮中使用它:

<Button x:Name="btnButton">
    <Button.Content>
        <fa:ImageAwesome Icon="LongArrowLeft"/>
    </Button.Content>
</Button>
Run Code Online (Sandbox Code Playgroud)

最后在你的C#代码背后:

using FontAwesome.WPF; // on the top of the code
btnButton.Content = FontAwesomeIcon.LongArrowRight;
Run Code Online (Sandbox Code Playgroud)

  • 这是一个非常好的方式,如何使用fontawesome成为Handy. (3认同)

ter*_*rry 20

UPDATE

我找到了一个关于这个主题的不同帖子 - 在wpf中添加图标字体 我认为这应该更有可能是你想要的.

确保将您的字体添加为资源.然后,使用以下字符串:

<Setter Property="TextElement.FontFamily" Value="pack://application:,,,/fonts/#FontAwesome" />
Run Code Online (Sandbox Code Playgroud)

在上面的字符串中,我假设字体的名称(不是字体的文件名)是FontAwesome.

你只需要:

  1. 将Font添加到项目中,假设您将它们放入"fonts"文件夹中
  2. 将构建操作更改为资源 而非 嵌入资源
  3. 添加样式以设置字体系列,如上面的代码片段,并将其设置为TextBlock.Text您喜欢的图标并将样式应用于TextBlock.

如果要通过更新TextBlock.Text属性来更改图标,则应Text使用支持的unicode字符串设置该属性.

尝试类似的东西

 tblkFontIcon.Text = "\uf000";
Run Code Online (Sandbox Code Playgroud)

而不是

tblkFontIcon.Text = "&#xf000;";
Run Code Online (Sandbox Code Playgroud)

如果您使用的是使用字体图标的代码

然后你可能错过了那篇文章中的"工作原理"一节.您应该使用该标记扩展,而不是使用TextBlock.Text属性.

在他的示例代码中:

<RibbonButton Label="Import data" 
  LargeImageSource="{WpfTools:ImageFromFont Text=&#xf01a;, 
  FontFamily=/RibbonFontIconSample;component/Resources/#FontAwesome, Brush=Brown}" />
Run Code Online (Sandbox Code Playgroud)

注意WpfTools:ImageFromFont,它是Markup Extention,它允许xaml解析器进行转换

{WpfTools:ImageFromFont Text=&#xf01a;, 
      FontFamily=/RibbonFontIconSample;component/Resources/#FontAwesome, Brush=Brown}
Run Code Online (Sandbox Code Playgroud)

ImageSource和分配给LargeImageSource财产.

因此,在您的XAML,你可以更换TextBlockImage,那么就应该是这样的:

<Image Source="{WpfTools:ImageFromFont Text=&#xf000;, 
      FontFamily=/RibbonFontIconSample;component/Resources/#FontAwesome, Brush=Brown}" />
Run Code Online (Sandbox Code Playgroud)

如果要更改图标,则必须更改ImageSource自己,只需按照使用字体图标创建自己的方法,或者只是从该教程中复制以下代码.

private static ImageSource CreateGlyph(string text, 
        FontFamily fontFamily, FontStyle fontStyle, FontWeight fontWeight, 
        FontStretch fontStretch, Brush foreBrush)
{
    if (fontFamily != null && !String.IsNullOrEmpty(text))
    {
        Typeface typeface = new Typeface(fontFamily, fontStyle, fontWeight, fontStretch);
        GlyphTypeface glyphTypeface;
        if (!typeface.TryGetGlyphTypeface(out glyphTypeface))
                throw new InvalidOperationException("No glyphtypeface found");

        ushort[] glyphIndexes = new ushort[text.Length];
        double[] advanceWidths = new double[text.Length];
        for (int n = 0; n < text.Length; n++)
        {
            ushort glyphIndex = glyphTypeface.CharacterToGlyphMap[text[n]];
            glyphIndexes[n] = glyphIndex;
            double width = glyphTypeface.AdvanceWidths[glyphIndex] * 1.0;
            advanceWidths[n] = width;
        }

        GlyphRun gr = new GlyphRun(glyphTypeface, 0, false, 1.0, glyphIndexes,
                                    new Point(0, 0), advanceWidths, 
                                    null, null, null, null, null, null);
        GlyphRunDrawing glyphRunDrawing = new GlyphRunDrawing(foreBrush, gr);
        return new DrawingImage(glyphRunDrawing);

    }
    return null;
}
Run Code Online (Sandbox Code Playgroud)

  • 因为"&#xf000;" 是一个xaml"version"unicode,xaml解析器将它转换为相应的unicode字符串,在c#中为"\ uf000",并将其设置为`tblkFontIcon.Text`. (2认同)