Add a font family to resourceDictionary

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

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

I added fontfamily to the ResourceDictionary, for example:

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

But it doesn’t apply ... (I tried with Font files in the Windows Fonts directory and it works well). Any idea?

+4
source share
1 answer

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

The following example shows the package URI for the XAML resource file, which is located in the root of the specified directory of the assembly project.

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

The following example shows the package URI for the XAML resource file, which is located in a subfolder of the link folder of the assembly project.

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

The following example shows the package URI for the XAML resource file, which is located in the root folder of the referenced version of the project assembly.

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

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

The following example shows the package URI for the site of origin of the XAML file stored in the location from which the executable assembly is running.

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

The following example shows the package URI for the site of origin of the XAML file stored in a subfolder that refers to the location from which the executable assembly of the application 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> 
+2
source

Source: https://habr.com/ru/post/1401427/


All Articles