user1547566
user1547566

Reputation: 403

How to display a message in Windows Store Apps?

How to display a message box in windows 8 apps using c# like calling MessageBox.Show() in windows phone 7?

Upvotes: 27

Views: 46090

Answers (6)

user5823144
user5823144

Reputation: 1

use for page like:

private async void AppBarButton_Click(object sender, RoutedEventArgs e)
{
    Windows.UI.Popups.MessageDialog a = new Windows.UI.Popups.MessageDialog("hello this is awth");
    await a.ShowAsync();
}

Upvotes: 0

Keith
Keith

Reputation: 349

Additional tidbit:

It appears in a modern Windows App a MessageDialog will not show prior to your app making its Window.Current.Active() call, which usually happens in the app class' OnLaunched() method. If you're trying to use MessageDialog to display something like a start-up exception, that's important to keep in mind.

My testing indicates MessageDialog.ShowAsync() may actually await but without the dialog being shown if Window.Current.Active() hasn't been called yet, so from a code execution standpoint it'll look like everything is working but yet no dialog is displayed.

If the goal is to display an exception during start-up, I can think of two options (there may be more).

  1. Capture the exception information and then wait to display it until after Window.Current.Activate(). This can work if the exception is such that the application can recover from it and continue with start-up. For example, if there is an error restoring saved state information the app might want to report that to the user but then continue to start-up as if there was no saved state.

  2. If the situation is such that the app is throwing up its hands and intending to terminate, but wants to let the user know what happened, then another solution might be to have a separate dedicated code block/method that plugs a new clean frame into Windows.Current.Content, activates it using Windows.Current.Activate(), and then invokes MessageDialog.ShowAsync(). I haven't experimented with this approach so I'm not sure if other conditions also need to be met like possibly loading a page into the frame.

Upvotes: 2

Antonio Bakula
Antonio Bakula

Reputation: 20693

My simpler way, for confirmation type message boxes:

  var dlg = new MessageDialog("Are you sure?");
  dlg.Commands.Add(new UICommand("Yes", null, "YES"));
  dlg.Commands.Add(new UICommand("No", null, "NO"));
  var op = await dlg.ShowAsync();
  if ((string)op.Id == "YES")
  {
    //Do something
  }

Upvotes: 12

Muhammed Mufees
Muhammed Mufees

Reputation: 116

For simpler way, Just to display the message text and OK button. Use Windows.UI.Popups namespace. Create a method messagebox() that method should be

using Windows.UI.Popups;
protected async void messageBox(string msg)
{
      var msgDlg = new Windows.UI.Popups.MessageDialog(msg);
      msgDlg.DefaultCommandIndex = 1;
      await msgDlg.ShowAsync();
}

Then call this method in your code like

messageBox("Unexpected error held");

Upvotes: 8

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39988

   MessageDialog msgDialog = new MessageDialog("Your message", "Your title");

   //OK Button
   UICommand okBtn = new UICommand("OK");
   okBtn.Invoked = OkBtnClick;
   msgDialog.Commands.Add(okBtn);

   //Cancel Button
   UICommand cancelBtn = new UICommand("Cancel");
   cancelBtn.Invoked = CancelBtnClick;
   msgDialog.Commands.Add(cancelBtn);

   //Show message
   msgDialog.ShowAsync();

And your call backs

private void CancelBtnClick(IUICommand command)
{
}

private void OkBtnClick(IUICommand command)
{
}


P.S. You can follow this tutorial for more.

Upvotes: 48

JP Alioto
JP Alioto

Reputation: 45117

The MessageDialog class should fit your needs.

Upvotes: 27

Related Questions