mscorlib.dll 更新 DataGrid 时出现未处理的异常 argumentnullexception
Unhandled exception argumentnullexception in mscorlib.dll when updating a DataGrid
所以我有一个 wpf 应用程序,它有一个数据网格来显示数据库中的项目。我想刷新项目,所以我实现了一个刷新方法来替换需要刷新的项目。
Datagrid 项目源绑定到一个 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;
}
只要没有新项目,我就可以毫无问题地刷新我的列表。
如果我将新项目添加到我的列表并使用此方法刷新,当我向下滚动以查看新项目时,我的应用程序会在 mscorlib.dll 中抛出异常。显示以下消息和堆栈跟踪:
留言:
{"Value can not be null.\r\nParametername: key"}
堆栈跟踪:
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()
如果我改为重新创建 ObservableCollection 和 ICollectionView,一切正常。
当我重新启动应用程序时,我添加的新项目就在那里,一切看起来都很正常。
近一天来我一直在试图找出问题所在,但我似乎无法弄清楚。我 运行 .Net 4.6.1 在 visual studio 2015 年。
很明显它与堆栈跟踪中的数据网格有关,但我很难确定它不喜欢我的代码的地方。
有什么想法吗?
看起来你这里有错误:
如果(currentItem == null)
Orders.Add(currentItem);
在没有看到更多代码的情况下我无法确定,但我假设 Orders 是您的 ObservableCollection?如果是这样,我认为您可能试图从与 UI 线程不同的线程更新它。
您可以尝试在您的构造函数中或在您创建 ObservableCollection 的地方添加它。
BindingOperations.EnableCollectionSynchronization(Orders, Orders);
它将允许您从与 UI 线程不同的线程修改集合。
所以我有一个 wpf 应用程序,它有一个数据网格来显示数据库中的项目。我想刷新项目,所以我实现了一个刷新方法来替换需要刷新的项目。
Datagrid 项目源绑定到一个 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;
}
只要没有新项目,我就可以毫无问题地刷新我的列表。 如果我将新项目添加到我的列表并使用此方法刷新,当我向下滚动以查看新项目时,我的应用程序会在 mscorlib.dll 中抛出异常。显示以下消息和堆栈跟踪:
留言: {"Value can not be null.\r\nParametername: key"}
堆栈跟踪:
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()
如果我改为重新创建 ObservableCollection 和 ICollectionView,一切正常。
当我重新启动应用程序时,我添加的新项目就在那里,一切看起来都很正常。
近一天来我一直在试图找出问题所在,但我似乎无法弄清楚。我 运行 .Net 4.6.1 在 visual studio 2015 年。
很明显它与堆栈跟踪中的数据网格有关,但我很难确定它不喜欢我的代码的地方。
有什么想法吗?
看起来你这里有错误: 如果(currentItem == null) Orders.Add(currentItem);
在没有看到更多代码的情况下我无法确定,但我假设 Orders 是您的 ObservableCollection?如果是这样,我认为您可能试图从与 UI 线程不同的线程更新它。
您可以尝试在您的构造函数中或在您创建 ObservableCollection 的地方添加它。
BindingOperations.EnableCollectionSynchronization(Orders, Orders);
它将允许您从与 UI 线程不同的线程修改集合。