VS2019 WPF .Net Core 3.1不显示资源字体

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

我已经搜索了几乎整个Internet来找到我的问题的答案,但不幸的是没有找到它。我正在使用WPF .Net Core 3.1应用程序,我想在其中使用自定义字体。目前,我正在使用Visual Studio 2019社区16.5.5

我在项目的.ttf文件夹中有很多/Fonts文件。

它们的属性设置正确:Build Action: ResourceCopy to Output Directory: Do not copy

然后我创建了/Styles/Fonts.xaml文件并将此代码放入其中:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:GenoSoft.Word">

    <FontFamily x:Key="LatoThin">pack://application;,,,/Fonts/#Lato Thin</FontFamily>

</ResourceDictionary>

App.xaml中,我添加了合并词典:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Styles/Fonts.xaml" />
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

MainWindow.xaml中,我正在按以下方式使用此字体:

<TextBlock Text="Welcome, Friend!" FontFamily="{StaticResource LatoThin}"/>

但是设计人员不会更改TextBlock的设计时外观。当我构建并运行该应用程序时,字体在运行时正确显示,但在设计器中无法正确显示。

  • 在.Net Framework 4.7.2中做的完全相同的事情使设计者显示此字体,
  • 在我的C:\Windows\Fonts文件夹中安装此字体不会使StaticResource字体出现在设计器中,但设置FontFamily="Lato Thin"效果很好,但这不是可行的方法,
  • 将字体用作Content也不起作用

我将不胜感激。

c# wpf xaml visual-studio-2019 .net-core-3.1
1个回答
0
投票

所以...几天后,我设法解决了这个问题。如我所见,.Net Core在设计时还不支持程序包字体。

支持系统安装的字体。因此,我们需要在操作系统中安装字体,然后使用以下语法:

<FontFamily x:Key="LatoRegular">Lato Regular, pack://application;,,,/Fonts/#Lato Regular</FontFamily>

这意味着:“使用系统字体库中的字体Lato Regular(如果存在,否则使用资源字体”。

现在我们可以在设计器中看到所需的自定义字体。


注意:当您将其投入生产时,我不知道它如何影响性能,而用户很可能不会安装此字体。我个人发现在这种环境下没有延迟/性能问题。

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