C# 从WeakDictionary获取值时,ItemPeersStorage如何引发异常?

C# 从WeakDictionary获取值时,ItemPeersStorage如何引发异常?,c#,wpf,touch,C#,Wpf,Touch,我通过自动异常报告获得了以下异常,因此我没有太多上下文。看起来应用程序显示了一个几乎没有控件的视图,DataGrid是唯一的ItemsControl。应用程序使用的是触摸屏,我假设tabtip.exe可能导致调用AutomationPeer,因为我没有明确使用任何UI自动化。在没有任何用户交互的情况下,异常似乎在至少30分钟后发生 我的目标是.NET Framework 4.7.2,但计算机已安装.NET Framework 4.8 有人见过这个吗?有没有关于为什么会发生这种情况的线索 Unha

我通过自动异常报告获得了以下异常,因此我没有太多上下文。看起来应用程序显示了一个几乎没有控件的视图,
DataGrid
是唯一的
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)
来自.NET Framework 4.8的
System.Windows.Automation.Peers.ItemPeersStorage1.get\u Item(Object Item)
方法我不知道这是怎么发生的?在访问
WeakDictionary
之前,它会检查
ContainsKey()

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);
        }
}
public T此[对象项]
{
得到
{
如果(_count==0 | | item==null)
返回默认值(T);
如果(_usesHashCode)
{
if(_hashtable==null | |!_hashtable.ContainsKey(项))
返回默认值(T);
将哈希表[项]返回为T;
}
其他的
{
如果(_list==null)
返回默认值(T);
对于(int i=0;i<\u list.Count;i++)
{
KeyValuePair=_列表[i];
if(Object.Equals(item,pair.Key))
返回对值;
}
返回默认值(T);
}
}
请查看
ItemPeersStorage
内部使用的是什么,您会注意到它会对未包含的键抛出异常

public TValue this[TKey key]
    {
        get
        {
            if (!_hashTable.ContainsKey(key))
            {
                throw new KeyNotFoundException();
            }
            return (TValue)_hashTable[key];
        }
        set
        {
            _hashTable.SetWeak(key, value);
        }
    }
请查看
ItemPeersStorage
内部使用的是什么,您会注意到它会对未包含的键抛出异常

public TValue this[TKey key]
    {
        get
        {
            if (!_hashTable.ContainsKey(key))
            {
                throw new KeyNotFoundException();
            }
            return (TValue)_hashTable[key];
        }
        set
        {
            _hashTable.SetWeak(key, value);
        }
    }
我下载了的代码,该版本中似乎存在错误,因为他们没有检查ContainsKey():

get
{
如果(_count==0 | | item==null)
返回默认值(T);
如果(_usesHashCode)
{
if(_hashtable==null)
返回默认值(T);
将哈希表[项]返回为T;
}
其他的
{
如果(_list==null)
返回默认值(T);
对于(int i=0;i<\u list.Count;i++)
{
KeyValuePair=_列表[i];
if(Object.Equals(item,pair.Key))
返回对值;
}
返回默认值(T);
}
}
由于我以.NET Framework 4.7.2为目标,并使用
配置元素,因此我没有使用.NET Framework 4.8代码,这是我最初假设的

因此,在.NET Framework 4.8中修复了错误。我想我将改为针对该版本。

我下载了的代码,该版本中似乎存在错误,因为他们没有检查ContainsKey():

get
{
如果(_count==0 | | item==null)
返回默认值(T);
如果(_usesHashCode)
{
if(_hashtable==null)
返回默认值(T);
将哈希表[项]返回为T;
}
其他的
{
如果(_list==null)
返回默认值(T);
对于(int i=0;i<\u list.Count;i++)
{
KeyValuePair=_列表[i];
if(Object.Equals(item,pair.Key))
返回对值;
}
返回默认值(T);
}
}
由于我以.NET Framework 4.7.2为目标,并使用
配置元素,因此我没有使用.NET Framework 4.8代码,这是我最初假设的


因此,在.NET Framework 4.8中修复了错误。我想我将改为针对该版本。

是的,我目前正在调查源代码,并且这似乎已在.NET 4.8的ItemPeersStorage中修复是的,我目前正在调查源代码,并且似乎已在.NET 4.8的ItemPeersStorage中修复