【问题标题】:Display image depending on application theme根据应用程序主题显示图像
【发布时间】:2017-11-19 19:22:49
【问题描述】:

我正在开发一个 UWP 应用,我正在使用模板 10。 我有一个黑色图像和一个白色图像。我想当用户选择深色主题时显示白色图像,当用户选择浅色主题时显示黑色图像,例如:

if(dark theme)
{
   white image;
}
else    
{
   black image;
}

【问题讨论】:

标签: c# uwp uwp-xaml template10


【解决方案1】:

您可以使用this.RequestedTheme 获取当前的RequestedTheme,然后将其与ElementTheme.LightElementTheme.Dark 进行比较

方法一

if (this.RequestedTheme == ElementTheme.Light)
    BackgroundImage.Source = new BitmapImage(new Uri("ms-appx:///Assets/BlackImage.png"));
else
    BackgroundImage.Source = new BitmapImage(new Uri("ms-appx:///Assets/WhiteImage.png"));

方法二

BackgroundImage.Source = (this.RequestedTheme == ElementTheme.Light)? new BitmapImage(new Uri("ms-appx:///Assets/BlackImage.png")): new BitmapImage(new Uri("ms-appx:///Assets/WhiteImage.png"));

【讨论】:

  • 您可能还需要考虑ElementTheme.Default
  • @Clemens 当用户使用默认主题时,我们如何识别主题?此外,默认主题可以在运行时更改。
  • 不知道,我刚刚看到,当我启动我的测试应用程序时,RequestedTheme 是 ElementTheme.Default。该应用可能应该知道它的默认主题是什么。
  • @Clemens 如果您找到任何方法,请更新我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-14
  • 1970-01-01
  • 2021-07-28
  • 1970-01-01
  • 1970-01-01
  • 2018-04-21
  • 2021-08-31
相关资源
最近更新 更多