C# 如何在 Windows 应用商店应用中显示消息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12435457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 23:17:01  来源:igfitidea点击:

How to display a message in Windows Store Apps?

c#windows-8

提问by user1547566

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

如何使用 c# 在 windows 8 应用程序中显示消息框,如在 windows phone 7 中调用 MessageBox.Show()?

采纳答案by JP Alioto

The MessageDialog classshould fit your needs.

MessageDialog类应该满足您的需求。

回答by Inder Kumar Rathore

   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 tutorialfor more.


PS您可以按照本教程了解更多信息。

回答by Muhammed Mufees

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

对于更简单的方法,只显示消息文本和确定按钮。使用Windows.UI.Popups命名空间。创建一个messagebox()方法应该是

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");

回答by Antonio Bakula

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
  }

回答by Keith

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.

它出现在现代 Windows 应用程序中,在您的应用程序进行 Window.Current.Active() 调用之前不会显示 MessageDialog,这通常发生在应用程序类的 OnLaunched() 方法中。如果您尝试使用 MessageDialog 来显示启动异常之类的内容,请务必牢记这一点。

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.

我的测试表明 MessageDialog.ShowAsync() 实际上可能会等待,但如果 Window.Current.Active() 尚未被调用,则不会显示对话框,因此从代码执行的角度来看,它看起来像一切正常但没有对话框被陈列。

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.

  1. 捕获异常信息,然后等待在 Window.Current.Activate() 之后显示它。如果异常使得应用程序可以从中恢复并继续启动,这可以工作。例如,如果恢复保存的状态信息时出现错误,应用程序可能希望向用户报告该错误,但随后继续启动,就好像没有保存的状态一样。

  2. 如果情况是应用程序举手并打算终止,但想让用户知道发生了什么,那么另一种解决方案可能是使用单独的专用代码块/方法将新的干净框架插入 Windows .Current.Content,使用 Windows.Current.Activate() 激活它,然后调用 MessageDialog.ShowAsync()。我还没有尝试过这种方法,所以我不确定是否还需要满足其他条件,比如可能将页面加载到框架中。

回答by user5823144

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();
}