Search code examples
macosxamlxamarin.formsvisual-studio-macmacos-darkmode

How can I change my project to build with MacOS DarkMode


I am colour blind and would like to take advantage of the dark mode in MacOS for my app.

I saw this but I don't know if it is correct for my context.

Being colourblind I am not in a position to choose my own colour scheme. I just want the standard dark mode.

At the moment my window (which is all in just XAML / Xamarin.Forms 4.8 with hard coded text) looks like this:

enter image description here

Any pointers on a walk through for Visual Studio for Mac appreciated.


Update 1

If I add the following control:

<Label x:Name="currentThemeLabel" Text="{AppThemeBinding Dark='Dark1', Light='Light2',Default='Default3'}"/>

The result on my window is Light2. My MacOS app seems to be forced to be Light mode.


Update 2

I added a button handler to test this:

void OnButtonAddElderClicked(object sender, EventArgs e)
{
    var alert = new NSAlert()
    {
        AlertStyle = NSAlertStyle.Informational,
        InformativeText = "The active theme is: " + Application.Current.UserAppTheme.ToString(),
        MessageText = "Active Theme",
    };
    alert.RunModal();
}

And it displays:

enter image description here

So the theme in the app is Unspecified. According to this info it states:

Unspecified will be returned when the operating system does not have a specific user interface style to request. An example of this is on devices running versions of iOS older than 13.0.

I am using macOS Big Sur (latest update as of 11 Feb 2021) and it is set to Dark:

enter image description here

So I still don't understand why the app returned Unspecified instead of Dark.

This is my AppDelegate.cs if it helps:

using AppKit;
using Foundation;
using VisitsRota;
using Xamarin.Forms;
using Xamarin.Forms.Platform.MacOS;

namespace VisitsRota.MacOS
{
    [Register("AppDelegate")]
    public class AppDelegate : FormsApplicationDelegate
    {
        NSWindow window;
        public AppDelegate()
        {
            var style = NSWindowStyle.Closable | NSWindowStyle.Resizable | NSWindowStyle.Titled;

            var rect = new CoreGraphics.CGRect(200, 1000, 1024, 768);
            window = new NSWindow(rect, style, NSBackingStore.Buffered, false);
            window.Title = "Visits Rota for Mac";
            window.TitleVisibility = NSWindowTitleVisibility.Hidden;
           
        }

        public override NSWindow MainWindow
        {
            get { return window; }
        }

        public override void DidFinishLaunching(NSNotification notification)
        {
            Forms.Init();
            LoadApplication(new App());
            base.DidFinishLaunching(notification);
        }
    }
}

Solution

  • I ungraded to the latest 5.x snd it shows dark now:

    enter image description here

    The only controls that don't change properly if I toggle the theme is the Button objects.