【问题标题】:.NET 4.0 WPF Automation Exception.NET 4.0 WPF 自动化异常
【发布时间】:2011-09-16 20:05:55
【问题描述】:

我有一个在 .NET 3.5 SP1 中运行良好的项目。现在,当升级到 .NET 4.0 时,我发生了一个自动化异常。

我已经在整个项目中搜索了与自动化相关的任何内容,但与自动化无关。谷歌搜索也无助于确认这是否是一个错误。该错误仅发生在少数 PC 上,并且是随机发生的。可以完全禁用自动化,因为我认为这可能是一个 .NET 4.0 错误?

Exception Source: PresentationCore
Message: Object reference not set to an instance of an object.
Stack Trace:
   at System.Windows.Automation.Peers.AutomationPeer.EnsureChildren()
   at System.Windows.Automation.Peers.AutomationPeer.UpdateChildrenInternal(Int32 invalidateLimit)
   at System.Windows.Automation.Peers.AutomationPeer.UpdateSubtree()
   at System.Windows.Automation.Peers.AutomationPeer.UpdateSubtree()
   at System.Windows.Automation.Peers.AutomationPeer.UpdatePeer(Object arg)
   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)
   at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.DispatcherOperation.InvokeImpl()
   at System.Threading.ExecutionContext.runTryCode(Object userData)
   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.ProcessQueue()
   at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   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)
   at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Application.RunInternal(Window window)
   at System.Windows.Application.Run()

【问题讨论】:

  • 不知道答案,建议大家继续观察。如果问题真的是随机的,那么它可能是 .net 4.0 的错误。否则不是
  • 我们遇到了同样的问题,当我删除了一些样式后就消失了。当我们确切地找出是什么部分导致它时,我会在这里写出来。
  • 好的,在我们的例子中,它与日期时间选择器日历样式的情节提要动画有关。我们仍然没有调查确切的原因,但我们会在动画解决后删除它。
  • 更新:原来动画可能不是确切的原因。有人在 Buttons 上添加了全局样式。结果,DateTimePicker 风格被搞砸了。他们放入的“修复”是用一大堆 xaml 代码覆盖 DateTimePicker 样式,可能是从某个网站复制的。风格非常复杂。 当我删除复杂的自定义 DateTimePicker 样式时,异常消失了。 我还删除了全局 Button 样式覆盖,这使 DateTimePicker 看起来再次正常。因此,只需删除一大堆 XAML 样式代码,我就阻止了异常。
  • 顺便说一句,它仍然是一个 WPF 错误,仅供记录。只是告诉我如何避免它。

标签: .net wpf exception automation


【解决方案1】:

EnsureChildren 方法相当简单:

private void EnsureChildren()
{
    if (!this._childrenValid || this._ancestorsInvalid)
    {
        this._children = this.GetChildrenCore();
        if (this._children != null)
        {
            int count = this._children.Count;
            for (int i = 0; i < count; i++)
            {
                this._children[i]._parent = this;
                this._children[i]._index = i;
                this._children[i]._hwnd = this._hwnd;
            }
        }
        this._childrenValid = true;
    }
}

出现 NullReferenceException 的唯一机会是 this.children[i] 代码。 GetChildrenCore 通常由自动化对等体实现,用于自定义 WPF 控件。因此,其中一种可能是在 GetChildrenCore 返回的集合中返回 null。

如果您有任何自定义控件或任何第三方控件实现了自定义自动化对等体,那么这很可能是可疑的。

您可以通过创建自定义类并覆盖 OnCreateAutomationPeer 并返回 null 来逐个元素地禁用 UI 自动化。这是我知道禁用自动化的唯一方法。

您最好的选择可能是删除 UI 的各种元素,以缩小导致问题的控件范围。

【讨论】:

  • 谢谢!好吧,我有所有自定义控件等的源代码。似乎没有什么在实现自定义自动化对等。我将尝试覆盖项目中所有 Windows 的 OnCreateAutomationPeer。
  • @Luke:你能以某种方式解决这个问题吗?如果是这样,你能否提供一些关于你做了什么作为答案的信息?我们目前遇到了同样的问题。
【解决方案2】:

我遇到了类似的问题,下面的帖子帮助了我:http://social.msdn.microsoft.com/Forums/ar/wpf/thread/0ee9954d-0df5-4d61-8dc9-eb50c7a5be99

将 DataTemplate 从“DayTitleTemplate”更改为“{x:Static CalendarItem.DayTitleTemplateResourceKey}”

【讨论】:

    【解决方案3】:

    这是 WPF 错误。如果没有为 Day Title 行(显示日期名称的日历部分)定义数据模板,CalendarAutomationPeer.GetChildrenCore 将返回空值。这会导致空指针异常,这些异常通常在 WPF 中的某处得到处理,但有时会导致应用程序崩溃。

    为了解决这个问题,只需定义 DayTitleTemplate(正如 Friggers 的评论和这篇文章 How can I change the DataTemplate for DayTitleTemplate in a CalendarItemTemplate 中所提到的)。

    【讨论】:

      猜你喜欢
      • 2020-01-13
      • 1970-01-01
      • 1970-01-01
      • 2011-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-24
      • 2019-09-15
      相关资源
      最近更新 更多