我正在使用 MVVM Light V3 alpha 3 编写一个 WPF 4 应用程序(带有 VS2010 RC),并且在这里遇到了一些奇怪的行为......
我有一个打开 的命令Window
,并且该 Window 创建 ViewModel 等等 - 没有什么奇怪的。
在那Window
我有一些RelayCommand
s,例如:
CategoryBeenSelected = new RelayCommand(() => OnCategoryUpdate = true);
没有什么奇怪的了 - 它按我的预期工作。
问题是我不能拥有带有通用 RelayCommand 的 CanExecute 方法/lambda 表达式。
这有效:
DeleteCategoryCommand = new RelayCommand<int>(DeleteCategory);
但这不会:
DeleteCategoryCommand = new RelayCommand<int>(DeleteCategory, CanDeleteCategory);
窗口不显示。我的意思是,我单击打开窗口的按钮,应用程序被阻止,几秒钟后,窗口的InitializeComponent
方法抛出一个NullReferenceException
(对象引用未设置为对象的实例)
简而言之,如果我将CanExecute
Method 放在 a 上RelayCommand<T>
,则Window
拥有该ViewModel(带有RelayCommand<T>
)的 the 将无法实例化。如果我删除CanExecute
,Window
就会出现。
这里的问题在哪里?我很困惑。
谢谢你。
编辑:根据要求,这是堆栈跟踪:
PresentationFramework.dll 中出现“System.NullReferenceException”类型的第一次机会异常 在 GalaSoft.MvvmLight.Command.RelayCommand`1.CanExecute(对象参数) 在 System.Windows.Controls.Primitives.ButtonBase.UpdateCanExecute() 在 System.Windows.Controls.Primitives.ButtonBase.OnCommandChanged(DependencyObject d,DependencyPropertyChangedEventArgs e) 在 System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e) 在 System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e) 在 System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs 参数) 在 System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex、DependencyProperty dp、PropertyMetadata 元数据、EffectiveValueEntry oldEntry、EffectiveValueEntry& newEntry、布尔 coerceWithDeferredReference、布尔 coerceWithCurrentValue、OperationType operationType) 在 System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp、对象值、PropertyMetadata 元数据、布尔 coerceWithDeferredReference、布尔 coerceWithCurrentValue、OperationType operationType、布尔 isInternal) 在 System.Windows.DependencyObject.SetValue(DependencyProperty dp,对象值) 在 MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(对象实例,XamlMember 属性,对象值) 在 MS.Internal.Xaml.Runtime.PartialTrustTolerantRuntime.SetValue(对象 obj,XamlMember 属性,对象值) 在 System.Xaml.XamlObjectWriter.Logic_ApplyPropertyValue(ObjectWriterContext ctx,XamlMember 道具,对象值,布尔 onParent) 在 System.Xaml.XamlObjectWriter.Logic_DoAssignmentToParentProperty(ObjectWriterContext ctx) 在 System.Xaml.XamlObjectWriter.WriteEndObject() 在 System.Windows.Markup.WpfXamlLoader.TransformNodes(XamlReader xamlReader、XamlObjectWriter xamlWriter、Boolean onlyLoadOneNode、Boolean skipJournaledProperties、Boolean shouldPassLineNumberInfo、IXamlLineInfo xamlLineInfo、IXamlLineInfoConsumer xamlLineInfoConsumer、XamlContextStack`1 堆栈、IStyleConnector styleConnector) 在 System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader,IXamlObjectWriterFactory writerFactory,布尔型 skipJournaledProperties,对象 rootObject,XamlObjectWriterSettings 设置,Uri baseUri) 在 System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader,布尔 skipJournaledProperties,对象 rootObject,XamlAccessLevel accessLevel,Uri baseUri) 在 System.Windows.Markup.XamlReader.LoadBaml(流流,ParserContext parserContext,对象父,布尔 closeStream) 在 System.Windows.Application.LoadComponent(对象组件,Uri 资源定位器) 在 ApuntaNotas.Views.CategoryEditorView.InitializeComponent() 在 c:\Users\Jesus\Documents\Visual Studio 2010\Projects\ApuntaNotas\ApuntaNotas\Views\CategoryEditorView.xaml:line 1 在 ApuntaNotas.Views.CategoryEditorView..ctor() 在 C:\Users\Jesus\Documents\Visual Studio 2010\Projects\ApuntaNotas\ApuntaNotas\Views\CategoryEditorView.xaml.cs:line 18 PresentationFramework.dll 中出现“System.NullReferenceException”类型的第一次机会异常