Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

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

$
0
0

I have a Xamarin.Forms application for iOS, Android, and now hopefully Mac. I made all the adjustments for the UI to look great on Mac. Submitted it for approval where it was rejected because the user can close the window where the Xamarin Forms UI is running, but the app itself and menu bar continue running. So I figure I would just add a confirmation pop-up asking if they want to exit the app when they try to close the window. OK = Terminate the App. Cancel = Keep the window open. I find lots of articles on how to handle this with a Xamarin.Mac app, but nothing on how to handle Xamarin.Forms on mac. The FormsApplicationDelegate does not give access to the View Controller or the Window Delegate in order to override the WindowShouldClose method. I found that I can use NSAlert to do the pop-up which works great. Now I cannot find anything on what to do when the user responds. I also can figure out how to quit the app via code in the mac project. Open to suggestions.

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
    }

}

Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>