cansado2930
cansado2930

Reputation: 339

How to set dark theme on Xamarin Forms UWP?

I'm using on xamarin forms and on Android and iOS, and both when phone is using dark theme, it changes in order to use the same theme. But When I run app on UWP, xamarin forms work with Light theme, always. Could you tell me how to force to use dark theme? I have tried to set manually the dark theme on constructor of App.cs (UWP) as I do on native UWP apps, but it is setted but Xamarin forms continue working with Light theme. Any idea?

Steps to reproduce it.

Thank you

Upvotes: 0

Views: 892

Answers (1)

Nico Zhu
Nico Zhu

Reputation: 32785

If you want to set uwp theme manually, please call the following method in uwp client project.

public static class ThemeSelectorService
{

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


}

Usage

ThemeSelectorService.SetRequestedThemeAsync(ElementTheme.Dark);

Upvotes: 1

Related Questions