WP7,WP8如何设置多个ResourceDictionaries以使用自定义FontFamilies

时间:2013-04-05 08:06:41

标签: windows-phone-7 windows-phone-8 windows-phone-7.1 windows-phone windows-phone-7.1.1

我想为某些控件设置自定义字体,因此如果我在唯一的 one ResourceDictionary中设置字体并在样式中使用它,那么的所有内容都可以正常工作。但这种方法对我来说并不合适,因为我需要在单独的字典中使用字体声明,然后使用它们的样式。

App.xaml我制作了几个资源词典

<Application ...> 
   <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/ResourceAgencyDictionary.xaml"/>
                <ResourceDictionary Source="Resources/ResourceCommonDictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources> 
   ....
</Application>

ResourceAgencyDictionary.xaml我有MyFontFamilyNormal声明

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"       
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <FontFamily x:Key="MyFontFamilyNormal">/GBAgencyCommon;component/Fonts/Fonts.zip#Gotham Book</FontFamily>
    .....    
</ResourceDictionary>

ResourceCommonDictionary.xaml我想使用该字体系列(MyFontFamilyNormal):

 <ResourceDictionary ...>
        <Style x:Key="MyTextBlockValueStyle" BasedOn="{StaticResource PhoneTextBlockBase}" TargetType="TextBlock">
            <Setter Property="FontFamily" Value="{StaticResource MyFontFamilyNormal}"/>
            ....
        </Style>
 </ResourceDictionary>

项目编译但我收到运行时错误

发生了System.Windows.Markup.XamlParseException   _HResult = -2146233087   _message = 无法找到名称/密钥MyFontFamilyNormal的资源

有谁知道如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

对不起,伙计们,在互联网上搜索了很多解决方案并找到了它。 解决方案很简单(我已尝试过,但文件路径错误,并没有注意到它是正确的方向)

所以在App.xaml

<Application ...> 
   <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/ResourceCommonDictionary.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources> 
   ....
</Application>

ResourceAgencyDictionary.xaml是相同的

ResourceCommonDictionary.xaml

中的

 <ResourceDictionary ...>

        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="ResourceAgencyDictionary.xaml"/>
        </ResourceDictionary.MergedDictionaries>

        <Style x:Key="MyTextBlockValueStyle" BasedOn="{StaticResource PhoneTextBlockBase}" TargetType="TextBlock">
            <Setter Property="FontFamily" Value="{StaticResource MyFontFamilyNormal}"/>
            ....
        </Style>
 </ResourceDictionary>

注意:Source="ResourceAgencyDictionary.xaml"因为此文件与ResourceCommonDictionary.xaml

位于同一文件夹中