2

I've searched nearly whole Internet to find an answer for my question, but untortunately didn't find it. I'm working on the WPF .Net Core 3.1 application, where I want to use custom font. Currently I'm working in Visual Studio 2019 Community 16.5.5

I've got plenty .ttf files located in project's /Fonts folder.

Their properties are set correct: Build Action: Resource and Copy to Output Directory: Do not copy.

Then I've created /Styles/Fonts.xaml file and put this code inside:

<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>

In App.xaml I've added Merged Dictionary:

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

In MainWindow.xaml I'm using this font as following:

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

But designer doesn't change the design-time appearance of the TextBlock. When I build and run the application the font is displayed correctly in runtime, but not in designer.

  • Doing exactly same thing in .Net Framework 4.7.2 makes designer display this font,
  • Installing this font in my C:\Windows\Fonts folder doesn't make StaticResource font to appear in designer, but setting FontFamily="Lato Thin" works well, but that's not a way to go,
  • Using font as Content also doesn't work

I would be grateful for any advices.

Genotypek
  • 213
  • 3
  • 11
  • Point in [Limitations on Font Usage](https://learn.microsoft.com/en-us/dotnet/framework/wpf/advanced/packaging-fonts-with-applications#limitations-on-font-usage) you need pay attention to: Absolute URI using the pack: notation. The solution in [here](https://stackoverflow.com/questions/46601790/xaml-using-fonts-from-resource-dictionary-from-another-assembly) may give you some help. – DasiyTian_1203 May 15 '20 at 03:26

1 Answers1

1

So... After some days I've managed to workaround this problem. As I see package fonts are not supported in design time yet in .Net Core.

BUT system installed fonts are supported. So we need to install our font in the OS, and then use this syntax:

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

Which means: "Use font Lato Regular from system fonts library if exists, else use resource font".

Now we can see our desired custom-font in designer.


NOTE: I have no informations how it's affecting the performance when you leave it for production, where user most likely will not have this font installed. I've personally observed no latency/performance issues in such environment.

Genotypek
  • 213
  • 3
  • 11