WPF自定义FontFamily从程序集? [英] WPF custom FontFamily from assembly?

查看:1441
本文介绍了WPF自定义FontFamily从程序集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个为WPF应用程序定义样式和一些控件的程序集。在这个程序集中,我有一个ResourceDictionary,它指定了程序集中其他控件和样式所使用的默认字体。

 < FontFamily x:Key =DefaultFontFamily> Segoe UI< / FontFamily> 
< sys:Double x:Key =DefaultFontSize> 15< / sys:Double>

我想要做的是将DefaultFontFamily更改为链接的自定义字体(不嵌入)与组装。



我已经在项目中创建了一个资源文件夹,并在那里复制了自定义字体。我已经将字体的构建动作设置为内容和复制到输出目录为复制,如果更新。

该项目是解决方案的一部分包含使用程序集中的样式的另一个项目,因此解决方案生成时,字体会按预期复制。我似乎无法指定正确的文件夹来引用字体。



什么工作:

 < FontFamily x:Key =DefaultFontFamily> C:\ code\Solution\Project\bin\Debug\Resources\Lobster -Regular.ttf#龙虾LT; /&的FontFamily GT; 
< FontFamily x:Key =DefaultFontFamily> file:/// c:/code/Solution/Project/bin/Debug/Resources/Lobster-Regular.ttf#Lobster< / FontFamily>

这里'Project'是引用样式程序集的启动(可执行)项目的名称。 / p>

什么不行:

  < FontFamily x:Key =DefaultFontFamily> \Resources\Lobster-Regular.ttf#龙虾< / FontFamily> 
< FontFamily x:Key =DefaultFontFamily> .\Resources\Lobster-Regular.ttf#龙虾< / FontFamily>
< FontFamily x:Key =DefaultFontFamily> / Resources /#Lobster< / FontFamily>
< FontFamily x:Key =DefaultFontFamily> ./ Resources /#Lobster< / FontFamily>

我也尝试将字体的内容设置为Resource和Embedded Resource并尝试这样:

 < FontFamily x:Key =DefaultFontFamily> pack:// application:,,, /资源/ Lobster.ttf#龙虾LT; /&的FontFamily GT; 

这也行不通。 文档说我应该能够使用:

lockquote

一个字符串,指定一个包含字体的文件夹,以及一个字体名称。文件夹和字体系列名称由#字符分隔。文件夹引用可能是绝对的,也可能是相对的。例如,Custom Fonts \#My Custom Font。

我做错了什么?

$ b $把你的文件字体参数像Resource和Copy if newer放在一起


解析方案

I have an assembly that defines styles and some controls for a WPF application. In this assembly, I have a ResourceDictionary that specifies a 'default font' for use by the other controls and styles in the assembly.

<FontFamily x:Key="DefaultFontFamily">Segoe UI</FontFamily>
<sys:Double x:Key="DefaultFontSize">15</sys:Double>

What I would like to do is change the DefaultFontFamily to a custom font that is linked (not embedded) with the assembly.

I have created a 'Resources' folder in the project and copied the custom font there. I've set the font's 'Build Action' to 'Content' and 'Copy to Output Directory' to 'Copy if newer'.

The project is part of a solution containing another project that uses the styles in the assembly, so when the solution is built, the fonts are copied over as expected. I can't seem to specify the correct folder to reference the fonts though.

What works:

<FontFamily x:Key="DefaultFontFamily">C:\code\Solution\Project\bin\Debug\Resources\Lobster-Regular.ttf#Lobster</FontFamily>
<FontFamily x:Key="DefaultFontFamily">file:///c:/code/Solution/Project/bin/Debug/Resources/Lobster-Regular.ttf#Lobster</FontFamily>

Here 'Project' is the name of the startup (executable) project that references the styles assembly.

What does not work:

<FontFamily x:Key="DefaultFontFamily">\Resources\Lobster-Regular.ttf#Lobster</FontFamily>
<FontFamily x:Key="DefaultFontFamily">.\Resources\Lobster-Regular.ttf#Lobster</FontFamily>
<FontFamily x:Key="DefaultFontFamily">/Resources/#Lobster</FontFamily>
<FontFamily x:Key="DefaultFontFamily">./Resources/#Lobster</FontFamily>

I've also tried setting the content of the font to both 'Resource' and 'Embedded Resource' and tried this:

<FontFamily x:Key="DefaultFontFamily">pack://application:,,,/Resources/Lobster.ttf#Lobster</FontFamily>

This didn't work either. The documentation says I should be able to use:

A string specifying a folder containing the font, along with a font family name. The folder and font family name are delimited by a # character. The folder reference may be absolute, or relative. For example, "Custom Fonts\#My Custom Font".

What am I doing wrong?

解决方案

Put your file font parameters like "Resource" and "Copy if newer"

这篇关于WPF自定义FontFamily从程序集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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