C# 根据应用程序主题显示图像(暗/亮主题)

C# 根据应用程序主题显示图像(暗/亮主题),c#,uwp,uwp-xaml,C#,Uwp,Uwp Xaml,我正在开发一个UWP应用程序,我正在使用template10。我有两张照片,一张是白色的,另一张是黑色的。我想在灯光主题中显示黑色图像,在黑暗主题中显示白色图像。我有以下代码: if (this.RequestedTheme == ElementTheme.Light) Image.Source = new BitmapImage(new Uri("ms-appx:///Assets/BlackImage.png")); else Image.Source = new Bitma

我正在开发一个UWP应用程序,我正在使用template10。我有两张照片,一张是白色的,另一张是黑色的。我想在灯光主题中显示黑色图像,在黑暗主题中显示白色图像。我有以下代码:

if (this.RequestedTheme == ElementTheme.Light)
    Image.Source = new BitmapImage(new Uri("ms-appx:///Assets/BlackImage.png"));
else
    Image.Source = new BitmapImage(new Uri("ms-appx:///Assets/WhiteImage.png"));
但是,当我选择灯光主题时,图像不会出现!但是当我选择深色主题时,会出现白色图像。

如果我们不将or
ElementTheme.dark设置为,它将始终返回
ElementTheme.Default
。因此,无论
applicationteme
是否为
Light
,您的图像都将设置为白色图像

可以获取或设置一个值的应用程序内,该值确定应用程序整体主题的明暗首选项。它返回枚举的
applicationteme
。它包括
。我们应该能够使用
App.Current.RequestedTheme
获取应用程序的当前版本

例如:

var AppRequestedTheme = App.Current.RequestedTheme.ToString();
if (AppRequestedTheme == "Light")
    Image.Source = new BitmapImage(new Uri("ms-appx:///Assets/BlackImage.png"));
else
    Image.Source = new BitmapImage(new Uri("ms-appx:///Assets/WhiteImage.png"));

这段代码确实有效,因为我已经在我的应用程序中使用了它。此外,如果您对此代码有任何问题,请在回答中对其进行注释。不要创建新帖子来说明答案不起作用。请确保资产实际位于您预期的位置,您确认黑色图像在那里了吗?这是什么?它是应用程序吗?或
页面
?您是否也在
初始化中使用此选项?或
按钮
单击?