【问题标题】:Xceed PropertyGrid with System.Windows.Media.Color[] throws NullReferenceExceptionXceed PropertyGrid 与 System.Windows.Media.Color[] 抛出 NullReferenceException
【发布时间】:2016-11-30 15:34:18
【问题描述】:

我有一个对象,我正尝试与 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 对象数组并且失败了,但是该数组和里面的所有对象都不是空的。

有什么想法吗?

【问题讨论】:

    标签: c# arrays wpf propertygrid xceed


    【解决方案1】:

    这是CollectionControlDialog 类中的一个错误。

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

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

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

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

    目前,此错误使您无法将CollectionControlDialog 与没有默认无参数构造函数的对象一起使用(例如structs 或一些classes)。

    【讨论】:

    • 很好的答案!我将错误提交给他们的支持,它将在 v3.3 中修复。与此同时,我已经在本地副本中解决了它。
    猜你喜欢
    • 1970-01-01
    • 2018-10-30
    • 1970-01-01
    • 1970-01-01
    • 2013-05-30
    • 1970-01-01
    • 2020-11-17
    • 2016-03-21
    • 1970-01-01
    相关资源
    最近更新 更多