Display image depending on application theme

2019-09-19 11:13发布

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条回答
Melony?
2楼-- · 2019-09-19 12:00

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"));
查看更多
登录 后发表回答