如何使用 Xamarin Forms mac 应用程序进行关闭确认?

How to do a Close Confirmation with a Xamarin Forms mac App?

我有 Xamarin.Forms 申请 iOS, Android,,现在希望 Mac。我对 UI 进行了所有调整,以便在 Mac 上看起来很棒。在被拒绝的地方提交以供批准,因为用户可以关闭 window 而应用程序和菜单栏仍然是 运行。所以我想我会添加一个确认弹出窗口,询问他们在尝试关闭 window.

时是否要退出应用程序

我找到很多关于如何使用 Xamarin.Mac 应用程序处理此问题的文章,但没有关于如何在 Mac 上处理 Xamarin.Forms 的文章。 FormsApplicationDelegate 不允许访问视图控制器或 Window 委托以覆盖 WindowShouldClose 方法。我发现我可以使用 NSAlert 来制作效果很好的弹出窗口。现在我找不到关于用户响应时该怎么做的任何信息。接受建议。

 private void Window_WillClose(object sender, System.EventArgs e)
    {
        NSNotification senderNotification = ((NSNotification)sender); 
        NSWindow closingWindow = (NSWindow)senderNotification.Object;

        var confirmation = new NSAlert()
        {
            AlertStyle = NSAlertStyle.Warning,
            InformativeText = "Are you sure you want to exit the App?",
            MessageText = "Exit?"
        };
        confirmation.AddButton("OK");
        confirmation.AddButton("Cancel");
        var result = confirmation.RunModal();

        if (result == 1001)
        {
            //Cancel closing the window
        }
        else
        {
            //terminate the app
        }

    }

经过大量试验,我确实找到了解决办法。这是正式通过Apple审查的内容。它要求 n 个菜单操作链接为 "New Window"。它会跟踪打开的 windows,当只剩下一个时,它会提示关闭该应用程序。如果用户关闭所有 windows 并保留应用程序 运行,他们可以选择在菜单中打开一个新的 window。

 [Register("AppDelegate")]
    public class AppDelegate : FormsApplicationDelegate
    {
        public NSWindow window;
        private bool closeApp;
        private List<NSWindow> openWindows;
        public override NSWindow MainWindow
        {
            get { return window; }
        }


        public AppDelegate()
        {
            this.closeApp = false;
            this.openWindows = new List<NSWindow>();
            createNewWindow();
        }

        [Action("newWindow:")]
        public void newWindow(NSObject sender)
        {

            createNewWindow();
            this.window.MakeKeyAndOrderFront(sender);
            LoadApplication(new App());
            base.DidFinishLaunching(null);

        }

        private void createNewWindow()
        {
            var style = NSWindowStyle.Closable | NSWindowStyle.Resizable | NSWindowStyle.Titled;
            var rect = new CoreGraphics.CGRect(200, 1000, 1024, 768);
            window = new MainWindow(rect, style, NSBackingStore.Buffered, false);
            window.Title = "MyApp"; // choose your own Title here
            window.TitleVisibility = NSWindowTitleVisibility.Hidden;
            window.WillClose += Window_WillClose;
            openWindows.Add(window);
        }

        private void Window_WillClose(object sender, System.EventArgs e)
        {
            openWindows.Remove((NSWindow)((NSNotification)sender).Object);
            if (openWindows.Count == 0)
            {
                var confirmation = new NSAlert()
                {
                    AlertStyle = NSAlertStyle.Warning,
                    InformativeText = "Do you want to exit the app?",
                    MessageText = "Exit?"
                };
                confirmation.AddButton("Yes");
                confirmation.AddButton("No");
                var result = confirmation.RunModal();

                if (result == 1001)
                {
                    this.closeApp = false;
                }
                else
                {
                    //terminate the app
                    this.closeApp = true;
                }
            }
        }

        public override bool ApplicationShouldTerminateAfterLastWindowClosed(NSApplication sender)
        {
            return closeApp;
        }

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