WP7, WP8 How to set several ResourceDictionaries to use custom FontFamilies

喜你入骨 提交于 2019-12-12 01:57:24

问题


I want to set custom fonts for some controls, so if I set a font inside the only one ResourceDictionary and use it in styles then everything works just fine. But this approach is not fine for me, because I need to have font declarations in a separate dictionary then styles that are using them.

In App.xaml I have made several Resource Dictionaries

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

In ResourceAgencyDictionary.xaml I have MyFontFamilyNormal declaration

<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>

In ResourceCommonDictionary.xaml I want to use that font family (MyFontFamilyNormal):

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

The project compiles but I get a runtime error

System.Windows.Markup.XamlParseException occurred _HResult=-2146233087 _message=Cannot find a Resource with the Name/Key MyFontFamilyNormal

Does anyone know how can I fix that?


回答1:


Sorry guys, searched the solution a lot in Internet and just found it. The solution was simple (I've tried it but with wrong path to file and didn't notice that it was the right direction)

so in App.xaml

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

the ResourceAgencyDictionary.xaml is the same

in 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>

NOTE: Source="ResourceAgencyDictionary.xaml" because this file is in the same folder as ResourceCommonDictionary.xaml



来源:https://stackoverflow.com/questions/15828872/wp7-wp8-how-to-set-several-resourcedictionaries-to-use-custom-fontfamilies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!