locked
Show message box when page opens in Windows RT

    Question

  • How can I show message box when a page has opened? This is the code for my WP project, but I want to know the WinRT version of it.

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
    
            CustomMessageBox messageBox = new CustomMessageBox()
            {
                Caption = "",
                Message = "",
                LeftButtonContent = "ok"
            };
    
            messageBox.Show();
        }

    Saturday, April 04, 2015 12:24 PM

Answers

  • Hi,

    For Universal Apps, the new APIs require you to use await MessageDialog().ShowAsync() (in Windows.UI.Popups) to bring it into line with Win 8.1.Also you need to add following namespace

     using Windows.UI.Popups;

    To see following code to 

     
     protected  override void OnNavigatedTo(NavigationEventArgs e)
            {
                navigationHelper.OnNavigatedTo(e);
              
               showMessageDialog();//when page has open msg dialogbox display
            }
    public async void showMessageDialog()
            {
                var msg = new MessageDialog("Popup message content", "Messagebox Title");
                msg.ShowAsync();
            }
    Don't forget to mark the right answer and vote up if helps you.
    • Marked as answer by m.findlay93 Saturday, April 04, 2015 4:02 PM
    Saturday, April 04, 2015 2:38 PM

All replies

  • In WinRT you need a MessageDialog instead of a MessageBox:

    var msg = new MessageDialog("Popup message content", "Title");
                msg.ShowAsync();

    You can use msg.Commands.Add() to add button commands.

    You need the Windows.UI.Popups namespace.

    The ShowAsync method is of course async, so you'll want to await it if you're getting a response.


    Visit http://blog.grogansoft.com/ for Windows development fun.

    Saturday, April 04, 2015 1:07 PM
  • OK, but I get an error after using your code. How can I fix it?

    Saturday, April 04, 2015 1:12 PM
  • Hi,

    For Universal Apps, the new APIs require you to use await MessageDialog().ShowAsync() (in Windows.UI.Popups) to bring it into line with Win 8.1.Also you need to add following namespace

     using Windows.UI.Popups;

    To see following code to 

     
     protected  override void OnNavigatedTo(NavigationEventArgs e)
            {
                navigationHelper.OnNavigatedTo(e);
              
               showMessageDialog();//when page has open msg dialogbox display
            }
    public async void showMessageDialog()
            {
                var msg = new MessageDialog("Popup message content", "Messagebox Title");
                msg.ShowAsync();
            }
    Don't forget to mark the right answer and vote up if helps you.
    • Marked as answer by m.findlay93 Saturday, April 04, 2015 4:02 PM
    Saturday, April 04, 2015 2:38 PM
  • protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
       
    {
           
    base.OnNavigatedTo(e);

           
    CustomMessageBox messageBox = new CustomMessageBox()
           
    {
               
    Caption = "",
               
    Message = "",
               
    LeftButtonContent = "ok"
           
    }; messageBox.Show();
       
    }
    Saturday, April 04, 2015 2:43 PM
  • How can I get the title to display on more than one line?
    Saturday, April 04, 2015 3:00 PM
  • Hi,

    As per my knowledge you can't give more than one line title ,simply you can see that following code when you add title more than one line Title2 not display(title2 display when cursorover go on Title 1) -

    public async void showMessageDialog()
            {
                var msg = new MessageDialog("Popup msg","Title1"+Environment.NewLine+"Title2");
                msg.ShowAsync();
             
            }
    Don't forget to mark the right answer and vote up if helps you.


    • Edited by Siddhanath Saturday, April 04, 2015 3:45 PM
    Saturday, April 04, 2015 3:44 PM
  • OK shame but thanks for letting me know.
    Saturday, April 04, 2015 4:01 PM