bragboy
bragboy

Reputation: 35572

Standard way dynamically set theme based Images for Windows Phone

I am currently setting the images for theme like this in the MainPage.xaml.cs

public MainPage()
{
    InitializeComponent();
    setThemeIcons();
}

private void setThemedIcons()
{
    Uri u;
    if ((Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"] == Visibility.Visible)
    {
        u = new Uri("/Images/img_dark.png", UriKind.Relative);
    }
    else
    {
        u = new Uri("/Images/img_light.png", UriKind.Relative);
    }
    btnSessionSearch.Source = new BitmapImage(u);
}

This seems an inferior coding to me. The main reason is I will have to do this for every image in the application that is going to be theme sensitive.

Ideal way is to bind the image directly in the XAML. How to do it so that it is theme aware ?

Upvotes: 2

Views: 451

Answers (2)

Boryana Miloshevska
Boryana Miloshevska

Reputation: 2730

Take a look at the ThemedImageConverter you can use it in this way:

<Image Stretch="None" Source="{Binding Converter={StaticResource themedImageConverter}, ConverterParameter={StaticResource PhoneBackgroundColor}}"
DataContext="/WP7SampleProject4;component/Images/{0}/appbar.feature.camera.rest.png" />

Upvotes: 3

MarcinJuraszek
MarcinJuraszek

Reputation: 125660

Prepare a ValueConverter which will add 'dark' or 'light' suffix to the file path and use it when binding the image source property.

More info about IValueConverter interface and using converters in XAML can be found in the Internet, eg. on MSDN, TheCodeproject.

Upvotes: 2

Related Questions