Kacper Stachowski
Kacper Stachowski

Reputation: 975

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:

Upvotes: 6

Views: 9567

Answers (3)

Kacper Stachowski
Kacper Stachowski

Reputation: 975

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>

Upvotes: 9

Redouane
Redouane

Reputation: 430

Here is one way to reference a font from deferent library. Say we have library called MyAwesomeFonts and here is its project structure:

MyAwesomeFontsLibrary
 |
 |__ Fonts\
 |      |
 |      |__ Fonts.cs
 |      |__ MyAwesomeFont.ttf
 |
 |__ Themes\
        |
        |__ generic.xaml

In the Fonts\ folder you put your fonts, and in the generic.xaml file you declare your font normally like this:

xmlns:fonts="clr-namespace:MyAwesomeFontsLibrary.Fonts"...
<FontFamily 
     x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type fonts:Fonts}, ResourceId=MyAwesomeFont}">Fonts/#My Awesome Font Name</FontFamily>

and in the Fonts.cs file contains:

// say this class declared in namespace: MyAwesomeLibrary.Fonts
public class Fonts
{
    public static ComponentResourceKey MyAwesomeFontKey => new ComponentResourceKey(typeof(Fonts), "MyAwesomeFont");
}

For every font you have you do the same things done above, you add them to Fonts folder, declare them as resource in generic.xaml file and finally create a static property in Fonts.cs

Now build your library, add reference to it in project where you want to use it and add a namespace of your AwesomeFontsLibrary to your XAML something like that:

<... xmlns:fonts="clr-namespace:MyAwesomeFontsLibrary.Fonts;assembly=MyAwesomeFontsLibrary" >

Now here is two snippets of reusing your AwesomeFontsLibrary Fonts in TextBlock and style:

In Style:

<Style x:Key="MyTextBlockStyle"
           TargetType="TextBlock">
        <Setter Property="FontFamily" Value="{DynamicResource {x:Static fonts:Fonts.MyAwesomeFontKey}}"></Setter>
    </Style>

Or directly in Textblock:

    <TextBlock FontFamily="{DynamicResource {x:Static fonts:Fonts.MyAwesomeFontKey}}" FontSize="50">Welcome!</TextBlock>

You must use Dynamic Resource, not static resource, when using a ComponentResourceKey

Your new font my not appear in designer window but in runtime it will work.

Steps above are tested on my machine and they work. Hopefully this will help.

Upvotes: 0

Ramankingdom
Ramankingdom

Reputation: 1496

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>

Upvotes: 3

Related Questions