WPF自定义字体的问题 [英] WPF Custom fonts problem

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

问题描述

这真的很奇怪在Blend 4中,当我在设计器中看到应用程序时,自定义字体起作用,但是当我运行它时,字体消失了,并返回到arial或其他东西。这是我的XAML:

 < TextBlock Text =Text GFontFamily =/ ProjectName; component / Fonts /#Futura Lt BTFontSize =48Background =#FFC44747/> 

字体位于名为Fonts的文件夹中,位于名为Controls的文件夹中。我知道一定是Fonts文件夹与Controls文件夹的相对位置有问题,但我已经尝试了很多东西,并且不起作用。

另外,我放置的XAML标记是Blend在选择自定义字体时创建的。字体被复制为一个资源没问题(我已经检查了csprof文件,它在那里)。

任何想法?现在我已经踢了几个小时了。



谢谢。

解决方案


虽然我明白现在为时已晚,无法帮助问题作者,但我仍然希望能够帮助将来的观众解决这个问题。


这个答案中的信息来自使用应用程序打包字体页面上的MSDN:



添加字体作为内容项



您可以将字体添加到应用程序中,作为与应用程序的程序集文件分开的项目内容项目。这意味着内容项目不会作为资源嵌入到程序集中。下面的项目文件示例显示了如何定义内容项。

 < Project DefaultTargets =Buildxmlns =http:/ /schemas.microsoft.com/developer/msbuild/2003\"> 
<! - 其他项目构建设置... - >

< ItemGroup>
< Content Include =Peric.ttf/>
< Content Include =Pericl.ttf/>
< / ItemGroup>
< / Project>

为了确保应用程序可以在运行时使用字体,必须可以访问字体应用程序的部署目录。应用程序项目文件中的元素允许您在构建过程中自动将字体复制到应用程序部署目录。以下项目文件示例显示了如何将字体复制到部署目录。

 < ItemGroup> 
< Content Include =Peric.ttf>
< CopyToOutputDirectory> PreserveNewest< / CopyToOutputDirectory>
< / Content>
< Content Include =Pericl.ttf>
< CopyToOutputDirectory> PreserveNewest< / CopyToOutputDirectory>
< / Content>
< / ItemGroup>

添加字体作为资源项目



您可以将字体添加到应用程序中,作为嵌入到应用程序的程序集文件中的项目资源项目。为资源使用单独的子目录有助于组织应用程序的项目文件。下面的项目文件示例显示如何将字体定义为资源项目在一个单独的子目录中。

 < Project DefaultTargets =Build的xmlns = http://schemas.microsoft.com/developer/msbuild/2003 > 
<! - 其他项目构建设置... - >

< ItemGroup>
< Resource Include =resources\Peric.ttf/>
< Resource Include =resources\Pericl.ttf/>
< / ItemGroup>
< / Project>

将字体作为资源添加到应用程序时,请确保设置元素,而不是元素在您的应用程序的项目文件中。



以下标记示例显示了如何引用应用程序的字体资源。

 < TextBlock FontFamily =./ resources /#Pericles Light> 
爱琴海
< / TextBlock>

请参照上面的链接,了解有关从代码和其他有用信息中引用字体资源的详细信息。


This is really weird. In Blend 4, the custom font works when I see the application in the designer, but when I run it, the font is gone and it goes back to arial or something. This is my XAML:

<TextBlock Text="Text G" FontFamily="/ProjectName;component/Fonts/#Futura Lt BT" FontSize="48" Background="#FFC44747" />

The font is in a folder called "Fonts" and the control in which I'm trying the font is in a folder called "Controls". I know it must be a problem with the relative position of the "Fonts" folder to the "Controls" folder, but I've already tried a lot of stuff and it doesn't work.

Also, the XAML markup I put up there is what Blend creates when I select the custom font. The font is copied as a resource all right (I already check the csprof file and it's there).

Any ideas? This has been kicking my butt for a couple hours now.

Thanks.

解决方案

While I understand that this is far too late to help the question author, I am leaving this to help future viewers of this question.

The information in this answer comes from the Packaging Fonts with Applications page on MSDN:

Adding Fonts as Content Items

You can add fonts to your application as project content items that are separate from the application's assembly files. This means that content items are not embedded as resources within an assembly. The following project file example shows how to define content items.

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!-- Other project build settings ... -->

  <ItemGroup>
    <Content Include="Peric.ttf" />
    <Content Include="Pericl.ttf" />
  </ItemGroup>
</Project>

In order to ensure that the application can use the fonts at run time, the fonts must be accessible in the application's deployment directory. The element in the application's project file allows you to automatically copy the fonts to the application deployment directory during the build process. The following project file example shows how to copy fonts to the deployment directory.

<ItemGroup>
  <Content Include="Peric.ttf">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </Content>
  <Content Include="Pericl.ttf">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </Content>
</ItemGroup>

Adding Fonts as Resource Items

You can add fonts to your application as project resource items that are embedded within the application's assembly files. Using a separate subdirectory for resources helps to organize the application's project files. The following project file example shows how to define fonts as resource items in a separate subdirectory.

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <!-- Other project build settings ... -->

  <ItemGroup>
    <Resource Include="resources\Peric.ttf" />
    <Resource Include="resources\Pericl.ttf" />
  </ItemGroup>
</Project>

When you add fonts as resources to your application, make sure you are setting the element, and not the element in your application's project file. The element for the build action is not supported.

The following markup example shows how to reference the application's font resources.

<TextBlock FontFamily="./resources/#Pericles Light">
  Aegean Sea
</TextBlock>

Please follow the above link for details on referencing Font resources from code and other useful information.

这篇关于WPF自定义字体的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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