如何根据当前主题WP7交换图像

时间:2011-08-25 22:41:06

标签: windows-phone-7 themes

我的认证失败,因为我的图像都是白色的。因此,当用户切换到灯光主题时,它会失败,因为您无法看到它。如何根据应用的主题交换此图像?

提前谢谢

3 个答案:

答案 0 :(得分:17)

问题Making an image control invert its colors depending on theme只有一个简单的XAML答案:

<Image HorizontalAlignment="Center"Stretch="None" Visibility="{StaticResource PhoneLightThemeVisibility}" Source="/MyApplication;component/imageDarkTheme.png" />
<Image HorizontalAlignment="Center" Stretch="None" Visibility="{StaticResource PhoneDarkThemeVisibility}"Source="/MyApplication;component/imageLightTheme.png" />

答案 1 :(得分:5)

在XAML中

<Image Source="{Binding ImageSource}" />

在包含属性ImageSource

的视图模型中
public string ImageSource
{
  get
  {
    if( (Visibility)App.Current.Resources["PhoneDarkThemeVisibility"] 
      == Visibility.Visible ) {
        return "/path/to/dark/image.png";

    } else {
        return "/path/to/light/image.png";

    }
  }
  private set {}
}

如果用户墓碑化您的应用,更改主题并切换回应用,则可能无法更改图片。

处理该场景的一种方法是在App类构造函数中缓存当前主题设置,然后将其与App.Application_Activated中的当前设置进行比较,如果它们不同,则需要以某种方式指示上面的视图模型需要触发ImageSource的属性更改通知。

答案 2 :(得分:0)

对于任何关注我上述评论的人 - 我必须直接从ImageBrush切换到Image(xaml)

<Button Tag="{Binding}" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="-10,20,0,0" BorderThickness="0" Width="105" Height="102" Click="ShowKioskOnMap_Click">
                                    <Image Source="../images/arrow.png" Width="55" Height="53" ImageOpened="Image_ImageOpened"/>
                                </Button>

接下来在我正在使用的页面的构造函数中 - 我做了主题查找,以防止应用程序状态恢复时出现任何问题(例如,第一次加载视图)

        InitializeComponent();

        theme = ""; //field level var (could make it dark by default if needed)
        if ((Visibility)App.Current.Resources["PhoneDarkThemeVisibility"] == Visibility.Visible)
        {
            theme = "dark";
        }
        else
        {
            theme = "light";
        }

我必须在我的公开活动中实现以下内容,以便根据主题进行切换

    private void Image_ImageOpened(object sender, RoutedEventArgs e)
    {
        var brush = (sender as Image);
        if (brush.Stretch == Stretch.Uniform)
        {
            if (theme == "light")
                brush.Source = new BitmapImage(new Uri("../images/arrowLight.png", UriKind.Relative));

            brush.Stretch = Stretch.Fill;
        }
    }