Windows Phone 8.1: Change background on phone theme change (Application.Current.Resources is empty)

StackOverflow https://stackoverflow.com//questions/24014795

  •  21-12-2019
  •  | 
  •  

Вопрос

I would like to detect theme change in my application so I can change the background image accordingly. Sadly the WP7 example that included

(Visibility)Application.Current.Resources["PhoneDarkThemeVisibility"] ==
Visibility.Visible

throws an exception saying:

An exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.ni.dll but was not handled in user code WinRT information:
Cannot find a resource with the given key.
Additional information: Unspecified error

On debugging it turns out that Application.Current.Resources has 0 elements.

I would be very happy if it would turn out that I messed up something, but on running the WP7 example (in a WP8.1 project) found here: How to apply theme resources for Windows Phone it resulted in the same exception as before.
Does anyone know a WP8.1 solution to my problem? :)

Additional info:
I tried using the above mentioned code in the OnNavigatedTo part, the page constructor and even tried using it in a button click, but they all yielded the same result.
Other things like the font colour actually change after changing the theme of the phone automatically.

Это было полезно?

Решение

Use following code if you are using windows phone 8.1(not windows phone 8.1 silverlight)

if (App.Current.RequestedTheme == ApplicationTheme.Dark)
       {}

hope this helps

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top