我正在写使用MVVM光V3阿尔法3 WPF应用程序4(与VS2010 RC)和正在运行到这里的一些怪异的行为...CanExecute上RelayCommand <T>不工作
我打开一个命令Window
,并且该窗口创建ViewModel等 - 没有什么奇怪的。再次
CategoryBeenSelected = new RelayCommand(() => OnCategoryUpdate = true);
没什么奇怪的 - 它的工作原理如我所料:
在这种Window
我有一些RelayCommand
S,例如。
问题是我不能有一个通用的RelayCommand CanExecute方法/ lambda表达式。
这工作:
DeleteCategoryCommand = new RelayCommand<int>(DeleteCategory);
但这并不:
DeleteCategoryCommand = new RelayCommand<int>(DeleteCategory, CanDeleteCategory);
窗口不显示。我的意思是,我点击打开窗口的按钮,应用程序就被阻止,几秒钟后,窗口的InitializeComponent
方法抛出一个NullReferenceException
(对象引用未设置为对象的实例)
总之,如果我把CanExecute
方法放在RelayCommand<T>
,Window
那拥有那ViewModel(与RelayCommand<T>
)不能实例化。如果我删除CanExecute
,则显示Window
。
问题在哪里?我很困惑。
谢谢。
编辑:按照要求,这里是堆栈跟踪:
A first chance exception of type 'System.NullReferenceException' occurred in PresentationFramework.dll at GalaSoft.MvvmLight.Command.RelayCommand`1.CanExecute(Object parameter) at System.Windows.Controls.Primitives.ButtonBase.UpdateCanExecute() at System.Windows.Controls.Primitives.ButtonBase.OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e) at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e) at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args) at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType) at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal) at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value) at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(Object inst, XamlMember property, Object value) at MS.Internal.Xaml.Runtime.PartialTrustTolerantRuntime.SetValue(Object obj, XamlMember property, Object value) at System.Xaml.XamlObjectWriter.Logic_ApplyPropertyValue(ObjectWriterContext ctx, XamlMember prop, Object value, Boolean onParent) at System.Xaml.XamlObjectWriter.Logic_DoAssignmentToParentProperty(ObjectWriterContext ctx) at System.Xaml.XamlObjectWriter.WriteEndObject() at System.Windows.Markup.WpfXamlLoader.TransformNodes(XamlReader xamlReader, XamlObjectWriter xamlWriter, Boolean onlyLoadOneNode, Boolean skipJournaledProperties, Boolean shouldPassLineNumberInfo, IXamlLineInfo xamlLineInfo, IXamlLineInfoConsumer xamlLineInfoConsumer, XamlContextStack`1 stack, IStyleConnector styleConnector) at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri) at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri) at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream) at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator) at ApuntaNotas.Views.CategoryEditorView.InitializeComponent() in c:\Users\Jesus\Documents\Visual Studio 2010\Projects\ApuntaNotas\ApuntaNotas\Views\CategoryEditorView.xaml:line 1 at ApuntaNotas.Views.CategoryEditorView..ctor() in C:\Users\Jesus\Documents\Visual Studio 2010\Projects\ApuntaNotas\ApuntaNotas\Views\CategoryEditorView.xaml.cs:line 18 A first chance exception of type 'System.NullReferenceException' occurred in PresentationFramework.dll
也许你可以附加一个堆栈跟踪?这可能有助于了解哪里出了问题。 – Vlad
对不起,我忘记了,它是:) –
这很奇怪:反射器告诉函数'CanExecute'是这样定义的:'public bool CanExecute(object parameter){return(this._canExecute == null)| | this._canExecute((T)parameter)); }'。没有什么可以抛出异常。 – Vlad