mscorlib 中未处理的异常 argumentnullexception.dll更新 DataGrid 时

本文关键字:dll 更新 DataGrid argumentnullexception 异常 未处理 mscorlib | 更新日期: 2023-09-27 18:35:55

所以我有一个wpf应用程序,它有一个数据网格来显示数据库中的项目。我想刷新项目,所以我实现了一个刷新方法来替换需要刷新的项目。

Datagrid itemssource 绑定到 ICollectionView,该视图具有以项作为源的可观察集合。

它看起来像这样:

private async Task RefreshContentOrders()
{   
    _isRefreshingContentOrders = true;
    var orders = await _contentOrderSession.GetAllContentOrdersAvaliableToUser(CurrentUser);
    foreach (var order in orders)
    {
        var currentItem = Orders.FirstOrDefault(x =>x!=null&& x.Id == order.Id);
        if (currentItem == null)
            Orders.Add(currentItem);
        else
        {
        Orders.Remove(currentItem);
        Orders.Add(order);                 
        }
    }    
    _isRefreshingContentOrders = false;
}

只要没有新项目,我就可以毫无问题地刷新我的列表。如果我将一个新项目添加到我的列表中并使用此方法刷新,当我向下滚动以查看新项目时.dll我的应用程序会在 mscorlib 中引发异常。将显示以下消息和堆栈跟踪:

消息:{"值不能为空。''r'参数名称: 键"}

堆栈跟踪:

   vid System.Collections.Generic.Dictionary`2.FindEntry(TKey key)
   vid System.Collections.Generic.Dictionary`2.TryGetValue(TKey key, TValue& value)
   vid System.Windows.Controls.DataGridItemAttachedStorage.TryGetValue(Object item, DependencyProperty property, Object& value)
   vid System.Windows.Controls.DataGridRow.RestoreAttachedItemValue(DependencyObject objectWithProperty, DependencyProperty property)
   vid System.Windows.Controls.DataGridRow.SyncProperties(Boolean forcePrepareCells)
   vid System.Windows.Controls.DataGridRow.PrepareRow(Object item, DataGrid owningDataGrid)
   vid System.Windows.Controls.DataGrid.PrepareContainerForItemOverride(DependencyObject element, Object item)
   vid System.Windows.Controls.ItemsControl.MS.Internal.Controls.IGeneratorHost.PrepareItemContainer(DependencyObject container, Object item)
   vid System.Windows.Controls.ItemContainerGenerator.System.Windows.Controls.Primitives.IItemContainerGenerator.PrepareItemContainer(DependencyObject container)
   vid System.Windows.Controls.VirtualizingStackPanel.InsertContainer(Int32 childIndex, UIElement container, Boolean isRecycled)
   vid System.Windows.Controls.VirtualizingStackPanel.AddContainerFromGenerator(Int32 childIndex, UIElement child, Boolean newlyRealized, Boolean isBeforeViewport)
   vid System.Windows.Controls.VirtualizingStackPanel.MeasureChild(IItemContainerGenerator& generator, IContainItemStorage& itemStorageProvider, IContainItemStorage& parentItemStorageProvider, Object& parentItem, Boolean& hasUniformOrAverageContainerSizeBeenSet, Double& computedUniformOrAverageContainerSize, Double& computedUniformOrAverageContainerPixelSize, Boolean& computedAreContainersUniformlySized, IList& items, Object& item, IList& children, Int32& childIndex, Boolean& visualOrderChanged, Boolean& isHorizontal, Size& childConstraint, Rect& viewport, VirtualizationCacheLength& cacheSize, VirtualizationCacheLengthUnit& cacheUnit, Boolean& foundFirstItemInViewport, Double& firstItemInViewportOffset, Size& stackPixelSize, Size& stackPixelSizeInViewport, Size& stackPixelSizeInCacheBeforeViewport, Size& stackPixelSizeInCacheAfterViewport, Size& stackLogicalSize, Size& stackLogicalSizeInViewport, Size& stackLogicalSizeInCacheBeforeViewport, Size& stackLogicalSizeInCacheAfterViewport, Boolean& mustDisableVirtualization, Boolean isBeforeFirstItem, Boolean isAfterFirstItem, Boolean isAfterLastItem, Boolean skipActualMeasure, Boolean skipGeneration, Boolean& hasBringIntoViewContainerBeenMeasured, Boolean& hasVirtualizingChildren)
   vid System.Windows.Controls.VirtualizingStackPanel.MeasureOverrideImpl(Size constraint, Nullable`1& lastPageSafeOffset, List`1& previouslyMeasuredOffsets, Nullable`1& lastPagePixelSize, Boolean remeasure)
   vid System.Windows.Controls.VirtualizingStackPanel.MeasureOverride(Size constraint)
   vid System.Windows.Controls.Primitives.DataGridRowsPresenter.MeasureOverride(Size constraint)
   vid System.Windows.FrameworkElement.MeasureCore(Size availableSize)
   vid System.Windows.UIElement.Measure(Size availableSize)
   vid System.Windows.ContextLayoutManager.UpdateLayout()
   vid System.Windows.ContextLayoutManager.UpdateLayoutCallback(Object arg)
   vid System.Windows.Media.MediaContext.InvokeOnRenderCallback.DoWork()
   vid System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks()
   vid System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)
   vid System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)
   vid System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   vid System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   vid System.Windows.Threading.DispatcherOperation.InvokeImpl()
   vid System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)
   vid System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   vid System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   vid System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   vid System.Windows.Threading.DispatcherOperation.Invoke()
   vid System.Windows.Threading.Dispatcher.ProcessQueue()
   vid System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   vid MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   vid MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   vid System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   vid System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
   vid System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
   vid MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   vid MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   vid System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   vid System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   vid System.Windows.Application.RunDispatcher(Object ignore)
   vid System.Windows.Application.RunInternal(Window window)
   vid System.Windows.Application.Run(Window window)
   vid System.Windows.Application.Run()
   vid White.Db.ContentOrderDb.App.Main() i C:'Revit Dev'GENERELLA'DATABASER'ContentOrderDb'White.Db.ContentOrderDb.Application'obj'Debug'App.g.cs:rad 0
   vid System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   vid System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   vid Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   vid System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   vid System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   vid System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   vid System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   vid System.Threading.ThreadHelper.ThreadStart()

如果我改为重新创建可观察集合和ICollectionView一切正常。

当我重新启动应用程序时,我添加的新项目就在那里,一切似乎都很正常。

我已经试图弄清楚出了

什么问题,我已经快一天了,但我似乎无法弄清楚。我在Visual Studio 2015中运行.Net 4.6.1。

很明显,它

与堆栈跟踪中的数据网格有关,但我很难确定它不喜欢我的代码的地方。

有什么想法吗?

mscorlib 中未处理的异常 argumentnullexception.dll更新 DataGrid 时

看起来你在这里有一个错误: if (当前项 == 空) 订单.添加(当前项目);

没有看到更多的代码,我不能确定,但我认为订单是你的可观察集合?如果是这样,我认为您可能正在尝试从与 UI 线程不同的线程更新它。

您可以尝试看到的是将其添加到构造函数中或创建 ObservableCollection 的位置。

BindingOperations.EnableCollectionSynchronization(Orders, Orders);

它将允许您从与 UI 线程不同的线程修改集合。