Giacomo
Giacomo

Reputation: 11

ThemeResources not resolved in Windows Phone XAML

Here is my problem:

In my application for WP 8.1, I have to make a button with an image, and this image have to change according to the theme used in the device,otherway it will be hidden by the background.

So I've tryed using ThemeResource. But it can't be resolved.

Here is the code:

MainPage.xaml

<Page.Resources>
    <ResourceDictionary>  
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="ButtonImage.xaml"/>
        </ResourceDictionary.MergedDictionaries>

....

<Button HorizontalAlignment="Left"
        VerticalAlignment="Top" 
        Width="40" Height="40"
        MinWidth="0" MinHeight="0"
        Padding="0"
        BorderThickness="1.5"
        Grid.Column="2"
        Style="{ThemeResource Button123}"/>

ButtonImage.xaml

<Style x:Key="ButtonImage" TargetType="Button">
...
</Style>

<ResourceDictionary.ThemeDictionaries>
    <ResourceDictionary x:Name="Black" x:Key="Black">
        <Style x:Key="ButtonAZ" BasedOn="{StaticResource ButtonImage}" TargetType="Button">
            <Setter Property="Content">
                <Setter.Value>
                    <Image Source="Assets/AtoZWhite.png"></Image>
                </Setter.Value>
            </Setter>
        </Style>
        <Style x:Key="Button123" BasedOn="{StaticResource ButtonImage}" TargetType="Button">
            <Setter Property="Content">
                <Setter.Value>
                    <Image Source="Assets/123White.png"></Image>
                </Setter.Value>
            </Setter>
        </Style>
    </ResourceDictionary>
    <ResourceDictionary x:Name="White" x:Key="White">
        <Style x:Key="ButtonAZ" BasedOn="{StaticResource ButtonImage}" TargetType="Button">
            <Setter Property="Content">
                <Setter.Value>
                    <Image Source="Assets/AtoZBlack.png"></Image>
                </Setter.Value>
            </Setter>
        </Style>
        <Style x:Key="Button123" BasedOn="{StaticResource ButtonImage}" TargetType="Button">
            <Setter Property="Content">
                <Setter.Value>
                    <Image Source="Assets/123Black.png"></Image>
                </Setter.Value>
            </Setter>
        </Style>
    </ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>

Can some one tell me why is impossibile to use the themeResources??

Upvotes: 1

Views: 247

Answers (2)

Łukasz Rejman
Łukasz Rejman

Reputation: 1892

What kind of image is it? Is it just a black & white icon in Windows Phone Modern style? If so, you can use it in BitmapIcon and Windows Phone will color it for you. Here's my example with 64x64 image.

<Button>
    <BitmapIcon UriSource="Assets/Smile.png"
                Width="64"
                Height="64"/>
</Button>

This is the image:
enter image description here

But in the app it will look like this:
enter image description here
or this
enter image description here
...depending on your phone settings.

Upvotes: 1

Peter Torr
Peter Torr

Reputation: 12019

According to MSDN, the appropriate keys to use for you dictionaries are Light and Dark, not Black and White. See the documentation.

Upvotes: 0

Related Questions