c# - WPF - 字体真棒图标未在运行时显示
问题描述
我正在构建 WPF 应用程序,我想使用 Font Awesome 中的图标。我在样式中包含了 Font Awesome,并在我想使用它的字段中指定了一个字体系列。问题是图标在设计窗口中正确显示,但在运行时变为交叉方块。
有我的字体 XAML
<FontFamily x:Key="ArconRegular">pack://application;,,,/Fonts/#Arcon</FontFamily>
<FontFamily x:Key="ArconRounded">pack://application;,,,/Fonts/#Arcon Rounded-</FontFamily>
<FontFamily x:Key="FASolid">pack://application;,,,/Fonts/#Font Awesome 5 Free Solid</FontFamily>
<Style TargetType="{x:Type Control}" x:Key="BaseStyle">
<Setter Property="FontFamily" Value="{StaticResource ArconRegular}"/>
</Style>
<Style TargetType="{x:Type TextBlock}" x:Key="BaseTextBlockStyle">
<Setter Property="FontFamily" Value="{StaticResource ArconRegular}"/>
</Style>
<Style TargetType="{x:Type Button}" BasedOn="{StaticResource BaseStyle}"/>
<Style TargetType="{x:Type Label}" BasedOn="{StaticResource BaseStyle}"/>
<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource BaseStyle}"/>
<Style TargetType="{x:Type TextBlock}" BasedOn="{StaticResource BaseTextBlockStyle}"/>
<Style TargetType="{x:Type ListView}" BasedOn="{StaticResource BaseStyle}"/>
<system:String x:Key="FAMinimizeIcon"></system:String>
<system:String x:Key="FAMaximizeIcon"></system:String>
<system:String x:Key="FACloseIcon"></system:String>
Buttons XAML(我想在按钮上使用图标)
<Style TargetType="{x:Type Button}" x:Key="WindowControlButton" BasedOn="{StaticResource BaseStyle}">
<Setter Property="FontFamily" Value="{StaticResource FASolid}"/>
<Setter Property="WindowChrome.IsHitTestVisibleInChrome" Value="True"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="Padding" Value="3"/>
<Setter Property="Foreground" Value="{StaticResource ForegroundMainBrush}"/>
<Setter Property="LayoutTransform">
<Setter.Value>
<ScaleTransform ScaleX="1.5"/>
</Setter.Value>
</Setter>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Border Padding="{TemplateBinding Padding}" Background="{TemplateBinding Background}">
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Text="{TemplateBinding Content}"/>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background" Value="{StaticResource BackgroundLightBrush}"/>
</Trigger>
</Style.Triggers>
</Style>
最后是 MainWindow XAML(我想使用它们的一部分)
<!-- Window Buttons -->
<StackPanel Grid.Column="2" Orientation="Horizontal">
<Button Style="{StaticResource WindowControlButton}" Content="{StaticResource FAMinimizeIcon}" Command="{Binding MinimizeCommand}"/>
<Button Style="{StaticResource WindowControlButton}" Content="" Command="{Binding MaximizeCommand}"/>
<Button Style="{StaticResource WindowCloseButton}" Content="" Command="{Binding CloseCommand}"/>
</StackPanel>
如您所见,我尝试在单独的字符串中指定图标代码并使用 StaticResource 但没有任何效果。
解决方案
就我而言,它FontFamily="{TemplateBinding FontFamily}"
在ControlTemplate
推荐阅读
- android - 带有过滤器功能的 RecyclerView 分页
- android - 用于离线使用的较新版本的“Android SDK 文档”
- javascript - JavaScript 类 - 使用 WeakMap 将变量设为私有并在其他方法中仍然使用“this”
- python - 我想问一下通过python爬网的问题
- sass - Sass:函数按屏幕返回值
- c# - 如何在没有异步调用的情况下显示 Rad Busy 指示器
- javascript - React.js 使用非状态变量时出现错误作为组件正在更改要控制的文本类型的不受控制的输入
- android - 如何在android中使用布尔标志隐藏另一个活动的组件?
- jenkins-plugins - 安装时出错:没有这样的插件:cloudbees-folder
- wcf - 将 Swagger JSON 转换为 WCF 合同