Question

I have a Resources.xaml file in my project that contains a resource dictionary like so:

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

    <Style x:Key="GPHeaderFontSize" TargetType="TextBlock">
        <Setter Property="FontWeight" Value="Bold" />
        <Setter Property="FontSize" Value="24" />
        <Setter Property="Text" Value="BLAHHHHH"/>
    </Style>
</ResourceDictionary>

I have included this dictionary in App.xaml like so:

<Application x:Class="GoldenPlains.App" 
    xmlns="schemas.microsoft.com/winfx/2006/xaml/presentation"; 
    xmlns:x="schemas.microsoft.com/winfx/2006/xaml"; 
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone" 
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone">
    <Application.Resources> 
        <local:LocalizedStrings xmlns:local="clr-namespace:GoldenPlains" x:Key="LocalizedStrings"/> 
        <ResourceDictionary x:Key="GPResources"> 
            <ResourceDictionary.MergedDictionaries> 
                <!-- Sometimes VS2012 complaining about path with blue line, please ignore it as path is correct --> 
                <ResourceDictionary Source="Styles/GPResources.xaml"/> 
            </ResourceDictionary.MergedDictionaries>
            <Style x:Key="GPRootOverlayBarStyle" TargetType="Image"> 
                <Setter Property="Source" Value="Assets/Images/root_brown_horizontal_bar.png"/> 
                <Setter Property="Width" Value="729"/> 
                <Setter Property="HorizontalAlignment" Value="Left"/> 
                <Setter Property="Stretch" Value="Uniform"/> 
            </style> 
    </Application.Resources>
    ...
    ...
</Application>

However when I try to reference an element in the resource dictionary from another Page.xaml file it cannot seem to resolve the resource.... eg:

I have tried using a binding like so:

<TextBlock Style="{Binding Path=LocalizedResources.MyTextBlockStyle, Source=  {StaticResource GPResources}}"/>

it does not indicate that something is wrong but nothing shows up on the UI.

A point in the right direction would be great, cheers.

Was it helpful?

Solution

Resource Dictionary definition in the App.xaml should be about like following example :

<Application.Resources>
    <ResourceDictionary>
        <local:LocalizedStrings xmlns:local="clr-namespace:GoldenPlains" x:Key="LocalizedStrings"/> 
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="Styles/GPResources.xaml"/> 
        </ResourceDictionary.MergedDictionaries>
        <!-- Other resources if you have -->
    </ResourceDictionary>
</Application.Resources>

Then, when you need to apply style defined in Resources.xaml to a UI control, simply refer to the style's key/name :

<TextBlock Style="{StaticResource GPHeaderFontSize}" />

Notes: All resources need to be inside ResourceDictionary tag, including LocalizedStrings.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top