locked
MessageDialog in MainPage Constructor

    Question

  • I have been running in circles on this one.  You need to use await with MessageDialog, but MainPage is not async, and you get an error if you try to make it async.  Nothing I have come across works.

    What I want to do:

    When the program is launched, after the splash page, when the main page is displayed, I want to pop up a dialog asking the user a yes/no question, and decide whether to allow the program to operate correctly or just sit there waiting to be closed based on the result.  Currently, in the constructor for the main page I have:

                MessageDialog dialog = new MessageDialog("Please answer yes or no", "My App");
                dialog.Commands.Clear();
                dialog.Commands.Add(new UICommand { Label = "Yes", Id = 0 });
                dialog.Commands.Add(new UICommand { Label = "No", Id = 1 });
                bool agrees = await dialog.ShowAsync();
                if (! agrees)
                {
                    timer = null;
                }
                else
                {
                     InitialiseLoop();
                }

    However, since MainPage is not async, and can't be made async, "bool agrees = await dialog.ShowAsync();" does not compile. I realize that MessageDialog may not even be the correct way to tackle this problem, but if it isn't, I can't figure out what the correct way is. 


    Larry Maturo

    • Moved by Mike Danes Monday, April 20, 2015 5:15 AM winstore specific
    Sunday, April 19, 2015 9:40 PM

Answers

  • The Page has a Loaded event which occurs once it has been constructed and is ready for interaction. There is no point of displaying a dialog until then anyway:

       public partial class MainPage : Page
        {
            public MainPage()
            {
                InitializeComponent();
                this.Loaded += Page_Loaded;
     }
    
     private async void Page_Loaded(object sender, RoutedEventArgs e)
            {
                //here you can await methods:
         ...
         bool agrees = await dialog.ShowAsync();
    
            }
    
    }
    

    You could mark the Loaded event handler as async and await any method in it.

    And the Loaded event is supported and available in both Windows Store Apps and Windows Phone 8.1 Windows Runtime apps: https://msdn.microsoft.com/en-uS/office/office365/windows.ui.xaml.frameworkelement.loaded.aspx?f=255&MSPPError=-2147217396


    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't post several questions in the same thread.

    • Marked as answer by lmaturo Monday, April 20, 2015 5:19 PM
    Monday, April 20, 2015 3:25 PM

All replies

  • Dear Imaturo

    You could try to popup the dialog after the page load

    Winform

    https://msdn.microsoft.com/en-us/library/system.windows.forms.form.load%28v=vs.110%29.aspx

    private void Form1_Load(object sender, EventArgs e) {    MessageDialog dialog = new MessageDialog("Please answer yes or no", "My App");

    ... } }





    Best Regards
    mintssoul

    Monday, April 20, 2015 4:38 AM
  • Unfortunately, that does not solve the problem.  In universal apps there isn't a load event, but the page does have a navigate to: void navigationHelper_LoadState(object sender, LoadStateEventArgs e).  As you can see it is not async either, so again I can't use the await in it.  While I can create the dialog, I can't display it.

    Larry Maturo

    Monday, April 20, 2015 2:19 PM
  • The Page has a Loaded event which occurs once it has been constructed and is ready for interaction. There is no point of displaying a dialog until then anyway:

       public partial class MainPage : Page
        {
            public MainPage()
            {
                InitializeComponent();
                this.Loaded += Page_Loaded;
     }
    
     private async void Page_Loaded(object sender, RoutedEventArgs e)
            {
                //here you can await methods:
         ...
         bool agrees = await dialog.ShowAsync();
    
            }
    
    }
    

    You could mark the Loaded event handler as async and await any method in it.

    And the Loaded event is supported and available in both Windows Store Apps and Windows Phone 8.1 Windows Runtime apps: https://msdn.microsoft.com/en-uS/office/office365/windows.ui.xaml.frameworkelement.loaded.aspx?f=255&MSPPError=-2147217396


    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't post several questions in the same thread.

    • Marked as answer by lmaturo Monday, April 20, 2015 5:19 PM
    Monday, April 20, 2015 3:25 PM
  • Thank you!  I had tried that, but by default it is not marked as async.  At your suggestion, I set it to async and it didn't give me a compile error. More importantly, the code worked as desired.  I was pretty sure putting it in the constructor was not the correct way to do it, but I hadn't run across any mention that Load could be marked async.

    Larry Maturo

    Monday, April 20, 2015 5:23 PM