Custom font in WPF application only works in desig

2019-07-12 14:22发布

问题:

Recently I have a problem when using custom font in my WPF application. I added a custom font in my project using methods described in Packaging Fonts with Applications. It works fine in design mode but when I run the application, it doesn't work and seems to use default font instead. To find the reason:

  • I downloaded a free font from internet, and used same way to refer it. It worked very well in both situations.

  • I created a new WPF project, and added the custom font in this new project. It unexpectedly worked. I compared the new project and my problem project, the difference is that I use .Net 3.5 in my old project, and .Net 4.0 in the new project. After I changed the old project to use .Net 4.0, the problem is resolved. However, .Net 4.0 is not allowed in my old project, so that I want to find a solution based on .Net 3.5.

  • by the way. I tried to build the font file as both content and resource. Also, I tried to use absolute and relevant path. It didn't work.

Looking forward to your help. Thanks very much.

Regards, Yupeng

回答1:

I have found that the font family whose name includes Medium/Light or other font key words will have the problem described above. When I used code to load the font files, the loaded font family names were different from that shown in the font files. So I assuming that the key words in the font name will be removed automatically when loading.

To solve the problem, I recoded the names(loaded by code) and used them to refer to my custom font family. It works well in the build but does work in the design mode now. Comment/Uncomment will be helpful to switch.

I think the easiest way should be to change the font family name by removing the key words, but I don't have permission and I didn't try it.



标签: wpf fonts