如何更改我的项目以使用 MacOS DarkMode 构建

问题描述

我是色盲,想在我的应用中使用 MacOS 中的暗模式。

我看到了 this,但我不知道它是否适合我的上下文。

作为色盲,我无法选择自己的配色方案。我只想要标准的黑暗模式。

目前我的窗口(全是 XAML / Xamarin.Forms 4.8 硬编码文本)看起来像这样:

enter image description here

感谢有关 Visual Studio for Mac 的任何指导。


更新 1

如果我添加以下控件:

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

我的窗口上的结果是 Light2。我的 MacOS 应用似乎被强制设为 Light 模式。


更新 2

添加一个按钮处理程序来测试这个:

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();
}

显示

enter image description here

所以应用中的主题Unspecified。根据这个 info 它指出:

当操作系统没有要请求的特定用户界面样式时,将返回

Unspecified。例如,在运行 13.0 之前版本的 iOS 的设备上。

我使用的是 macOS Big Sur(截至 2021 年 2 月 11 日的最新更新)并且设置为深色:

enter image description here

所以我仍然不明白为什么应用返回 Unspecified 而不是 Dark

如果有帮助,这是我的AppDelegate.cs

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);
        }
    }
}

解决方法

我未升级到最新的 5.x 并且现在显示很暗:

enter image description here

如果我切换主题,唯一不会正确更改的控件是 Button 对象。