How to suppress UI exceptions in wpf?

让人想犯罪 __ 提交于 2019-12-25 00:08:48

问题


I am creating designer where user can drag controls to surface. Some controls in toolbar can contains broken logic and generate exceptions. When user drags broken items to surface i have unhandled exception.

I want to suppress this UI exception and show message like blend or visual studio:

workArea.Dispatcher.UnhandledException += MainContainerDispatcherUnhandledException

I can cache this exception like this:

protected void MainContainerDispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {

        }

What i should do to suppress this message? Remove all controls from UI and shows label? or i can set some properties so WPF will ignore this exceptions? Any comments regarding this?

Thanks.


回答1:


You can make use of AppDomain.UnhandledException and in case you recieve it, identify the control that made it happen, and

  • or show to the user a user-friendly message
  • or show some red surface (more or less like VS does)
  • or use an Adorner with animaniton (cool, but more to program)

Hope this helps.




回答2:


It is necessary to hadle the event: e.Handled=true;. DispatcherUnhandledExceptionEventArgs.Handled. Unhandled Exception Handler For WPF Applications



来源:https://stackoverflow.com/questions/10084196/how-to-suppress-ui-exceptions-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!