CanExecuteonReExCommandNotWorkingBehavior...在那个窗口中,我有一些RelayCommand,例如:CategoryBeenSelected=newRelayCommand(()=>OnCategoryUpdate=true);没什么奇怪的-它按我的预期工作。问题是我不能使用通用RelayCommand的CanExecute方法/lambda表达式。这有效:DeleteCategoryCommand=newRelayCommand(DeleteCategory);但这不是:DeleteCategoryCommand=newRelayCommand(DeleteCategory,CanDeleteCategory);窗口没有出现。我的意思是,我单击打开窗口的按钮,应用程序只是阻塞,几秒钟后,Window的InitializeComponent方法抛出NullReferenceException(对象引用未设置为对象的实例),然后在CanExecute方法上放一个CanExecute方法无法实例化拥有ViewModel(带有RelayCommand)的Window。如果我删除CanExecute,CanExecute会显示窗口。这里有什么问题?我很困惑。谢谢。编辑:根据要求,这里是堆栈跟踪:在System.Windows.Controls的GalaSoft.MvvmLight.Command.RelayCommand`1.CanExecute(Objectparameter)的PresentationFramework.dll中发生类型为“System.NullReferenceException”的第一次机会异常。Primitives.ButtonBase.UpdateCanExecute()在System.Windows.Controls.Primitives.ButtonBase.OnCommandChanged(DependencyObjectd,DependencyPropertyChangedEventArgse)在System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgse)在System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgse)在System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgsargs)在System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndexentryIndex,DependencyPropertydp,PropertyMetadatametadata,EffectiveValueEntryoldEntry,EffectiveValueEntry&newEntry,BooleancoerceWithDeferredReference,BooleancoerceWithCurrentValues,在OptionalcoerceWithCurrentValues依赖对象ject.SetValueCommon(DependencyPropertydp,对象值,PropertyMetadata元数据,BooleancoerceWithDeferredReference,BooleancoerceWithCurrentValue,OperationTypeoperationType,BooleanisInternal)在System.Windows.DependencyObject.SetValue(DependencyPropertydp,对象值)在MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(Objectinst,XamlMember属性,Objectvalue)在MS.Internal.Xaml.Runtime.PartialTrustTolerantRuntime.SetValue(Objectobj,XamlMember属性,Objectvalue)在System.Xaml.XamlObjectWriter.Logic_ApplyPropertyValue(ObjectWriterContextctx,XamlMemberprop,Objectvalue,BooleanonParent)在System.Xaml.XamlObjectWriter.Logic_DoAssignmentToParentProperty(ObjectWriterContextctx)在System.Xaml.XamlObjectWriter.WriteEndObject()在System.Windows.Markup.WpfXamlLoader.TransformNodes(XamlReaderxamlReader,XamlObjectWriterxamlProdLoader,NotiesonlyBooleanshouldPassLineNumberInfo,IXamlLineInfoxamlLineInfo,IXamlLineInfoConsumerxamlLineInfoConsumer,XamlContextStack`1stack,IStyleConnectorstyleConnector)在System.Windows.Markup.WpfXamlLoader.Load(XamlReaderxamlReader,IXamlObjectWriterFactorywriterFactory,BooleanskipJournaledProperties,ObjectrootObject,XamlObjectWriterSettingssettings,UribaseUri)在System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReaderxamlReader,BooleanskipJournaledProperties,ObjectrootObject,XamlAccessLevelaccessLevel,UribaseUri)在System.Windows.Markup.XamlReader.LoadBaml(Stream流,ParserContextparserContext,Objectparent,BooleancloseStream)在System.Windows.Application.LoadComponent(对象组,UriresourceLocator)在c:UsersJesusDocumentsVisualStudio2010ProjectsApuntaNotasApuntaNotasViewsCategoryEditorView.xaml:第1行中的ApuntaNotas.Views.CategoryEditorView.InitializeComponent()在C:UsersJesusDocumentsVisualStudio2010ProjectsApuntaNotasApuntaNotasViews分类编辑器View.xaml.cs中的ApuntaNotas.Views.CategoryEditorView..ctor():第18行的PresentationFramework.dll中出现类型为“System.NullReferenceException”的第一次机会异常似乎RelayCommand将参数的值转换为genericsT。但是你不能将null转换为结构,因为异常告诉你!如果您使用可为空的结构初始化RelayCommand,它会按预期工作!RelayCommand或RelayCommand>HTHArcturus在确定问题所在方面是正确的,但我不喜欢使用可空基元的解决方案我个人不喜欢可空基元,除非我有充分的理由使用它们。相反,我更改了RelayCommand的实现,如下所示:boolICommand.CanExecute(objectparameter){if(parameter==null&&typeof(T).IsValueType){returnCanExecute(default(T));}返回CanExecute((T)参数);我没有对通用的Execute方法进行相同的更改(至少到目前为止),因为如果命令确实需要参数,我认为在这种情况下失败是不合理的。CanExecute的问题是WPF系统有时会在计算某些绑定之前调用它。例如:在上面的XAML中,您注意到命令参数绑定到控件的实际宽度。但是,WPF将在必须布局/呈现“imageScrollViewer”控件之前对按钮的命令调用CanExecute-因此没有实际的宽度/高度。当用户单击按钮并调用Execute时,布局控件以便将值发送到命令。如果不是——我认为失败是预料之中的——但只有当用户实际点击按钮时。当然,我不喜欢CanExecute和Execute的不同行为,但现在它似乎符合框架强加的约束。我可能会发现让我难过的情况,但我一直喜欢这种变化。也许,此时,参数为null?以上就是C#学习教程的全部内容:CanExecuteonReExCommand无法正常工作。如果对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
