Reputation: 35572
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
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
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