0

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?

giacoder
  • 980
  • 8
  • 21
  • This is called knowledge exhibition... You got your answer right? Close the topic then. – Mani Apr 05 '13 at 10:12
  • I solved it after I've ask it and placed here. At that time there is no checkbox which was while creating "Answer your own question – share your knowledge, Q&A-style". And if I close the question now what radio button should I select? duplicate / off topic / not constructive / not a real question / too localized ?? Nothing fits – giacoder Apr 05 '13 at 11:34
  • if you can ad answer your own question now then you can close this topic. – Mubashar Mar 15 '14 at 12:40

1 Answers1

0

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

giacoder
  • 980
  • 8
  • 21