使用 System.Windows.Media.Color[] 的 Xceed PropertyGrid 抛出 NullReferenceException

Xceed PropertyGrid with System.Windows.Media.Color[] throws NullReferenceException

我有一个对象,我正尝试将其与 PropertyGrid 一起使用,并且在尝试访问 Color[] 时抛出 NullReferenceException 属性。

堆栈跟踪看起来像:

System.NullReferenceException: Object reference not set to an instance of an object.
   at Xceed.Wpf.Toolkit.CollectionControlDialog.Clone(Object source) in C:\Users\brian\Documents\Visual Studio 2015\Projects\FevDnDTester\ExtendedWPFToolkitSolution\Src\Xceed.Wpf.Toolkit\CollectionControl\Implementation\CollectionControlDialog.xaml.cs:line 168
   at Xceed.Wpf.Toolkit.CollectionControlDialog.OnSourceInitialized(EventArgs e) in C:\Users\brian\Documents\Visual Studio 2015\Projects\FevDnDTester\ExtendedWPFToolkitSolution\Src\Xceed.Wpf.Toolkit\CollectionControl\Implementation\CollectionControlDialog.xaml.cs:line 137
   at System.Windows.Window.CreateSourceWindow(Boolean duringShow)
   at System.Windows.Window.CreateSourceWindowDuringShow()
   at System.Windows.Window.SafeCreateWindowDuringShow()
   at System.Windows.Window.ShowHelper(Object booleanBox)
   at System.Windows.Window.Show()
   at System.Windows.Window.ShowDialog()
   at Xceed.Wpf.Toolkit.CollectionControlButton.CollectionControlButton_Click(Object sender, RoutedEventArgs e) in C:\Users\brian\Documents\Visual Studio 2015\Projects\FevDnDTester\ExtendedWPFToolkitSolution\Src\Xceed.Wpf.Toolkit\CollectionControl\Implementation\CollectionControlButton.cs:line 124
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
   at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
   at System.Windows.Controls.Primitives.ButtonBase.OnClick()
   at System.Windows.Controls.Button.OnClick()
   at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
   at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)
   at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
   at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
   at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
   at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
   at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
   at System.Windows.Input.InputManager.ProcessStagingArea()
   at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
   at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
   at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
   at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   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)

如您所见,与我自己的代码没有任何关系,所以这一定是一个相当常见的错误。看起来它正在尝试克隆我的 Color 对象数组但失败了,但是数组和内部对象的 none 为空。

有什么想法吗?

这是 CollectionControlDialog class 中的错误。

在方法 Clone() 中,他们获取提供的 collection 项的类型,然后尝试调用该类型的默认构造函数。我想,他们想为每个 collection 项目创建一个深层副本。

但显然,struct Color 没有可以通过反射获得的默认构造函数(作为方法)。 (您可以在 struct 上使用的默认无参数构造函数实际上不是构造函数,但它是运行时如何创建 struct 的默认实例的 IL 指令)。

这就是为什么 GetConstructor() 方法 returns 为 null,并尝试在 null 上调用方法会抛出您观察到的 NullReferenceException

您可以列出错误或自行修复并提交到 Xceed 存储库。

目前,此错误会阻止您将 CollectionControlDialog 与没有默认无参数构造函数(如 struct 或某些 class也是)。