MatsL
MatsL

Reputation: 99

Enforce theme in Xamarin

I want to enforce the lightmode theme to my android application.

I've added this line of code to the app.cs

public partial class App : Application
{
    public App()
    {
        InitializeComponent();
        App.Current.UserAppTheme = OSAppTheme.Light;
        MainPage = new MainPage();
    }

I read this https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/theming/system-theme-changes but don't understand what I'm missing.

Upvotes: 0

Views: 293

Answers (3)

Mantas Daškevičius
Mantas Daškevičius

Reputation: 324

If you are planning to make your XF app cross platform and you will not be toggling between themes you can force light theme on iOS by setting OverrideUserInterfaceStyle in your custom page renderer:

using TheNameSpace;
using UIKit;
using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly: ExportRenderer(typeof(ContentPage), typeof(CustomPageRenderer))]
namespace TheNameSpace
{
    public class CustomPageRenderer : PageRenderer
    {
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

            OverrideUserInterfaceStyle = UIUserInterfaceStyle.Light;
        }
    }
}

Upvotes: 1

MatsL
MatsL

Reputation: 99

I've solved this by placing this line of code in the MainActivity.cs from the Android project

AppCompatDelegate.DefaultNightMode = AppCompatDelegate.ModeNightNo;

Placing this line of code directly at the start of the OnCreate method solves the problem.

Upvotes: 0

Cfun
Cfun

Reputation: 9701

Assigning App.Current.UserAppTheme is not enough, you have to define your light or/and dark theme through styles for different controls using the xaml extension AppThemeBinding like shown in examples in documentation link.

Upvotes: 1

Related Questions