Estoy escribiendo una aplicación de WPF 4 (con VS2010 RC) usando MVVM Light V3 alpha 3 y estoy corriendo en algún comportamiento extraño aquí ...CanExecute en RelayCommand <T> no trabajar
tengo un comando que abre una Window
, y esa ventana crea el ViewModel, etc., nada raro allí.
En ese Window
Tengo algunas RelayCommand
s, por ejemplo:
CategoryBeenSelected = new RelayCommand(() => OnCategoryUpdate = true);
Nada raro otra vez - que funciona como esperaba.
El problema es que no puedo tener un método CanExecute/expresión lambda con un RelayCommand genérico.
Esto funciona:
DeleteCategoryCommand = new RelayCommand<int>(DeleteCategory);
Pero esto no es así:
DeleteCategoryCommand = new RelayCommand<int>(DeleteCategory, CanDeleteCategory);
La ventana no aparece. Es decir, hacer clic en el botón que abre la ventana, y la aplicación sólo se bloquea y unos segundos más tarde, el método de la ventana InitializeComponent
lanza una (Referencia a objeto no establecida como instancia de un objeto) NullReferenceException
En resumen, si Puse un método CanExecute
en un RelayCommand<T>
, Window
que posee que no se puede crear una instancia de ViewModel (con el RelayCommand<T>
). Si elimino el CanExecute
, aparece el Window
.
¿Dónde está el problema aquí? Estoy confundido.
Gracias.
EDIT: Conforme a lo solicitado, aquí está el seguimiento de la pila:
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
¿Tal vez podría adjuntar un rastro de pila? Podría ayudar a entender qué salió mal. – Vlad
Lo siento, lo olvidé, ahí está :) –
Es extraño: Reflector dice que la función 'CanExecute' se define de tal manera:' public bool CanExecute (parámetro de objeto) {return (this._canExecute == null) | | this._canExecute ((T) parámetro)); } ' No hay nada para arrojar una excepción. – Vlad