gpt4 book ai didi

c# - 如何避免 XamlParseException 关闭整个应用程序

转载 作者:太空狗 更新时间:2023-10-29 23:26:25 25 4
gpt4 key购买 nike

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

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

你知道实现这一目标的方法吗?

异常示例:

System.Windows.Markup.XamlParseException: Provide value on 'System.Windows.Markup.StaticResourceHolder' threw an exception. ---> System.Exception: Cannot find resource named 'PasteCommandRef'. Resource names are case sensitive.   at System.Windows.StaticResourceExtension.ProvideValueInternal(IServiceProvider serviceProvider, Boolean allowDeferredReference)   at System.Windows.StaticResourceExtension.ProvideValue(IServiceProvider serviceProvider)   at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CallProvideValue(MarkupExtension me, IServiceProvider serviceProvider)   --- End of inner exception stack trace ---   at System.Windows.Markup.XamlReader.RewrapException(Exception e, Uri baseUri)   at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlReader templateReader, XamlObjectWriter currentWriter)   at System.Windows.FrameworkTemplate.LoadTemplateXaml(XamlObjectWriter objectWriter)   at System.Windows.FrameworkTemplate.LoadOptimizedTemplateContent(DependencyObject container, IComponentConnector componentConnector, IStyleConnector styleConnector, List`1 affectedChildren, UncommonField`1 templatedNonFeChildrenField)   at System.Windows.FrameworkTemplate.LoadContent(DependencyObject container, List`1 affectedChildren)   at System.Windows.StyleHelper.ApplyTemplateContent(UncommonField`1 dataField, DependencyObject container, FrameworkElementFactory templateRoot, Int32 lastChildIndex, HybridDictionary childIndexFromChildID, FrameworkTemplate frameworkTemplate)   at System.Windows.FrameworkTemplate.ApplyTemplateContent(UncommonField`1 templateDataField, FrameworkElement container)   at System.Windows.FrameworkElement.ApplyTemplate()   at System.Windows.FrameworkElement.MeasureCore(Size availableSize)   at System.Windows.UIElement.Measure(Size availableSize)   at System.Windows.Controls.Primitives.UniformGrid.MeasureOverride(Size constraint)   at System.Windows.FrameworkElement.MeasureCore(Size availableSize)   at System.Windows.UIElement.Measure(Size availableSize)   at MS.Internal.Helper.MeasureElementWithSingleChild(UIElement element, Size constraint)   at System.Windows.Controls.ItemsPresenter.MeasureOverride(Size constraint)   at System.Windows.FrameworkElement.MeasureCore(Size availableSize)   at System.Windows.UIElement.Measure(Size availableSize)   at System.Windows.Controls.Border.MeasureOverride(Size constraint)   at System.Windows.FrameworkElement.MeasureCore(Size availableSize)   at System.Windows.UIElement.Measure(Size availableSize)   at System.Windows.Controls.Control.MeasureOverride(Size constraint)   at System.Windows.FrameworkElement.MeasureCore(Size availableSize)   at System.Windows.UIElement.Measure(Size availableSize)   at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)   at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV)   at System.Windows.Controls.Grid.MeasureOverride(Size constraint)   at System.Windows.FrameworkElement.MeasureCore(Size availableSize)   at System.Windows.UIElement.Measure(Size availableSize)   at System.Windows.Controls.Grid.MeasureCell(Int32 cell, Boolean forceInfinityV)   at System.Windows.Controls.Grid.MeasureCellsGroup(Int32 cellsHead, Size referenceSize, Boolean ignoreDesiredSizeU, Boolean forceInfinityV)   at System.Windows.Controls.Grid.MeasureOverride(Size constraint)   at System.Windows.FrameworkElement.MeasureCore(Size availableSize)   at System.Windows.UIElement.Measure(Size availableSize)   at System.Windows.ContextLayoutManager.UpdateLayout()   at System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)   at System.Windows.Media.MediaContext.InvokeOnRenderCallback.DoWork()   at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()   at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)   at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler)

最佳答案

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

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

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

但只是盲目地接受 XAMLParseException 会导致您的应用程序出现更多问题,这些问题将难以调试,因为异常会被接受。

关于c# - 如何避免 XamlParseException 关闭整个应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5472867/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com