Search code examples
windowswindows-phone-8

How to use ResourceDictionary as StaticResource in Windows Phone 8 (element not fount)


I am creating a Windows Phone 8 app. In the app I have a ResourceDictionary defined in a XAML file as:

     <Style TargetType="TextBlock">
        <Setter Property="Foreground" Value="YellowGreen" />
        <Setter Property="FontSize" Value="35" />
        <Setter Property="TextWrapping" Value="Wrap"/>
    </Style>

    <Style TargetType="TextBox" x:Key="CommonSetters">
        <Setter Property="Width" Value="450"/>
        <Setter Property="FontSize" Value="35" />
        <Setter Property="Foreground" Value="YellowGreen" />
        <Setter Property="Height" Value="100"/>
        <Setter Property="Background" Value="Red">
            <!--<Setter.Value>
                <ImageBrush ImageSource="logo.png" Opacity="0.1"/>
            </Setter.Value>-->
        </Setter>
    </Style>

</ResourceDictionary>

This ResourceDictionary is referenced in App.xaml as:

xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone">

    <!--Application Resources-->
    <Application.Resources>
        <local:LocalizedStrings xmlns:local="clr-namespace:Work_Force" x:Key="LocalizedStrings"/>
        <ResourceDictionary x:Key="myDict">
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resource.xaml"/>
            </ResourceDictionary.MergedDictionaries>

        </ResourceDictionary>
        <Style TargetType="TextBlock" x:Key="NN">
            <Setter Property="Width" Value="450"/>
            <Setter Property="FontSize" Value="35" />
            <Setter Property="Foreground" Value="YellowGreen" />
            <Setter Property="Height" Value="100"/>
        </Style>
    </Application.Resources>

    <Application.ApplicationLifetimeObjects>
        <!--Required object that handles lifetime events for the application-->
        <shell:PhoneApplicationService
            Launching="Application_Launching" Closing="Application_Closing"
            Activated="Application_Activated" Deactivated="Application_Deactivated"/>
    </Application.ApplicationLifetimeObjects>

</Application>

And then the easy part of doing:

Static Resources NN work fine but commonSetters is not working it declared in resourse.xaml.


Solution

  • The way you defined your resources in App.xaml, your 'myDict' resource dictionary is a nested dictionary within Application.Resources default dictionary. I don't know of any syntax how you can reference resources in nested dictionaries in XAML (as I didn't need this ever yet; I think FindResource(...) from code behind can accomplish this). A way to fix your problem is, that you modify the Application.Resources' default dictionary directly to merge with your 'Resources' dictionary. You can do so by modifying the Application.Resources section:

    <Application.Resources>
        <ResourceDictionary>
            <local:LocalizedStrings xmlns:local="clr-namespace:PhoneApp1" x:Key="LocalizedStrings"/>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources.xaml" />
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
    

    Note that now you don't have a ReosurceDictionary called 'myDict' anymore. Your 'Resources' dictionary will be directly merged into the default dictionary, that has no key. Then the resource will be resolved correctly.