Tôi đang viết một ứng dụng WPF 4 (với VS2010 RC) sử dụng MVVM nhẹ V3 alpha 3 và đang chạy vào một số hành vi kỳ lạ ở đây ...CanExecute trên RelayCommand <T> không làm việc
Tôi có một lệnh mở ra một Window
và Cửa sổ đó tạo ViewModel và v.v. - không có gì lạ ở đó.
Trong Window
rằng tôi có một số RelayCommand
s, ví dụ:
CategoryBeenSelected = new RelayCommand(() => OnCategoryUpdate = true);
Không có gì lạ một lần nữa - nó hoạt động như tôi mong đợi.
Vấn đề là tôi không thể có biểu thức CanExecute method/lambda với một RelayCommand chung.
này hoạt động:
DeleteCategoryCommand = new RelayCommand<int>(DeleteCategory);
Nhưng điều này không:
DeleteCategoryCommand = new RelayCommand<int>(DeleteCategory, CanDeleteCategory);
Window không hiển thị. Ý tôi là, tôi nhấp vào nút mở cửa sổ và ứng dụng sẽ bị chặn và một vài giây sau, phương thức InitializeComponent
của Window sẽ ném một số NullReferenceException
(Tham chiếu đối tượng không được đặt thành thể hiện của đối tượng)
Tóm lại, Nếu Tôi đặt một phương thức CanExecute
trên RelayCommand<T>
, Window
rằng sở hữu rằng ViewModel (với RelayCommand<T>
) không thể được khởi tạo. Nếu tôi xóa CanExecute
, thì Window
sẽ hiển thị.
Sự cố ở đây ở đâu? Tôi bối rối.
Cảm ơn bạn.
EDIT: Theo yêu cầu, đây là 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
Có thể bạn có thể đính kèm theo dõi ngăn xếp? Nó có thể giúp hiểu những gì đã xảy ra. – Vlad
Xin lỗi, tôi quên rằng, có nó là :) –
Thật lạ: Reflector nói rằng hàm 'CanExecute' được định nghĩa theo cách như sau:' public bool CanExecute (tham số đối tượng) {return (this._canExecute == null) | | this._canExecute ((T) tham số)); } '. Không có gì để ném một ngoại lệ. – Vlad