app.xaml.cs与MainViewmodel通讯出现问题 [英] Issue with app.xaml.cs to MainViewmodel communication

查看:148
本文介绍了app.xaml.cs与MainViewmodel通讯出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在wpf App.xaml.cs文件中遵循以下代码:

I've following code in my wpf App.xaml.cs file:

     void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)            {                var mainVM = MainWindowViewModel.Instance;                mainVM.DisplayMessage = string.Format("Something went wrong and it has been logged...If the problem persists, please contact {0}.", mainVM.NotificationsReceiver);                mainVM.DisplayMessageForegroundColor = "Red";                        e.Handled = true;        }


MainWindowViewModel.cs
   


MainWindowViewModel.cs
   

    public string DisplayMessage            {                get                {                    return m_displayMessage;                }                set                {                    m_displayMessage = value;                    OnPropertyChanged("DisplayMessage");                                                    }            }                public string DisplayMessageForegroundColor            {                get                {                    return m_displayMessageForegroundColor;                }                set                {                    m_displayMessageForegroundColor = value;                    OnPropertyChanged("DisplayMessageForegroundColor");                    }            }



MainWindow.xaml



MainWindow.xaml

     <Label Content="{Binding DisplayMessage}" Foreground="{Binding DisplayMessageForegroundColor}" Grid.Column="1" HorizontalAlignment="Left" Height="33" Margin="14,660,0,0" Grid.Row="1"                VerticalAlignment="Top" Width="693" Grid.ColumnSpan="3"/>


但这似乎不起作用.尽管正在调用app.xaml.cs中的方法,但我在UI上看不到该错误正在显示消息.请问这里可能出什么问题了?
(不过,从MainWindowViewModel内设置DisplayMessage和DisplayMessageForegroundColor属性时,我可以看到消息.).

请指教.

谢谢







But this does not seem to work.Although the method in app.xaml.cs is getting invoked, I dont see the error getting displayed message on the UI.What could be wrong here please?
(I'm able to see the message when I set the DisplayMessage and DisplayMessageForegroundColor properties from within MainWindowViewModel though).

Please advise.

Thanks.






推荐答案

在开始新文章之前,请先将所有有用的文章标记为答案,以关闭之前的主题.

Please close your previous threads by marking all helpful posts as answer before you start a new one.

您还应该对代码段的格式进行一些调整.

You should also work a bit on the formatting of your code snippets.

>>请问这有什么问题吗?

您正在以错误的方式处理异常.通常,在DispatcherUnhandledExceptionEvent事件处理程序中将DispatcherUnhandledExceptionEventArgs的Handled属性设置为true,然后继续假装运行应用程序是一种不好的做法. 什么都没发生.这可能会使您的应用程序处于未定义状态,在这种情况下,UI线程将无法对您的Label进行任何更新,因为它发生了异常.

You are handling the exception the wrong way. It is generally a bad practice to set the Handled property of the DispatcherUnhandledExceptionEventArgs to true in the DispatcherUnhandledException event handler and continue running the application pretending that nothing ever happened. This may leave your application in an undefined state and in this case the UI thread won't be able to render any updates to your Label since an exception has occurred on it.

您应该在发生异常的地方捕获它.当您通过这样处理DispatcherUnhandledException全局捕获异常时,在事件处理程序中唯一有意义的操作是记录异常并关闭应用程序:

You should catch the exception where it occurs. When you catch an exception globally by handling the DispatcherUnhandledException like this, the only meaningful you can do in the event handler is to log the exception and close down the application:

private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            MessageBox.Show("Ooops! An error occured: " + e.Exception.Message);
            //log...
            Environment.Exit(1);
        }

当发生未知异常时,您不应尝试使该进程保持活动状态.这可能导致比应用程序崩溃更严重的问题,例如数据损坏.

You should not try to keep the process alive when an unknown exception occurs. This may lead to far worse issues than having the application crash, like for example data corruption.

希望有帮助.

请记住,通过将有用的帖子标记为答案来关闭话题,然后在遇到新问题时开始新话题.请不要在同一线程中问几个问题.

Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.


这篇关于app.xaml.cs与MainViewmodel通讯出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆