C# 标准方式为Windows Phone动态设置基于主题的图像

C# 标准方式为Windows Phone动态设置基于主题的图像,c#,silverlight,windows-phone-7,windows-phone-7.1,C#,Silverlight,Windows Phone 7,Windows Phone 7.1,我目前正在MainPage.xaml.cs中为主题设置图像 public MainPage() { InitializeComponent(); setThemeIcons(); } private void setThemedIcons() { Uri u; if ((Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"] == Visibility.Visible) {

我目前正在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);
}
对我来说,这似乎是一个低劣的编码。主要原因是我必须为应用程序中的每个主题敏感的图像执行此操作


理想的方法是直接在XAML中绑定图像。如何使其具有主题意识?

准备一个ValueConverter,它将向文件路径添加“暗”或“亮”后缀,并在绑定图像源属性时使用它


有关
IValueConverter
接口和在XAML中使用转换器的更多信息,请访问互联网。

查看主题图像转换器,您可以通过以下方式使用它:

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

我自己有点明白了,但是一个示例代码对我来说真的很有帮助。。至少是xaml部分。