我通过自动异常报告获得了下面的异常,所以我没有太多的上下文。看起来,应用程序显示了一个没有几个控件的视图,DataGrid是唯一的ItemsControl。应用程序使用的是触摸屏,我认为tabtip.exe可能会导致对AutomationPeer的调用,因为我没有显式地使用任何UI自动化。该异常似乎发生在至少30分钟之后,没有任何用户交互。
我的目标是.NET框架4.7.2,但这台计算机安装了.NET Framework4.8。
有人以前见过这个吗?关于为什么会发生这种事有什么暗示吗?
Unhandled exception occurred. Origin=System.Windows.Dispatcher.CurrentDispatcher.UnhandledException:
System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.
at MS.Internal.WeakDictionary`2.get_Item(TKey key)
at System.Windows.Automation.Peers.ItemPeersStorage`1.get_Item(Object item)
at System.Windows.Automation.Peers.ItemsControlAutomationPeer.GetPeerFromWeakRefStorage(Object item)
at System.Windows.Automation.Peers.ItemsControlAutomationPeer.AddProxyToWeakRefStorage(WeakReference wr, ItemAutomationPeer itemPeer)
at System.Windows.Automation.Peers.ItemAutomationPeer.AddToParentProxyWeakRefCache()
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.UpdateSubtree()
at System.Windows.Automation.Peers.AutomationPeer.UpdateSubtree()
at System.Windows.Automation.Peers.AutomationPeer.UpdateSubtree()
at System.Windows.Automation.Peers.AutomationPeer.UpdateSubtree()
at System.Windows.Automation.Peers.AutomationPeer.UpdateSubtree()
at System.Windows.Automation.Peers.AutomationPeer.UpdateSubtree()
at System.Windows.ContextLayoutManager.fireAutomationEvents()
at System.Windows.ContextLayoutManager.UpdateLayout()
at System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
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 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)来自.NET Framework4.8参考源的.NET方法--我不知道这是如何发生的?它在访问ContainsKey()之前检查WeakDictionary。
public T this[object item]
{
get
{
if (_count == 0 || item == null)
return default(T);
if (_usesHashCode)
{
if (_hashtable == null || !_hashtable.ContainsKey(item))
return default(T);
return _hashtable[item] as T;
}
else
{
if (_list == null)
return default(T);
for (int i = 0; i < _list.Count; i++)
{
KeyValuePair<object, T> pair = _list[i];
if (Object.Equals(item, pair.Key))
return pair.Value;
}
return default(T);
}
}发布于 2019-11-01 09:41:42
我下载了.NET框架4.7.2来自参考源的代码,似乎该版本中有一个bug,因为它们没有检查ContainsKey():
get
{
if (_count == 0 || item == null)
return default(T);
if (_usesHashCode)
{
if (_hashtable == null)
return default(T);
return _hashtable[item] as T;
}
else
{
if (_list == null)
return default(T);
for (int i = 0; i < _list.Count; i++)
{
KeyValuePair<object, T> pair = _list[i];
if (Object.Equals(item, pair.Key))
return pair.Value;
}
return default(T);
}
}由于我的目标是.NET Framework4.7.2并使用<supportedRuntime/>配置元素,所以我没有触及.NET Framework4.8代码,这是我最初假设的。
因此,在.NET框架4.8中修复了bug。我想我会用那个版本来代替。
发布于 2019-11-01 08:48:35
请查看WeakDictionary源,它在内部由ItemPeersStorage使用,您将注意到它会为未包含的键抛出异常。
public TValue this[TKey key]
{
get
{
if (!_hashTable.ContainsKey(key))
{
throw new KeyNotFoundException();
}
return (TValue)_hashTable[key];
}
set
{
_hashTable.SetWeak(key, value);
}
}https://stackoverflow.com/questions/58656290
复制相似问题