首页 > 解决方案 > 从 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);
        }
}

标签: c#wpftouch

解决方案


请查看内部使用的WeakDictionary SourceItemPeersStorage ,您会注意到它会为未包含的键引发异常。

public TValue this[TKey key]
    {
        get
        {
            if (!_hashTable.ContainsKey(key))
            {
                throw new KeyNotFoundException();
            }
            return (TValue)_hashTable[key];
        }
        set
        {
            _hashTable.SetWeak(key, value);
        }
    }

推荐阅读