如何从 App.xaml.cs 运行 MessageBox.Show()

发布于 2024-12-06 01:46:44 字数 102 浏览 0 评论 0原文

我可以从 App.xaml.cs 运行 MessageBox.Show() 吗,以便它显示在当前页面上。请提供此解决方案或其他解决方案的一些代码。我需要显示一些与整个应用程序相关的系统消息。

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 whole application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

安穩 2024-12-13 01:46:44

不确定,但这行不通吗?

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

您还可以使用 App.xaml.cs 中提供的导航数据来确定当前页面并在该页面上执行。

我想无论你想出什么,应用程序都需要处于稳定状态,当你处理(未处理的)异常时,你可能无法做到这一点。您应该告诉我们更多信息,以便我们了解您为什么要这样做。如果您必须从 App.xaml.cs 调用 MessageBox.Show,则您的设置似乎有问题

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

凡尘雨 2024-12-13 01:46:44

一个简单的方法是...

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

然后从窗口或应用程序的任何其他部分...

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

由于“ShowBox”方法是静态的,因此您可以从任何地方调用它。当然,不用说,这并不是真正的“最干净”或最强大的方法,但它会起作用。

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.

只是我以为 2024-12-13 01:46:44

我从您的问题中了解到,您正在寻找一种方法,当应用程序中其他地方发生某些事件时,显示来自 App.xaml.cs 的消息。

您可以使用 App.xaml.cs 实现观察者模式,为可能由您的自定义页面触发的事件注册。

话虽如此,从长远来看,我建议您使用消息传递框架来实现此目的。大多数MVVM 框架都带有消息传递框架。它们易于使用,没有或很少有学习曲线。

  1. MVVM Light
  2. Simple MVVM
  3. PRISM 来自 MS 图案和做法。 (对于较大的应用程序)这不仅仅是一个 MVVM 框架。
  4. 超轻MVVM

还有很多这样的框架。

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.

心舞飞扬 2024-12-13 01:46:44

我使用了此链接的摘录 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();
    }

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();
    }
仅冇旳回忆 2024-12-13 01:46:44

对其他解决方案没有运气,发现:

CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync

适用于 Windows Phone 8.1,用于从 App.xaml.cs 调用 MessageDialog 到 ui 线程。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文