wpfxamlfontsresourcedictionarymergeddictionaries

XAML - Using fonts from resource dictionary from another assembly


I am building an application that uses ResourceDictionaries from another assembilies and I'm having problems with using fonts.

There is an assembly named MyFontAssembly that stores fonts along with references to them as an ResourceDictionary. The structure of it looks as follows:

MyFontAssembly
    - FontDictionary.xaml - (stores references to fonts)
    - Fonts
        - FontA.ttf
        - FontB.ttf
          ...

There is also another assembly that stores ResourceDictionaries for styling controls and it's called MyStylesAssembly. ResourceDictionaries from MyStylesAssembly are then merged in an App.xaml of an application in order to provide reusable styles.

The problem is that my styles does recognise font resources (the code is not crashing because it couldn't find resource by its key), but it looks like fonts stored as ttf files were not applied.

I have tried the following in my FontDictionary.xaml, but none of it works:

<FontFamily x:Key="MyFontKey">Fonts/#MyFontName</FontFamily>
<FontFamily x:Key="MyFontKey">pack://application:,,,/MyFontAssemblyName;Component/Fonts/#MyFontName</FontFamily>
<FontFamily x:Key="MyFontKey">/MyFontAssemblyName;Component/Fonts/#MyFontName</FontFamily>
<FontFamily x:Key="MyFontKey">pack://application:,,,/Fonts/#MyFontName</FontFamily>

NOTE:


Solution

  • Found an answer here. I had to set build action to resource, then reference them by using:

    <FontFamily x:Key="MyFontKey">pack://application:,,,/MyFontAssemblyName;Component/Fonts/#MyFontName</FontFamily>
    

    or the shorter version:

    <FontFamily x:Key="MyFontKey">/MyFontAssemblyName;Component/Fonts/#MyFontName</FontFamily>