c# - 从 WeakDictionary 获取值时,ItemPeersStorage 如何抛出异常?
问题描述
我通过自动异常报告得到了以下异常,所以我没有太多上下文。看起来应用程序显示了一个带有少量控件的视图,aDataGrid
是唯一的ItemsControl
. 该应用程序正在使用触摸屏,我认为 tabtip.exe 可能会导致调用 AutomationPeer,因为我没有明确使用任何 UI 自动化。在没有任何用户交互的情况下至少 30 分钟后似乎发生了异常。
我的目标是 .NET Framework 4.7.2,但计算机安装了 .NET Framework 4.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)
System.Windows.Automation.Peers.ItemPeersStorage1.get_Item(Object item)
From .NET Framework 4.8 Reference Source中的方法我没有看到这是怎么发生的?它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);
}
}
解决方案
请查看内部使用的WeakDictionary SourceItemPeersStorage
,您会注意到它会为未包含的键引发异常。
public TValue this[TKey key]
{
get
{
if (!_hashTable.ContainsKey(key))
{
throw new KeyNotFoundException();
}
return (TValue)_hashTable[key];
}
set
{
_hashTable.SetWeak(key, value);
}
}
推荐阅读
- javascript - 打字稿:类型上不存在属性“导航”省略反应导航v5
- ios - 即使应用程序未启动,也会收到应用程序崩溃警报
- reporting-services - SSRS 组格式
- rust - 接受(可选)参考 fn 参数的 Rust 特征
- python - 如何从 OpenWeatherMap 中检索使用本地时区表示的给定本地日期和时间的天气预报?
- asp.net-mvc - 无法在 Visual Studio 中运行 MVC 应用程序,redirection.config 问题
- reactjs - React redux - 刷新页面不使用useEffect设置Form数据
- javascript - 如何在 Vuejs 中的 3 个组件之间传输数据?
- javascript - 在 JavaScript 代码中间插入一个变量
- dynamic-programming - 与活动选择动态规划作斗争