Display image depending on application theme

元气小坏坏 提交于 2019-12-25 18:25:01

问题


I am develop an UWP app, and I am using Template 10. I have a black image and a white image. I want when the user choose dark theme, show the white image, and when the user choose light theme show the black image, exemple:

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

回答1:


You can get current RequestedTheme using this.RequestedTheme and then compare it with ElementTheme.Light or ElementTheme.Dark

Method 1

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"));

Method 2

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


来源:https://stackoverflow.com/questions/44599012/display-image-depending-on-application-theme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!