user979033
user979033

Reputation: 6480

WPF load font from Resources

So after quick search i found this solution:

I have inside my Resources folder fond called Abstract.

<Window.Resources>
   <Style x:Key="Abstract">
       <Setter Property="Label.FontFamily" Value="Resources/#Abstract" />
   </Style>
</Window.Resources>

And my Label:

<Label Name="lblValue"
       Content="Value"                                
       Style="{DynamicResource Abstract}"
       FontSize="14"/>

And nothing happening, i just cannot see the font i want.

Upvotes: 0

Views: 1625

Answers (1)

Andy
Andy

Reputation: 12276

There's a memory leak you need to be aware of.

Any font reference which uses a relative path will cause a memory leak. The way to do this is to use an absolute path. And yes, that is a nuisance.

See this: WPF TextBlock memory leak when using Font

I've recently been working on something which uses fancy fonts and investigated the issue. It's still there with .net 4.7. I wouldn't use a temporary folder, deliver your ttf to the same folder as your exe or into local appdata if you have several apps which will use the same ttf.

My plan, when I get round to this specific aspect of our app, is to write a custom markup extension which will allow me to pass a short name and go find the absolute path on the user's machine. I'll be using appdata.

Your immediate problem is because you're not using a relative path. Put a / in front of your path there.

Value="/Resources/#Abstract"

But you will then have a memory leak.

Upvotes: 1

Related Questions