Sto scrivendo un'applicazione WPF 4 (con VS2010 RC) utilizzando MVVM Luce V3 alpha 3 e sono in esecuzione in un comportamento strano qui ...CanExecute su RelayCommand <T> non funziona
Ho un comando che apre un Window
, e che Window crea il ViewModel e così via - niente di strano lì.
In quel Window
mi hanno alcuni RelayCommand
s, per esempio:
CategoryBeenSelected = new RelayCommand(() => OnCategoryUpdate = true);
Niente di strano ancora una volta - che funziona come mi aspettavo.
Il problema è che non riesco ad avere un'espressione di metodo/lambda CanExecute con un RelayCommand generico.
Questo funziona:
DeleteCategoryCommand = new RelayCommand<int>(DeleteCategory);
Ma questo non lo fa:
DeleteCategoryCommand = new RelayCommand<int>(DeleteCategory, CanDeleteCategory);
La finestra non si presenta. Voglio dire, si fa clic sul pulsante che apre la finestra, e l'applicazione appena viene bloccato e alcuni secondi dopo, il metodo della finestra InitializeComponent
getta un (riferimento non impostato a un'istanza di un oggetto) NullReferenceException
In breve, se Ho inserito un metodo su un RelayCommand<T>
, il Window
che possiede che ViewModel (con RelayCommand<T>
) non può essere istanziato. Se rimuovo lo CanExecute
, viene visualizzato lo Window
.
Dov'è il problema qui? Non ho capito bene.
Grazie.
EDIT: Come richiesto, ecco la traccia dello stack:
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
Forse potresti attaccare una traccia dello stack? Potrebbe aiutare a capire cosa è andato storto. – Vlad
Scusa, ho dimenticato che, eccolo :) –
È strano: Reflector dice che la funzione 'CanExecute' è definita in questo modo:' public bool CanExecute (parametro oggetto) {return (this._canExecute == null) | | this._canExecute ((T) parametro)); } '. Non c'è nulla da gettare un'eccezione. – Vlad