0

我想为某些控件设置自定义字体,所以如果我在唯一的一个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 的资源

有谁知道我该如何解决?

4

1 回答 1

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

于 2014-06-17T23:37:13.147 回答