Show Alert after window has appeared

MatteoRisoMatteoRiso ✭✭USMember ✭✭
edited July 2017 in Xamarin.iOS

I'm using the following code:

public override void ViewDidAppear(){
            base.ViewDidAppear();
            string msg = GetCaption("Unable to retrieve date and time from any NTP server. Would you like to use local date and time as timestamp for your archive?");
            var calert = new NSAlert
            {
                MessageText = msg,
                AlertStyle = NSAlertStyle.Informational
            };
            calert.AddButton(GetCaption("Yes"));
            calert.AddButton(GetCaption("No"));
            var returnValue = calert.RunModal();

        }

But when I run my application I get the a stuck UI as depicted in the image attached to this post.
I'm surely missing something, but what?
How could I display an alert just after a window has appeared?

Answers

  • ChrisHamonsChrisHamons Xamurai USForum Administrator, Xamarin Team Xamurai

    So I tried that code in a new project, and I don't see the issue isolated.

    My guess is that there is something that needs UI thread which is being blocked with RunModal, which is running a nested message pump.

    You could look into either wrapping it in a

        BeginInvokeOnMainThread (() => { } 
    

    block, which might give the UI thread time before you setup your nested loop, or move to a sheet (https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Sheets/Concepts/AboutSheets.html).

    In any case, this strongly appears to be general Cocoa programming and not related to Xamarin.Mac.

  • MatteoRisoMatteoRiso ✭✭ USMember ✭✭
    edited July 2017

    Hello.
    Thanks but that didn't help.
    I solved by this way:

    msg = GetCaption("Unable to retrieve date and time from any NTP server. Would you like to use local date and time as timestamp for your archive?");
                    var alert = new NSAlert
                    {
    
                        MessageText = msg,
                        AlertStyle = NSAlertStyle.Critical
                    };
                    alert.AddButton(GetCaption("Yes"));
                    alert.AddButton(GetCaption("No"));
                    alert.BeginSheetForResponse(this.View.Window, (result) =>
                    {
                        if (result != 1000)
                        {
                            this.View.Window.Close();
                        }
                    });
    

    The key here is "alert.BeginSheetForResponse": I converted my alert into a sheet and it is working fine. I think that showing modal alerts while a window is loading should be considered very bad coding for Mac ;)

Sign In or Register to comment.