WPF - 添加自定义字体

Joh*_*orf 15 wpf fonts embedded-resource

我正在尝试在我的应用程序中添加自定义字体作为资源.我在应用程序中有一个"CustomFont"目录,其中的所有字体都设置为"Resource"

<Window.Resources>
    <Style x:Key="Gotham-XLight">
        <Setter Property="TextElement.FontFamily" 
                Value="/CustomFonts;Component/#Gotham-XLight" />
    </Style>
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)

然后在我的TextBlock上我有这个:(在网格内)

<TextBlock x:Name="TimeTextBlock" Style="{DynamicResource Gotham-XLight}" 
           TextAlignment="Center" FontSize="25" FontWeight="Bold" 
           Foreground="White" Text="TextBlockTimer" 
           Margin="105,242.974,0,226.975" HorizontalAlignment="Left" 
           Width="221.919" />
Run Code Online (Sandbox Code Playgroud)

但是我没有像人们说的那样看到我的字体.难道我做错了什么?

jos*_*rry 19

您可能想要检查字体的名称,您需要指定字体的名称而不是文件的名称.

双击字体文件,它应显示"字体名称:",这是您要确保在您的样式中指定的.


jos*_*rry 6

试试这个

<Window.Resources>
    <Style x:Key="Gotham-XLight">
        <Setter Property="TextElement.FontFamily" Value="CustomFonts/#Gotham-XLight" />
    </Style>
</Window.Resources>
Run Code Online (Sandbox Code Playgroud)

此外,如果您不打算在运行时更改样式,{StaticResource Gotham-XLight}那么性能会更高.