如何在WPF中抑制UI异常? [英] How to suppress UI exceptions in wpf?

查看:80
本文介绍了如何在WPF中抑制UI异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建设计器,用户可以在其中将控件拖动到表面。
工具栏上的某些控件可能包含损坏的逻辑并生成异常。
当用户将破碎的项目拖到表面时,我有未处理的异常。

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.

我想抑制此UI异常并显示消息,例如blend或visual studio:

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)
        {

        }

什么我应该做什么来抑制此消息?从用户界面中删除所有控件并显示标签?或者我可以设置一些属性,以便WPF会忽略此异常?对此有何评论?

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?

谢谢。

推荐答案

您可以使用 AppDomain.UnhandledException ,如果您收到它,请确定实现它的控件,然后

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


  • 或向用户显示用户友好消息

  • 或显示一些红色表面(或多或少像VS一样)

  • 或使用带有动画效果的Adorner(很酷,但还有更多编程功能)

希望这会有所帮助。

这篇关于如何在WPF中抑制UI异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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