rad
rad

Reputation: 1937

Add font family in resourceDictionary

I am using the msdn tutorial to add a FontFamily in my Wpf application, in .csproj I have :

  <ItemGroup>
    <Resource Include="Resources\MetaOT-Norm.otf" />
    <Resource Include="Resources\MetaOT-Bold.otf" />
  </ItemGroup>

I added the fontfamily in the ResourceDictionary, like that :

<FontFamily x:Key="FontMetaOT">./Resources/#Meta OT</FontFamily>

But it's not applied... (I have tried with the Font files in the windows Fonts directory and it's work well). Any idea ?

Upvotes: 4

Views: 6696

Answers (1)

Arash
Arash

Reputation: 156

If you are using a resource dictionary file, you have to address the files using the Pack URI Scheme. For instance:

The following example shows the pack URI for a XAML resource file that is located in the root of the referenced assembly's project folder.

pack://application:,,,/ReferencedAssembly;component/ResourceFile.xaml

The following example shows the pack URI for a XAML resource file that is located in a subfolder of the referenced assembly's project folder.

pack://application:,,,/ReferencedAssembly;component/Subfolder/ResourceFile.xaml

The following example shows the pack URI for a XAML resource file that is located in the root folder of a referenced, version-specific assembly's project folder.

pack://application:,,,/ReferencedAssembly;v1.0.0.1;component/ResourceFile.xaml

If the file is located in the output folder, you can use site of origin to refer to it:

The following example shows the pack URI for a XAML site of origin file, stored in the location from which the executable assembly is launched.

pack://siteoforigin:,,,/SiteOfOriginFile.xaml

The following example shows the pack URI for a XAML site of origin file, stored in subfolder that is relative to the location from which the application's executable assembly is launched.

pack://siteoforigin:,,,/Subfolder/SiteOfOriginFile.xaml

As an example:

    <UserControl.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <!--A resource dictionary in the output folder in the Assets folder-->
            <ResourceDictionary Source="pack://siteoforigin:,,,/Assets/OpenIconsDictionary.xaml"/>
            <!--A resource dictionary packed in the Gui dll-->
            <ResourceDictionary Source="pack://application:,,,/Gui;component/Assets/PackedIconsDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>
      </ResourceDictionary>
</UserControl.Resources>



<!--In the output folder /Assets/OpenIconsDictionary.xaml (Build Action: Embedded Resource, Copy always)-->
<ResourceDictionary
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <BitmapImage x:Key="Icon"                 
                 UriSource="pack://siteoforigin:,,,/Images/image.png"/>


</ResourceDictionary>


<!--In Gui.dll in the folder /Assets/PackedIconsDictionary.xaml (Build Action: Page, Do not copy)-->
<ResourceDictionary
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <BitmapImage x:Key="Icon"                 
                 UriSource="pack://siteoforigin:,,,/Images/image.png"/>


</ResourceDictionary>

Upvotes: 3

Related Questions