我们使用WPF工具包DataGrid和.NET 4.0已有大约1.5年的时间。有些时候,在一些客户端PC上出现了异常--“对自动化对等API的递归调用无效”,其他用户则没有这个问题。
我们使用了带有模板列的WPF,该单元格模板有一个复选框(WPF作为CheckBox内容控件)。此外,我们在这个网格中使用了上下文菜单。
Type : System.InvalidOperationException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Message : Recursive call to Automation Peer API is not valid.
Source : PresentationCore
Help link :
Data : System.Collections.ListDictionaryInternal
TargetSite : System.Collections.Generic.List`1[System.Windows.Automation.Peers.AutomationPeer] GetChildren()
HResult : -2146233079
Stack Trace : at System.Windows.Automation.Peers.AutomationPeer.GetChildren()
at System.Windows.Automation.Peers.AutomationPeer.isDescendantOf(AutomationPeer parent)
at System.Windows.Automation.Peers.AutomationPeer.isDescendantOf(AutomationPeer parent)
at System.Windows.Automation.Peers.AutomationPeer.isDescendantOf(AutomationPeer parent)
at System.Windows.Automation.Peers.AutomationPeer.isDescendantOf(AutomationPeer parent)
at System.Windows.Automation.Peers.AutomationPeer.isDescendantOf(AutomationPeer parent)
at System.Windows.Automation.Peers.AutomationPeer.isDescendantOf(AutomationPeer parent)
at System.Windows.Automation.Peers.AutomationPeer.isDescendantOf(AutomationPeer parent)
at System.Windows.Automation.Peers.AutomationPeer.isDescendantOf(AutomationPeer parent)
at System.Windows.Automation.Peers.AutomationPeer.isDescendantOf(AutomationPeer parent)
at System.Windows.Automation.Peers.AutomationPeer.ValidateConnected(AutomationPeer connectedPeer)
at MS.Internal.Automation.ElementProxy.StaticWrap(AutomationPeer peer, AutomationPeer referencePeer)
at System.Windows.Automation.Peers.AutomationPeer.UpdateChildrenInternal(Int32 invalidateLimit)
at System.Windows.Automation.Peers.AutomationPeer.UpdateChildren()
at System.Windows.Automation.Peers.AutomationPeer.ResetChildrenCache()
at Microsoft.Windows.Automation.Peers.DataGridItemAutomationPeer.GetChildrenCore()
at System.Windows.Automation.Peers.AutomationPeer.EnsureChildren()
at System.Windows.Automation.Peers.AutomationPeer.GetChildren()
at System.Windows.Automation.Peers.AutomationPeer.isDescendantOf(AutomationPeer parent)
at System.Windows.Automation.Peers.AutomationPeer.isDescendantOf(AutomationPeer parent)
at System.Windows.Automation.Peers.AutomationPeer.isDescendantOf(AutomationPeer parent)
at System.Windows.Automation.Peers.AutomationPeer.isDescendantOf(AutomationPeer parent)
at System.Windows.Automation.Peers.AutomationPeer.isDescendantOf(AutomationPeer parent)
at System.Windows.Automation.Peers.AutomationPeer.isDescendantOf(AutomationPeer parent)
at System.Windows.Automation.Peers.AutomationPeer.isDescendantOf(AutomationPeer parent)
at System.Windows.Automation.Peers.AutomationPeer.isDescendantOf(AutomationPeer parent)
at System.Windows.Automation.Peers.AutomationPeer.isDescendantOf(AutomationPeer parent)
at System.Windows.Automation.Peers.AutomationPeer.ValidateConnected(AutomationPeer connectedPeer)
at System.Windows.Automation.Peers.AutomationPeer.AutomationPeerFromInputElement(IInputElement focusedElement)
at System.Windows.Automation.Peers.AutomationPeer.RaiseFocusChangedEventHelper(IInputElement newFocus)
at System.Windows.Input.KeyboardDevice.ChangeFocus(DependencyObject focus, Int32 timestamp)
at System.Windows.Input.KeyboardDevice.TryChangeFocus(DependencyObject newFocus, IKeyboardInputProvider keyboardInputProvider, Boolean askOld, Boolean askNew, Boolean forceToNullIfFailed)
at System.Windows.Input.KeyboardDevice.Focus(DependencyObject focus, Boolean askOld, Boolean askNew, Boolean forceToNullIfFailed)
at System.Windows.Input.KeyboardDevice.Focus(IInputElement element)
at System.Windows.Interop.HwndKeyboardInputProvider.OnSetFocus(IntPtr hwnd)
at System.Windows.Interop.HwndKeyboardInputProvider.FilterMessage(IntPtr hwnd, WindowMessage message, IntPtr wParam, IntPtr lParam, Boolean& handled)
at System.Windows.Interop.HwndSource.InputFilterMessage(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)让我们知道同样的解决方案,或者如果我们做错了什么,就提出建议。
发布于 2015-10-29 15:59:14
我已经使用较低版本的WPFToolkit解决了这个问题。早些时候,我使用的是WPFToolkit v3.5.50211.1,它导致了这个问题。现在,我正在使用更低的版本v3.5.40619.1来解决这个问题。
在WPFtoolkit v3.5.50211.1中,修复了一个与UI自动化相关的bug,我猜是因为这个自动化对等问题是在使用最新的WPFToolkit时出现的。
检查相关链接-
发布于 2015-10-19 14:22:17
这是在论堆叠溢出之前被问到的,但从未得到解决。
当我查看代码时,它看起来是由一些可能正在修改集合的东西引起的,因为集合是被枚举的,强制新的枚举在原始枚举已经发生时是不可能的。这是由自动化对等代码中的框架级保护造成的。
试图找出发生这种情况的方式和原因可能非常棘手,除非您已经包含了在修改集合时记录并设法成功调试该集合的代码。一种解决方案可能是确保所有的集合修改只发生在GUI线程上,但是如果有大量的话,这可能会导致UI阻塞。
不过,这只是猜测而已。就我个人而言,我从未在我所拥有的任何代码中使用过它。因此,如果您有能够成功地复制此代码的示例代码,我将很高兴地尝试并查看一下。
电位固定
我忘记提到,有些人建议,一个潜在的解决方法是通过添加以下内容来覆盖模板:
protected override System.Windows.Automation.Peers.AutomationPeer OnCreateAutomationPeer()
{
// Create your own AutomationPeer
// return new MyAutomationPeer(this);
// or return null if you don't need it
return null;
}这是否有效,以及它是否删除了任何功能,因为它基本上是说您没有对等,我不知道。
(参见此MSDN论坛帖子)
https://stackoverflow.com/questions/33212420
复制相似问题