2017-06-22 17 views
0

我開發了一個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")); 

但是,當我選擇輕的主題形象不出現!但是當我選擇黑暗主題時,會出現白色圖像。

+1

可以肯定此代碼的工作,因爲我已經在我的應用程序中使用它。另外,如果您對此代碼有任何問題,請在[this](https://stackoverflow.com/a/44600513/7331395)上對其進行評論。不要創建一個新帖子來說答案不起作用。 –

+0

確保資產實際上位於您期望的位置,您是否確認黑色圖像存在? –

+0

這是什麼?它是'應用程序'嗎?或'頁面?你是否也在'Initialization'中使用它?或'按鈕'點擊? – AVK

回答

0

如果我們不將ElementTheme.LightElementTheme.Dark設置爲FrameworkElement.RequestedTheme,它將始終返回ElementTheme.Default。因此,您的圖像將使用WhiteImage進行設置,無論ApplicationTheme是否爲Light

該應用程序中的RequestedTheme可以獲取或設置一個值,該值確定應用程序整體主題的明暗偏好。它返回枚舉的ApplicationTheme。它包括LightDark。我們應該可以使用App.Current.RequestedTheme獲取該應用的當前ApplicationTheme

例如:

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