WPF应用程序中的自定义字体只能在设计模式下工作,而不能在构建中使用 [英] Custom font in WPF application only works in design mode but not in the build

查看:158
本文介绍了WPF应用程序中的自定义字体只能在设计模式下工作,而不能在构建中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我在我的WPF应用程序中使用自定义字体时遇到问题。我使用使用应用程序打包字体。它在设计模式下工作正常,但是当我运行该应用程序,它不起作用,似乎使用默认字体。找到原因:


  • 我从网上下载了一个免费的字体,并用相同的方式来引用它。它在两种情况下工作得非常好。

  • 我创建了一个新的WPF项目,并在这个新项目中添加了自定义字体。它出乎意料地工作。我比较了新项目和我的问题项目,不同之处在于我在旧项目中使用.Net 3.5,在新项目中使用.Net 4.0。在我将旧项目更改为使用.Net 4.0之后,问题就解决了。但是.Net 4.0在我的旧项目中是不允许的,所以我想找到一个基于.Net 3.5的解决方案。顺便说一句,
  • 我试图建立作为内容和资源的字体文件。此外,我试图使用绝对和相关的路径。它没有工作。




期待您的帮助。非常感谢。



问候,
Yupeng

解决方案

我发现名称包含中/光或其他字体关键字的字体系列将具有上述问题。当我使用代码加载字体文件时,加载的字体系列名称与字体文件中显示的名称不同。所以我假设加载时会自动删除字体名称中的关键字。

为了解决这个问题,我重新编写了名称(由代码加载),并用它们来引用我的自定义字体系列。它在构建中运行良好,但是现在在设计模式下工作。评论/取消注释将有助于切换。



我认为最简单的方法应该是通过删除关键字来更改字体系列名称,但是我没有权限而我没有尝试。

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

解决方案

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应用程序中的自定义字体只能在设计模式下工作,而不能在构建中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆