I'm writing a WPF 4 app (with VS2010 RC) using MVVM Light V3 alpha 3 and am running into some weird behaviour here...
I have a command that opens a Window
, and that Window creates the ViewModel and so on - nothing weird there.
In that Window
I have some RelayCommand
s, for example:
CategoryBeenSelected = new RelayCommand(() => OnCategoryUpdate = true);
Nothing weird again - it works as I expected.
The problem is that I cannot have a CanExecute method / lambda expression with a generic RelayCommand.
This works:
DeleteCategoryCommand = new RelayCommand<int>(DeleteCategory);
But this does not:
DeleteCategoryCommand = new RelayCommand<int>(DeleteCategory, CanDeleteCategory);
The Window doesn't show up. I mean, I click the button that opens the window, and the app just gets blocked and some seconds later, The Window's InitializeComponent
method throws a NullReferenceException
(Object reference not set to an instance of an object)
In short, If I put a CanExecute
Method on a RelayCommand<T>
, the Window
that owns that ViewModel (with the RelayCommand<T>
) can't be instantiated. If I remove the CanExecute
, the Window
shows up.
Where is the problem here? I'm confused.
Thank you.
EDIT: As requested, here is the stack trace:
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
CanExecute
is defined in such a way:public bool CanExecute(object parameter) { return (this._canExecute == null) || this._canExecute((T) parameter)); }
. There is nothing to throw an exception. – Ketubimnull
? – Louralourdes