• WPF 使用.ttf文件中的图标失败


       本章讲述问题:WPF 使用.ttf文件中的图标失败,变成白框问题。

        在WPF开发过程中,我们需要使用.ttf文件中的图标和文字,但是经常会遇到类似问题:WPF 在XMAL里增加图标字体时没办法实时显示出来只显示一个小方框,影响我们观察实际效果,但运行后界面显示却是正常的。

    我们先看一下效果图

     非正常的界面如下:

    显示正常的界面如下:

    **************************************************************************************************************

    本示例中,WPF查看和使用.ttf文件中的字体和图标步骤如下:

    一、在项目中添加字体图标文件的使用,层次结构如下图

    二、设置.ttf文件属性,生成操作设置为资源(Resource)类型

    三:在项目App.xaml中,添加字体文件的引用,代码如下:

    1. <Application.Resources>
    2. <FontFamily x:Key="iconfont">Resources/Fonts/#iconfontFontFamily>
    3. <FontFamily x:Key="bl">Resources/Fonts/#Bahnschrift LightFontFamily>
    4. Application.Resources>

    四:使用文件字体样式引用示例:

    1. <Style TargetType="Button" x:Key="MenuButtonStyle">
    2. <Setter Property="VerticalAlignment" Value="Bottom"/>
    3. <Setter Property="Template">
    4. <Setter.Value>
    5. <ControlTemplate TargetType="Button">
    6. <Grid Background="Transparent">
    7. <Grid.RowDefinitions>
    8. <RowDefinition/>
    9. <RowDefinition Height="22"/>
    10. Grid.RowDefinitions>
    11. <TextBlock Text="{TemplateBinding Tag}"
    12. VerticalAlignment="Center" HorizontalAlignment="Center"
    13. FontFamily="{StaticResource iconfont}" FontSize="26"
    14. Margin="0,5" Foreground="#4F4644"/>
    15. <TextBlock Text="{TemplateBinding Content}"
    16. Grid.Row="1" HorizontalAlignment="Center" FontSize="11"
    17. Foreground="#664F4644"/>
    18. Grid>
    19. ControlTemplate>
    20. Setter.Value>
    21. Setter>
    22. Style>
    23. <Button Content="首页" Grid.Column="1" Style="{StaticResource MenuButtonStyle}"
    24. Tag="" CommandParameter="FirstView">
    25. Button>

    代码编写完后,发现图标显示不出来但是运行时,显示正常。 

    **************************************************************************************************************

    我们从一下两个方面排查:

    (1)引用图标的路径对不对。如果路径不对,你程序运行的时候,也不会显示图标。

    (2)创建的项目中有没有中文字,或者标识符。

    (3)引用资源的类型不正确。

    排查上述三点都是符合的,这就不经思考起来,难道引用的路径还是存在问题?

    于是乎,就修改了一下资源的引用声明

     专门使用Pack Uri:

    1. <Application.Resources>
    2. <FontFamily x:Key="iconfont">pack://application:,,,/WPFControlStyle;component/Resources/Fonts/#iconfontFontFamily>
    3. <FontFamily x:Key="bl">pack://application:,,,/WPFControlStyle;component/Resources/Fonts/#Bahnschrift LightFontFamily>
    4. Application.Resources>

    1. <Application.Resources>
    2. <FontFamily x:Key="iconfont">/WPFControlStyle;component/Resources/Fonts/#iconfontFontFamily>
    3. <FontFamily x:Key="bl">/WPFControlStyle;component/Resources/Fonts/#Bahnschrift LightFontFamily>
    4. Application.Resources>

    或 使用相对路径,前导有“/”
    在其他情况下,文件未声明为内容类型(例如,必须作为资源嵌入到程序集中的图像,或者必须从网络加载的文件)涉及语法变化,这里是资源类型,使用“Resources/Fonts/#iconfont”是不合适的,故在xaml设计界面上不显示图标。

    1. <Application.Resources>
    2. <FontFamily x:Key="iconfont">/Resources/Fonts/#iconfontFontFamily>
    3. <FontFamily x:Key="bl">/Resources/Fonts/#Bahnschrift LightFontFamily>
    4. Application.Resources>

    修改之后,在xaml设计界面图标显示就正常了。

    在此做个笔记,以防后面遇到同样的问题,一时想不到原因和解决方法。

    **************************************************************************************************************

  • 相关阅读:
    Typora自定义主题
    wxFormBuilder + Python 工具开发第三章-日记本工具树节点增、改、删功能
    分享一下做一个电商小程序可以实现什么功能
    操作系统笔记(1)- 计算机硬件介绍
    Codeforces Round #816 (Div. 2) 题解 A,B,C
    容器编排学习(六)服务管理与用户权限管理
    【C++】运算符重载的示例实现和应用
    使用Django Rest Framework设计与实现用户注册API
    DyGRAIN: An Incremental Learning Framework for Dynamic Graphs
    栅格转换为CSV数据时如何优化数据文件的大小
  • 原文地址:https://blog.csdn.net/BYH371256/article/details/133013512