locked
How to show MessageDialog.ShowAsync in Try-catch block RRS feed

  • Question

  • How to solve above problem? The below method will crash.

    Appreciate your help.  TIA

    private async void GetDetail()
    {
     Try
      {

      -- code --

       var MyMessageBox2 = new Windows.UI.Popups.MessageDialog("SubTotal: " + SubTTL.ToString() + " Total =" +   TTLAmt.ToString());
       
         await MyMessageBox2.ShowAsync();


       }
      catch(Exception ex)
      {

      }
    }

    Monday, January 27, 2014 7:20 AM

Answers

  • Please don't post the same question in multiple threads. As noted in your other thread you cannot nest your calls to the MessageDialog.

    You could delay the second MessageDialog until after the first had finished, but really you should redesign this to keep the question and errors in a single page rather than yanking the user between MessageDialogs.

    If you need further help with this please continue in your other thread rather than duplicating effort.

    --Rob

    Tuesday, February 4, 2014 6:49 AM
    Moderator

All replies

  • Hi

    Not sure what is the issue, it works fine for me. Please find the code I used. I guess SubTTL is a object not a textbox or any control, if that is the case SubTTL.Text should be used. If you need more help please provide more inputs.

     private async void GetDetail()
            {
                try
                {

                    var MyMessageBox2 = new Windows.UI.Popups.MessageDialog("SubTotal: " + SubTTL.ToString() + " Total =" +   TTLAmt.ToString());

                    await MyMessageBox2.ShowAsync();

                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                }
            }

     Regards


    Varun Ravindranath


    Monday, January 27, 2014 8:36 AM
  • The issue is :

    The MessageDialog's ShowAsync() method  can not use inside a Try-Catch block.

    From my code above, after I have completed the task and want to display the result in a MessageDialog box ,  it will crash.

    Thanks.

    Monday, January 27, 2014 9:51 AM
  • Hi

    Can you give the details of how the task is executed. Message dialog works fine.

    Regards


    Varun Ravindranath

    Monday, January 27, 2014 4:06 PM
  • Hi FireDance,

    Are you using try-catch code within UI-thread?

    Try following code to back to UI:

    dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => 
    {
            var dialog = new Windows.UI.Popups.MessageDialog("xxx");
            dialog.ShowAsync(); 
    });
    

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Monday, February 3, 2014 8:18 AM
    Moderator
  • Hi

    I think this will not work for the case below:

    dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
    {
           
    var dialog = new Windows.UI.Popups.MessageDialog("xxx");
            dialog
    .ShowAsync();
    });

    --- Case :

      private async void btnOrderComplete_Click(object sender, RoutedEventArgs e)
            {

                var messageDialog = new MessageDialog("Is this order completed ?", "Order complete confirmation");

                messageDialog.Commands.Add(new UICommand("Yes", delegate(IUICommand command)
                {
                   
                    UpdateSalesOrderCompleteTrans();            


                }));

                messageDialog.Commands.Add(new UICommand("No", delegate(IUICommand command)
                {
                    return;
                }));
            
                await messageDialog.ShowAsync();

            }


     private async void UpdateSalesOrderCompleteTrans()
            {
                bool ShowError = false;
                System.Exception MyException = new Exception();

                try
                {

                -- can not show any messageDialog here in Try-Catch

                a) when there is error here

                b) or in If-else Condition

              

                 -- the messageDialog.ShowAsync() can not be showned.

       MessageDialog Msg2 = new MessageDialog("Please....", "No Transaction created");
                    await Msg2.ShowAsync();


                }
                catch (Exception ex)
                {
                    ShowError = true;
                    MyException = ex;
                }
                if (ShowError)
                {
                    var MyMessageBox = new Windows.UI.Popups.MessageDialog(MyException.Message);
                    await MyMessageBox.ShowAsync();
                }
            }       

     

    Tuesday, February 4, 2014 6:19 AM
  • Please don't post the same question in multiple threads. As noted in your other thread you cannot nest your calls to the MessageDialog.

    You could delay the second MessageDialog until after the first had finished, but really you should redesign this to keep the question and errors in a single page rather than yanking the user between MessageDialogs.

    If you need further help with this please continue in your other thread rather than duplicating effort.

    --Rob

    Tuesday, February 4, 2014 6:49 AM
    Moderator