简体   繁体   English

在 WPF 中,如何在后面的代码中引用资源库中的字体?

[英]In WPF, how do I reference a font in a resource library in code behind?

I have an application that uses a separate library assembly for resources (but not a resource-only assembly with no code), and I would like to include a custom font in the library.我有一个应用程序,它对资源使用单独的库程序集(但不是没有代码的仅资源程序集),我想在库中包含自定义字体。

I am able to get the font, which is an Open Type Font , to load if I add its .otf file as a resource to the project for the executing assembly (rather than to the resource library project), with properties set as Build Action = 'Resource' and Copy to Output = 'Do Not Copy', by using the following code:如果我将其 .otf 文件作为资源添加到执行程序集的项目(而不是资源库项目)中,并将属性设置为 Build Action ,则我能够获取字体,这是一个Open Type Font = 'Resource' and Copy to Output = 'Do Not Copy',使用以下代码:

FontFamily font = new FontFamily(new Uri("pack://application:,,,/"), 
                      "./Resources/#CustomFont")); // Resources is a subfolder

When I try to add the font to the resource library project, however, the font does not load.但是,当我尝试将字体添加到资源库项目时,字体未加载。 I tried using the following code to load it (also of note: I do not have much experience with pack URIs):我尝试使用以下代码加载它(同样值得注意的是:我对包 URI 没有太多经验):

FontFamily font = new FontFamily(new Uri("pack://application:,,,/MyLibrary"),
                      "./Resources/#CustomFont")); 
                      // there is a Resources subfolder in my library as well
                      // not sure about whether I need the .

The library does work for other resources, such as images.该库确实适用于其他资源,例如图像。

I've also tried a bunch of other permutations for the URI with no success (it also does not throw exceptions, just displays with the default font, not sure if this is a separate issue).我还为 URI 尝试了许多其他排列,但没有成功(它也不会抛出异常,只是使用默认字体显示,不确定这是否是一个单独的问题)。

I've been working from Packaging Fonts with Applications on MSDN, which has an example of creating a font resource library, but no examples using code behind (I am forced to use code behind for this).我一直在 MSDN 上使用Packaging Fonts with Applications工作,其中有一个创建字体资源库的示例,但没有使用隐藏代码的示例(为此我被迫使用隐藏代码)。

Any ideas about what I need to do?关于我需要做什么的任何想法? Am I off track?我跑题了吗?

I have it working in my application (loading fonts from another assembly in code-behind).我在我的应用程序中使用它(从代码隐藏中的另一个程序集加载字体)。 For a font URI like this:对于这样的字体 URI:

pack://application:,,,/MyAssembly.Name;component/Resources/Fonts/#Swis721 Md BT

The way I got it to work (after painful trial and error, if I remember correctly) is:我让它工作的方式(经过痛苦的反复试验,如果我没记错的话)是:

new FontFamily(
    new Uri("pack://application:,,,/MyAssembly.Name;component/Resources/Fonts/"),
    "./#Swis721 Md BT"
)

Hope that helps.希望有帮助。

WPF does not support creating the FontFamily object programmatically using pack notation. WPF 不支持使用包表示法以编程方式创建 FontFamily 对象。

The docs say it in the end of the page, here文档在页面的末尾说,在这里

Here is the quote:这是报价:

Absolute URI using the pack: notation: WPF applications do not allow you to create a FontFamily object programmatically using "pack:" as part of the absolute uniform resource identifier (URI) reference to a font.使用 pack: 符号的绝对 URI: WPF 应用程序不允许您使用“pack:”作为对字体的绝对统一资源标识符 (URI) 引用的一部分以编程方式创建 FontFamily 对象。 For example, "pack://application:,,,/resources/#Pericles Light" is an invalid font reference.例如,“pack://application:,,,/resources/#Pericles Light”是无效的字体引用。

(I know, old question, but I didn't find a correct answer.) (我知道,老问题,但我没有找到正确答案。)

the Ross's answer only works in some versions of the netframework .罗斯的回答仅适用于某些版本的网络框架。 (Does not work on netframework 4.6) (不适用于网络框架 4.6)

I think this is the best answer :我认为这是最好的答案:

Enumerating Fonts in an Application :枚举应用程序中的字体:

 foreach (FontFamily fontFamily in Fonts.GetFontFamilies(new Uri("pack://application:,,,/"), "./resources/"))
            {
                // Perform action.
            }

reference 参考

In order to reference font resource items from code, you must supply a two-part font resource reference: the base uniform resource identifier (URI);为了从代码中引用字体资源项,您必须提供一个由两部分组成的字体资源引用:基本统一资源标识符 (URI); and the font location reference.和字体位置参考。 These values are used as the parameters for the FontFamily method.这些值用作 FontFamily 方法的参数。 The following code example shows how to reference the application's font resources in the project subdirectory called resources.下面的代码示例显示了如何在名为 resources 的项目子目录中引用应用程序的字体资源。

// The font resource reference includes the base URI reference (application directory level),
// and a relative URI reference.

myTextBlock.FontFamily = new FontFamily(new Uri("pack://application:,,,/"), "./resources/#Pericles Light");

The base uniform resource identifier (URI) can include the application subdirectory where the font resource resides.基本统一资源标识符 (URI) 可以包括字体资源所在的应用程序子目录。 In this case, the font location reference would not need to specify a directory, but would have to include a leading "./", which indicates the font resource is in the same directory specified by the base uniform resource identifier (URI).在这种情况下,字体位置引用不需要指定目录,但必须包含一个前导“./”,这表示字体资源位于由基本统一资源标识符 (URI) 指定的同一目录中。 The following code example shows an alternate way of referencing the font resource item—it is equivalent to the previous code example.下面的代码示例显示了引用字体资源项的​​另一种方法——它等同于前面的代码示例。

// The base URI reference can include an application subdirectory.

myTextBlock.FontFamily = new FontFamily(new Uri("pack://application:,,,/resources/"), "./#Pericles Light");

Source: docs.microsoft.com来源: docs.microsoft.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM