WPF-运行时未显示Font Awesome图标

问题描述 投票:0回答:1

我正在构建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">&#xf2d1;</system:String>
    <system:String x:Key="FAMaximizeIcon">&#xf2d0;</system:String>
    <system:String x:Key="FACloseIcon">&#xf00d;</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="&#xf2d0;" Command="{Binding MaximizeCommand}"/>
    <Button Style="{StaticResource WindowCloseButton}" Content="&#xf00d;" Command="{Binding CloseCommand}"/>
</StackPanel>

您可以看到,我尝试在单独的字符串中指定图标代码,并使用StaticResource,但无济于事。

c# wpf font-awesome
1个回答
0
投票

存在相同的问题,即在设计模式下显示正常,但没有运行时。对我来说是因为fontawesome没有包含在程序集中。

在Visual Studio中,将.otf文件的Build Action更改为“ Resource”。您可能想通过反编译器检查文件是否包含在程序集中。

© www.soinside.com 2019 - 2024. All rights reserved.