I am building a wpf application and I wanted to use Open Sans Regular font in my application.
I referred this link for embedding font. I added OpenSans Regular.ttf file to resources under project properties.
Then I referred them in my application as mentioned below:
<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 designer of Visual Studio I can see font changed to open sans but when I run the application it is taking system default font (Arial in my system).
Do let me know if you need any other information.
From MSDN Page:
When you add fonts as resources to your application, make sure you are setting the
<Resource>
element, and not the element in your application's project file. The<EmbeddedResource>
element for the build action is not supported.
So, I guess, you will have to add this font file to your project as you would add any other file and set its BuildAction
to Resource
instead of adding the font to the Resources under Project Properties, which will make it as an EmbeddedResource.
EDIT
Read this excellent article on how to use custom fonts in a WPF application.
You could also, get this working by way @Sheridan mentioned - which is setting BuildAction
to Content
. However, the problem with this approach is you will have loose separate file hanging around along with your binary. Users can potentially change this file, which may cause problems with your app. It is best to use Resource
as BuildAction
as the font gets bundled into the assembly.
In my opinion, I believe that the accepted answer to this question may either be incorrect, or misleading. I am using Font
files in exactly the same way and I have absolutely no need to set their BuildAction
to Resource
. My Font
files have a BuldAction
set to Content
and that works just fine. I'm guessing that the accepted answer would only help users that have set their Font
file to EmbeddedResource
.
The comment that @sthotakura quoted from the linked MSDN page is merely talking about manually editing the project file, which the question author is not doing. Note that there is no mention of BuildAction
property in the linked page apart from mentioning that the EmbeddedResource
value is invalid in this case.
Please try this instead or as well:
Set the BuildAction
of the Font
file(s) to Content
.
Reference the Font
file like this (with the all important starting slash):
FontFamily="/Resources/#Open Sans"
I just tried removing the starting slash in my project and the Font
defaulted to another Font
, so I'm pretty sure that this will work... all the same, please let me know. I'm more than happy to remove this answer if I am mistaken.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With