我正在寫使用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