When to programmatically check for theme changes in Windows Phone 8.1

I have a page in a Windows Phone 8.1 application where I have several components that must have three different color states. They must be red, blue, or the current foreground color of the theme.

Therefore, if my application starts using a dark theme on the phone, and then the user exits the application and changes the Light theme and then switches to my application again, I need to immediately change the components, the old foreground color.

Since the components must change between different colors (where the main color of the theme is only one of them), I cannot set their Foreground to PhoneForegroundColor in XAML .

What I did was add a Resuming event Resuming that does this:

 myTextBlock.Foreground = new SolidColorBrush((Color)Application.Current.Resources["PhoneForegroundColor"]); 

But ... the Resuming event is Resuming before the Application.Current resource is updated, so I get the same color as before. If the user crashes again and again, it will work because Application.Current.Resources["PhoneForegroundColor"] was updated at some point after the Resuming event in the previous time.

Question: When can I read the updated Application.Current.Resources["PhoneForegroundColor"] , since Resuming seems to be the right place?

Question: Alternatively, is there a way for myTextBlock inherit another ForegroundColor component (CSS-ish), so that I can programmatically change myTextBlock.Foreground between Red / Blue / Inherit without what needs to be taken into account changes in the phone theme during the application life cycle?

Any suggestions appreciated!

+5
source share
3 answers

Regarding your first question: β€œThe renewal process” is not officially documented, but I understood the following:

The call in the user interface thread resumes. Since this is a void return method, the caller will simply continue when he waits inside. If you add something to the user interface thread, it will be in the dispatcher queue and will start after the current task (resume).

So, I just did this (and it works ^^):

 private async void App_Resuming(object sender, object e) { var x1 = new SolidColorBrush((Color)Application.Current.Resources["PhoneForegroundColor"]); Debug.WriteLine(x1?.Color.ToString()); // Await marshalls back to the ui thread, // so it gets put into the dispatcher queue // and is run after the resuming has finished. await Task.Delay(1); var x2 = new SolidColorBrush((Color)Application.Current.Resources["PhoneForegroundColor"]); Debug.WriteLine(x2?.Color.ToString()); } 

Regarding the second question: You can enter "ValueProvider" in your app.xaml, which logs a resume event and simply provides a dependency property with the current color.

You still have to install this on any text block you want to use, but at least directly in XAML. This may also work for styles, but have not tried this.

Implementation example ....

Provider:

 public class ColorBindingProvider : DependencyObject { public ColorBindingProvider() { App.Current.Resuming += App_Resuming; } private async void App_Resuming(object sender, object e) { // Delay 1ms (see answer to your first question) await Task.Delay(1); TextColor = new SolidColorBrush((Color)Application.Current.Resources["PhoneForegroundColor"]); } public Brush TextColor { get { return (Brush)GetValue(TextColorProperty); } set { SetValue(TextColorProperty, value); } } public static readonly DependencyProperty TextColorProperty = DependencyProperty.Register("TextColor", typeof(Brush), typeof(ColorBindingProvider), new PropertyMetadata(null)); } 

App.xaml:

 <Application.Resources> <local:ColorBindingProvider x:Name="ColorBindingProvider" TextColor="{StaticResource PhoneForegroundBrush}" /> </Application.Resources> 

MainPage.xaml:

 <TextBlock Text="Hey ho let go" Foreground="{Binding TextColor, Source={StaticResource ColorBindingProvider}}" /> 
+2
source

In Windows Phone 8.1, you can define the selected theme using Application.Current.RequestedTheme witch will return the value of the Windows.UI.Xaml.ApplicationTheme enumeration.

Example:

 public static string GetImagePath(){ // if the background color is black, i want a white image if(Application.Current.RequestedTheme == ApplicationTheme.Dark) return "ms-appx:///Assets/img_light.jpg"; // if the background color is white, i want a dark image return "ms-appx:///Assets/img_dark.jpg"; } 

Note: you can even change the selected theme using Application.Current.RequestedTheme

more details: https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.application.requestedtheme

+1
source

Registering in App_Resuming does not work for me because this event does not occur when the application is not paused. I had to listen to Window.Current.CoreWindow.VisibilityChanged += CoreWindow_VisibilityChanged;

There is no need for Task.Delay with this solution.

0
source

Source: https://habr.com/ru/post/1210365/


All Articles