Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • I am sure that my fonts form ttf work and are named correctly. I was using <FontFamily x:Key="MyFontKey">Fonts/#MyFontName</FontFamily> implementation in the single exe project with the same structure and everything was just fine, the problem appeared when I have split the implementation into few assemblies, just like I described, during refactoring.
  • MyFontAssembly is merged in MyStylesAssembly correctly. I just call it by that name here, in real code it has also a few more ResourceDictionaries that are used correctly by MyStylesAssembly. The problem appears to be with just <FontFamily> tags not recognising ttf files.
  • MyFontAssembly and MyStylesAssembly are projects of tyle ClassLibrary and does not contain any code other than in ResourceDictionaries (no classes or code behind)
like image 768
Kacper Stachowski Avatar asked Oct 06 '17 08:10

Kacper Stachowski


2 Answers

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>
like image 136
Kacper Stachowski Avatar answered Nov 20 '22 13:11

Kacper Stachowski


Create A WPF Class Library.Lets Say FontLibraryCommon enter image description here

Now Delete App.Xaml and MainWIndow.Xaml as shown below enter image description here

Now change the project properties to class library and compile

enter image description here

Now Add Font Folder and existing TTF files under it as shown. I use Pacifico font for example enter image description here enter image description here

Now add ResourceDictioanry let say FontDictioanry.xaml in your library and your xaml should look like this

Here is the code

  <ResourceDictionary 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:local="clr-namespace:FontLibraryCommon">
   <FontFamily x:Key="Pacifico">Fonts/#Pacifico</FontFamily>

 </ResourceDictionary>

Now in your other project add Font Library as reference And in your MainWindow.Xaml `

<Window.Resources>
    <ResourceDictionary Source="pack://application:,,,/FontLibraryCommon;component/FontDictioanry.xaml"></ResourceDictionary>
</Window.Resources>`

And last Lets Say label you can set like this

<Label FontFamily="{StaticResource Pacifico}"> Raman</Label>
like image 3
Ramankingdom Avatar answered Nov 20 '22 12:11

Ramankingdom