Search code examples
c#uwpmvvm-light

UWP Change theme in pop ups MVVM Light


public static async Task SetRequestedThemeAsync()
        {
            foreach (var view in CoreApplication.Views)
            {
                await view.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    if (Window.Current.Content is FrameworkElement frameworkElement)
                    {
                        frameworkElement.RequestedTheme = Theme;
                    }
                });
            }
        }

This is my code that correctly changes the theme of views in my app, but now I have some ContentDialogs and they don't change their theme. So in this case I wanna ask two questions:

  1. I content my popups in static full properties. Is this a good decision or will be better to create a new obj of popup every time?
  2. If static properties are a good decision, how to change themes in them?

Solution

  • ContentDialogs and they don't change their theme.

    It's looks known issue here. please feel free vote up this report. and currently there is a workaround that set the dialog's theme when show popup like the following.

    private async void Button_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
    {
        ContentDialog dialog = new ContentDialog();
        dialog.Title = "Save your work?";
        dialog.PrimaryButtonText = "Save";
        dialog.SecondaryButtonText = "Don't Save";
        dialog.CloseButtonText = "Cancel";
        dialog.DefaultButton = ContentDialogButton.Primary;
        dialog.Content = "Test Theme";
        dialog.RequestedTheme = (Window.Current.Content as FrameworkElement).RequestedTheme;
    
    
        var result = await dialog.ShowAsync();
    
    }