开发者

WPF Custom fonts problem

开发者 https://www.devze.com 2023-03-16 15:29 出处:网络
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

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.


Everywhere over the internet and in books it says that when you add a font you should set the Build Action to "Resource" (example here). And it 'worked for a while. Anyway, to fix my problem, I had to change it from "Resource" to "Content".

0

精彩评论

暂无评论...
验证码 换一张
取 消