Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In WPF, how do I reference a font in a resource library in code behind?

I have an application that uses a separate library assembly for resources (but not a resource-only assembly with no code), and I would like to include a custom font in the library.

I am able to get the font, which is an Open Type Font, to load if I add its .otf file as a resource to the project for the executing assembly (rather than to the resource library project), with properties set as Build Action = 'Resource' and Copy to Output = 'Do Not Copy', by using the following code:

FontFamily font = new FontFamily(new Uri("pack://application:,,,/"), 
                      "./Resources/#CustomFont")); // Resources is a subfolder

When I try to add the font to the resource library project, however, the font does not load. I tried using the following code to load it (also of note: I do not have much experience with pack URIs):

FontFamily font = new FontFamily(new Uri("pack://application:,,,/MyLibrary"),
                      "./Resources/#CustomFont")); 
                      // there is a Resources subfolder in my library as well
                      // not sure about whether I need the .

The library does work for other resources, such as images.

I've also tried a bunch of other permutations for the URI with no success (it also does not throw exceptions, just displays with the default font, not sure if this is a separate issue).

I've been working from Packaging Fonts with Applications on MSDN, which has an example of creating a font resource library, but no examples using code behind (I am forced to use code behind for this).

Any ideas about what I need to do? Am I off track?

like image 761
axanpi Avatar asked May 09 '12 19:05

axanpi


People also ask

What is font family in WPF?

In WPF, the default font family for text displayed on controls (like Label and Button) is Segoe UI, with a default size of 12.0 device-independent units. Because a device-independent unit is 1/96 inch, a FontSize value of 12 represents characters whose capitals are 1/8″ high.


2 Answers

I have it working in my application (loading fonts from another assembly in code-behind). For a font URI like this:

pack://application:,,,/MyAssembly.Name;component/Resources/Fonts/#Swis721 Md BT

The way I got it to work (after painful trial and error, if I remember correctly) is:

new FontFamily(
    new Uri("pack://application:,,,/MyAssembly.Name;component/Resources/Fonts/"),
    "./#Swis721 Md BT"
)

Hope that helps.

like image 122
Ross Avatar answered Oct 10 '22 00:10

Ross


WPF does not support creating the FontFamily object programmatically using pack notation.

The docs say it in the end of the page, here

Here is the quote:

Absolute URI using the pack: notation: WPF applications do not allow you to create a FontFamily object programmatically using "pack:" as part of the absolute uniform resource identifier (URI) reference to a font. For example, "pack://application:,,,/resources/#Pericles Light" is an invalid font reference.

like image 32
Ninglin Avatar answered Oct 10 '22 01:10

Ninglin