【问题标题】:How can I avoid whole application shutdown on XamlParseException如何避免在 XamlParseException 上关闭整个应用程序
【发布时间】:2011-07-25 06:40:30
【问题描述】:

我的应用程序是多窗口的。如果发生 XamlParseException,则整个应用程序将关闭。

我最想做的是关闭这个错误的“负责”窗口。

您知道实现此目的的方法吗?

异常示例:

System.Windows.Markup.XamlParseException:在“System.Windows.Markup.StaticResourceHolder”上提供值引发异常。 ---> System.Exception:找不到名为“PasteCommandRef”的资源。资源名称区分大小写。
   在 System.Windows.StaticResourceExtension.ProvideValueInternal(IServiceProvider serviceProvider,布尔 allowDeferredReference)
   在 System.Windows.StaticResourceExtension.ProvideValue(IServiceProvider serviceProvider)
   在 MS.Internal.Xaml.Runtime.ClrObjectRuntime.CallProvideValue(MarkupExtension me,IServiceProvider serviceProvider)
   --- 内部异常堆栈跟踪结束 ---
   在 System.Windows.Markup.XamlReader.RewrapException(异常 e,Uri baseUri)
   在 System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader,XamlObjectWriter currentWriter)
   在 System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlObjectWriter objectWriter)
   在 System.Windows.FrameworkTemplate.LoadOptimizedTemplateContent(DependencyObject 容器,IComponentConnector 组件连接器,IStyleConnector 样式连接器,列表`1 受影响的孩子,UncommonField`1 模板非FeChildrenField)
   在 System.Windows.FrameworkTemplate.LoadContent(DependencyObject 容器,列表`1 受影响的孩子)
   在 System.Windows.StyleHelper.ApplyTemplateContent(UncommonField`1 dataField,DependencyObject 容器,FrameworkElementFactory templateRoot,Int32 lastChildIndex,HybridDictionary childIndexFromChildID,FrameworkTemplate frameworkTemplate)
   在 System.Windows.FrameworkTemplate.ApplyTemplateContent(UncommonField`1 templateDataField,FrameworkElement 容器)
   在 System.Windows.FrameworkElement.ApplyTemplate()
   在 System.Windows.FrameworkElement.MeasureCore(可用大小)
   在 System.Windows.UIElement.Measure(可用大小)
   在 System.Windows.Controls.Primitives.UniformGrid.MeasureOverride(大小约束)
   在 System.Windows.FrameworkElement.MeasureCore(可用大小)
   在 System.Windows.UIElement.Measure(可用大小)
   在 MS.Internal.Helper.MeasureElementWithSingleChild(UIElement 元素,大小约束)
   在 System.Windows.Controls.ItemsPresenter.MeasureOverride(大小约束)
   在 System.Windows.FrameworkElement.MeasureCore(可用大小)
   在 System.Windows.UIElement.Measure(可用大小)
   在 System.Windows.Controls.Border.MeasureOverride(大小约束)
   在 System.Windows.FrameworkElement.MeasureCore(可用大小)
   在 System.Windows.UIElement.Measure(可用大小)
   在 System.Windows.Controls.Control.MeasureOverride(大小约束)
   在 System.Windows.FrameworkElement.MeasureCore(可用大小)
   在 System.Windows.UIElement.Measure(可用大小)
   在 System.Windows.Controls.Grid.MeasureCell(Int32 单元格,布尔 forceInfinityV)
   在 System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead,大小 referenceSize,布尔 ignoreDesiredSizeU,布尔 forceInfinityV)
   在 System.Windows.Controls.Grid.MeasureOverride(大小约束)
   在 System.Windows.FrameworkElement.MeasureCore(可用大小)
   在 System.Windows.UIElement.Measure(可用大小)
   在 System.Windows.Controls.Grid.MeasureCell(Int32 单元格,布尔 forceInfinityV)
   在 System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead,大小 referenceSize,布尔 ignoreDesiredSizeU,布尔 forceInfinityV)
   在 System.Windows.Controls.Grid.MeasureOverride(大小约束)
   在 System.Windows.FrameworkElement.MeasureCore(可用大小)
   在 System.Windows.UIElement.Measure(可用大小)
   在 System.Windows.ContextLayoutManager.UpdateLayout()
   在 System.Windows.ContextLayoutManager.UpdateLayoutCallback(对象 arg)
   在 System.Windows.Media.MediaContext.InvokeOnRenderCallback.DoWork()
   在 System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
   在 System.Windows.Media.MediaContext.RenderMessageHandlerCore(对象 resizedCompositionTarget)
   在 System.Windows.Media.MediaContext.RenderMessageHandler(对象 resizedCompositionTarget)
   在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调,对象 args,Int32 numArgs)
   在 MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)

【问题讨论】:

  • 愚蠢的答案,但避免 XamlParseException 将是处理它的最佳方法。这是一个非常严重的异常(这可能意味着在初始化控件时发生了一些问题),忽略它只会破坏 UI 的外观:/

标签: c# wpf exception xaml


【解决方案1】:

在您的示例中,应用程序崩溃了,因为它找不到您通过 StaticResource 标记扩展静态引用的资源。您是在告诉 WPF,当您去寻找该资源时,它会更好地存在。所以当然会有问题。

要修复您的特定示例(避免在找不到资源时出现异常),您可以更改为使用 DynamicResource 标记扩展。这将告诉 WPF 在寻找资源时可能 存在,并且 WPF 在为 this 示例抛出异常时会更加轻松。

一般来说,您应该始终为 WPF 应用程序提供根级异常处理程序,正如 Wesley 指出的那样,使用 App_HandledExcpetion 方法。

但只是盲目地吞下 XAMLParseException 会导致您的应用程序出现更多问题,如果吞下异常,这些问题将难以调试。

【讨论】:

    【解决方案2】:

    由于您无法将 XAML 包装在 Try..Catch blocks 中,因此您需要通过代码加载数据源,将其包装在异常处理中,并在绑定之前验证数据源是有效的 XML。

    我想,您也可以在 App.Application_UnhandledException 中捕获未处理的异常,但它并不那么优雅。

    【讨论】:

    • 在执行示例中,我不知道需要将 Try..Catch 块放在哪里。你有什么想法吗?
    • 如果你搜索“XAML Try Catch”,网上有很多例子,比如eggheadcafe.com/tutorials/aspnet/…,对于未指定try..catch的未处理异常,你只需在应用程序中处理事件类。
    猜你喜欢
    • 2021-01-28
    • 2019-09-18
    • 2016-06-07
    • 2020-08-20
    • 1970-01-01
    • 2011-06-17
    • 2020-03-06
    • 2014-08-10
    • 2019-09-18
    相关资源
    最近更新 更多