Embedded fonts work for the designer, but not for applications

I am creating a wpf application and I wanted to use the Open Sans Regular font in my application.

I referred to this link to insert the font. I added the OpenSans Regular.ttf file to the resources as part of the project properties.

I then mentioned them in my statement, as follows:

<Window x:Class="FontEmbeddingDemo.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" FontFamily="Resources/#Open Sans"> <Window.Resources> </Window.Resources> <Grid> <TextBlock Height="100" Text="This is test text." FontSize="14" FontFamily="Resources/#Open Sans"/> </Grid> </Window> 

In the Visual Studio designer, I see that the font is changed to open sans, but when I run the application, it uses the standard system font (Arial on my system).

Let me know if you need any other information.

+6
source share
2 answers

From MSDN Page:

When you add fonts as resources for your application, make sure that you are setting the <Resource> element, not the element in the application project file. The <EmbeddedResource> element for an assembly action is not supported.

So, I think you will have to add this font file to your project, since you add any other file and set its BuildAction to Resource instead of adding the font to Resources in the "Project Properties" section, which will make it as EmbeddedResource.

EDIT

Read this great article on how to use custom fonts in a WPF application.

You can also get this working, as pointed out by @Sheridan, which sets the BuildAction to Content . However, the problem with this approach is that you will have a separate separate file hanging along with your binary code. Users can potentially modify this file, which may cause problems with your application. It is better to use Resource as a BuildAction , as the font is inserted into the assembly.

+9
source

In my opinion, I believe that the accepted answer to this question can be either incorrect or misleading. I use Font files in exactly the same way, and I absolutely don't need to set their BuildAction to Resource . My Font files have BuldAction set to Content , and this works just fine. I assume that the accepted answer will only help users who installed their Font file on the EmbeddedResource .

The comment quoted by @sthotakura from the linked MSDN page simply talks about manually editing the project file, which the author of the question does not. Note that the BuildAction property is not mentioned on the linked page, except that the EmbeddedResource value is not valid in this case.

Try this instead:

Set the BuildAction the Font file to Content . Link to a Font file like this (with all the important starting slash):

 FontFamily="/Resources/#Open Sans" 

I just tried to remove the initial slash in my project, and the Font by default is a different Font , so I'm sure this will work ... anyway, please let me know. I am more than happy to delete this answer if I am mistaken.

+2
source

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


All Articles