开发者

How I can run MessageBox.Show() from App.xaml.cs

Can I run MessageBox.Show() from App.xaml.cs, so it is shown on current page. Please provide some code to this solution, or some other solution. I need to show some system message which is relevant for whol开发者_高级运维e application.


Not sure, but would this not work?

Deployment.Current.Dispatcher.BeginInvoke(() =>
{
 MessageBox.Show("");
}
);

You could also use the navigation data that is available in App.xaml.cs to determine the current Page and execute on that page.

I guess whatever you come up with, the app needs to be in a stable state, you probably can't do this when your handling (unhandled) exception. You should tell us a bit more so we understand why you want to do this. It seems that seomthing is wrong with your setup if you have to call MessageBox.Show from App.xaml.cs


A simple approach would be...

  public partial class App : Application
  {
    public static void ShowBox(string msg)
    {
      MessageBox.Show(msg);
    }
  }

And then from a window, or any other part of your application...

  public partial class MainWindow : Window
  {
    public MainWindow()
    {
      InitializeComponent();
      App.ShowBox("Hello!");
      ...

Since the 'ShowBox' method is static, you can just call into it from anywhere. Of course it goes without saying that this isn't really the 'cleanest' or most robust approach, but it will work.


What I understood from your question is you are looking for a way to display message from App.xaml.cs when some event occurs else where in your application.

You can have observer pattern implemented with App.xaml.cs registered for an event which may be triggered by your custom page.

Having said that, in long run I suggest you to use messaging framework for this purpose. Most of the MVVM frameworks come with messaging framework. They are easy to use with no or little learning curve.

  1. MVVM Light
  2. Simple MVVM
  3. PRISM from MS patterns & practices. (for larger applications) This is Not just an MVVM framework.
  4. Ultra light MVVM

there are many more such frameworks.


I used this extract from this link http://www.c-sharpcorner.com/UploadFile/c25b6d/message-dialog-in-windows-store-apps/

private async void Delete_Click(object sender, RoutedEventArgs e)
    {
        var messageDialog = new Windows.UI.Popups.MessageDialog("Are you sure you want to permanently delete this record?");

        messageDialog.Commands.Add(new UICommand("Yes", (command) =>
        {
            //Do delete
        }));
        messageDialog.Commands.Add(new UICommand("No", (command) =>
        {
            //Do Nothing
        }));
        //default command
        messageDialog.DefaultCommandIndex = 1;
        await messageDialog.ShowAsync();
    }


Had no luck with the other solutions, found:

CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync

Works on windows phone 8.1 for calling a MessageDialog to ui thread from App.xaml.cs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜