我正在使用MVVM Light V3 alpha 3编写一个WPF 4应用程序(使用VS2010 RC),并且在这里遇到了一些奇怪的行为......
我有一个打开a的命令,Window
那个Window创建了ViewModel等等 - 那里没什么奇怪的.
在那Window
我有一些RelayCommand
s,例如:
CategoryBeenSelected = new RelayCommand(() => OnCategoryUpdate = true);
没有什么奇怪的 - 它按照我的预期工作.
问题是我不能使用通用RelayCommand的CanExecute方法/ lambda表达式.
这有效:
DeleteCategoryCommand = new RelayCommand(DeleteCategory);
但这不是:
DeleteCategoryCommand = new RelayCommand(DeleteCategory, CanDeleteCategory);
窗口没有出现.我的意思是,我单击打开窗口的按钮,应用程序刚刚被阻止,几秒钟后,Window的InitializeComponent
方法抛出一个NullReferenceException
(对象引用未设置为对象的实例)
简而言之,如果我将一个CanExecute
方法放在a上RelayCommand
,Window
那么拥有 ViewModel(带有)的那个RelayCommand
就无法实例化.如果我删除了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
Arcturus.. 7
看来RelayCommand会将参数的值转换为泛型T.
但是你不能将null转换为结构,因为异常告诉你!
如果使用可为空的结构初始化RelayCommand,它将按预期工作!
RelayCommandor RelayCommand >
HTH
看来RelayCommand会将参数的值转换为泛型T.
但是你不能将null转换为结构,因为异常告诉你!
如果使用可为空的结构初始化RelayCommand,它将按预期工作!
RelayCommandor RelayCommand >
HTH