XAML - 从另一个程序集的资源字典中使用字体 [英] XAML - Using fonts from resource dictionary from another assembly

查看:230
本文介绍了XAML - 从另一个程序集的资源字典中使用字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,它使用另一个集合中的 ResourceDictionaries ,并且遇到了使用字体的问题。

有一个名为 MyFontAssembly 的程序集,它将字体和对它们的引用存储为 ResourceDictionary 。它的结构看起来如下所示:

$ My $ $ My $ My $ $ b $ FontFactory.xaml - (存储对字体的引用)
- 字体
- FontA.ttf
- FontB.ttf
...

另外还有一个程序集存储 ResourceDictionaries 作为样式控件,它被称为 MyStylesAssembly 。然后,将来自MyStylesAssembly的ResourceDictionaries合并到应用程序的App.xaml中,以提供可重用的样式。



问题是我的样式识别字体资源(代码没有崩溃,因为它找不到密钥的资源) 但看起来像ttf文件存储的字体没有应用。



我在<



 < FontFamily x:Key =MyFontKey> ;字体/#MyFontName< /&的FontFamily GT; 
< FontFamily x:Key =MyFontKey> pack:// application:,,, / MyFontAssemblyName; Component / Fonts /#MyFontName< / FontFamily>
< FontFamily x:Key =MyFontKey> / MyFontAssemblyName; Component / Fonts /#MyFontName< / FontFamily>
< FontFamily x:Key =MyFontKey> pack:// application:,,, / Fonts /#MyFontName< / FontFamily>

注意:


  • 我确定我的字体构成了ttf,并且命名正确。我在单个exe项目中使用< FontFamily x:Key =MyFontKey> Fonts /#MyFontName< / FontFamily> 很好,问题出现了,当我已经将实现拆分成几个程序集,就像我在重构过程中所描述的那样。

  • MyFontAssembly 被合并在 MyStylesAssembly 中正确。我只是在这里调用它的名字,在实际的代码中,还有一些 ResourceDictionaries MyStylesAssembly 。这个问题似乎与< FontFamily> 标签不能识别ttf文件。
  • code>和 MyStylesAssembly 是tyle ClassLibrary 的项目,不包含除< $ c> ResourceDictionaries (没有类或代码)

    解决方案

    找到一个答案这里。我必须将 build action 设置为 resource ,然后使用以下参考:

     < FontFamily x:Key =MyFontKey> pack:// application:,,, / MyFontAssemblyName; Component / Fonts /#MyFontName< / FontFamily> 

    或者更短的版本:

    <$ p $ < code>< FontFamily x:Key =MyFontKey> / MyFontAssemblyName; Component / Fonts /#MyFontName< / FontFamily>


    I am building an application that uses ResourceDictionaries from another assembilies and I'm having problems with using fonts.

    There is an assembly named MyFontAssembly that stores fonts along with references to them as an ResourceDictionary. The structure of it looks as follows:

    MyFontAssembly
        - FontDictionary.xaml - (stores references to fonts)
        - Fonts
            - FontA.ttf
            - FontB.ttf
              ...
    

    There is also another assembly that stores ResourceDictionaries for styling controls and it's called MyStylesAssembly. ResourceDictionaries from MyStylesAssembly are then merged in an App.xaml of an application in order to provide reusable styles.

    The problem is that my styles does recognise font resources (the code is not crashing because it couldn't find resource by its key), but it looks like fonts stored as ttf files were not applied.

    I have tried the following in my FontDictionary.xaml, but none of it works:

    <FontFamily x:Key="MyFontKey">Fonts/#MyFontName</FontFamily>
    <FontFamily x:Key="MyFontKey">pack://application:,,,/MyFontAssemblyName;Component/Fonts/#MyFontName</FontFamily>
    <FontFamily x:Key="MyFontKey">/MyFontAssemblyName;Component/Fonts/#MyFontName</FontFamily>
    <FontFamily x:Key="MyFontKey">pack://application:,,,/Fonts/#MyFontName</FontFamily>
    

    NOTE:

    • I am sure that my fonts form ttf work and are named correctly. I was using <FontFamily x:Key="MyFontKey">Fonts/#MyFontName</FontFamily> implementation in the single exe project with the same structure and everything was just fine, the problem appeared when I have split the implementation into few assemblies, just like I described, during refactoring.
    • MyFontAssembly is merged in MyStylesAssembly correctly. I just call it by that name here, in real code it has also a few more ResourceDictionaries that are used correctly by MyStylesAssembly. The problem appears to be with just <FontFamily> tags not recognising ttf files.
    • MyFontAssembly and MyStylesAssembly are projects of tyle ClassLibrary and does not contain any code other than in ResourceDictionaries (no classes or code behind)

    解决方案

    Found an answer here. I had to set build action to resource, then reference them by using:

    <FontFamily x:Key="MyFontKey">pack://application:,,,/MyFontAssemblyName;Component/Fonts/#MyFontName</FontFamily>
    

    or the shorter version:

    <FontFamily x:Key="MyFontKey">/MyFontAssemblyName;Component/Fonts/#MyFontName</FontFamily>
    

    这篇关于XAML - 从另一个程序集的资源字典中使用字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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