CanExecute上RelayCommand< T>不工作 [英] CanExecute on RelayCommand<T> not working

查看:240
本文介绍了CanExecute上RelayCommand< T>不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写使用MVVM光V3阿尔法3 WPF应用程序4(与VS2010 RC)和我遇到了一些奇怪的行为在这里...



我有打开一个窗口,并在该窗口创建视图模型等命令 - 没什么奇怪的还有



在该窗口我有一些 RelayCommand S,例如:

  CategoryBeenSelected =新RelayCommand(()=> OnCategoryUpdate = TRUE); 



没有,没有奇怪的 - 它的工作原理如我所料


$ B $ :b

现在的问题是,我不能有一个通用的RelayCommand一个CanExecute方法/ lambda表达式



本作品:

  DeleteCategoryCommand =新RelayCommand< INT>(DeleteCategory); 



但是,这并不:

  DeleteCategoryCommand =新RelayCommand< INT>(DeleteCategory,CanDeleteCategory); 



窗口不显示。我的意思是,我点击打开的窗口按钮,该应用程序只是获取受阻,几秒钟后,该窗口的的InitializeComponent 方法抛出的NullReferenceException (不设置到对象的实例对象引用)



在短期,如果我把一个 CanExecute 的方法一个 RelayCommand< T> 窗口拥有的那视图模型(用 RelayCommand< T> )不能被实例化。如果我删除 CanExecute 窗口显示出来。



如果这里的问题? 。我很困惑



感谢您



编辑:按照要求,这里是堆栈跟踪:

 
型System.NullReferenceException的第一次机会异常出现在PresentationFramework.dll
在GalaSoft.MvvmLight.Command.RelayCommand` 1.CanExecute(Object参数)
在System.Windows.Controls.Primitives.ButtonBase.UpdateCanExecute()
在System.Windows.Controls.Primitives.ButtonBase.OnCommandChanged(DependencyObject的D,DependencyPropertyChangedEventArgs E)
在System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs E)
在System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs E)
在System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs参数)
在System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex,DP的DependencyProperty,PropertyMetadata元,EffectiveValueEntry oldEntry,EffectiveValueEntry和新条目,布尔coerceWithDeferredReference,布尔coerceWithCurrentValue,OperationType operationType)
在System.Windows.DependencyObject.SetValueCommon(DP的DependencyProperty,对象值,PropertyMetadata元数据,布尔coerceWithDeferredReference,布尔coerceWithCurrentValue,OperationType operationType,布尔isInternal)
在System.Windows.DependencyObject.SetValue(DP的DependencyProperty,对象的值)
在MS.Internal.Xaml.Runtime.ClrObjectRuntime .SetValue(对象研究所,XamlMember属性,对象值)MS.Internal.Xaml.Runtime.PartialTrustTolerantRuntime.SetValue
(obj对象,XamlMember属性,对象的值)
在System.Xaml.XamlObjectWriter.Logic_ApplyPropertyValue在System.Xaml.XamlObjectWriter.WriteEndObject(ObjectWriterContext CTX,XamlMember道具,对象的值,布尔onParent)在System.Xaml.XamlObjectWriter.Logic_DoAssignmentToParentProperty
(ObjectWriterContext CTX)
()
的系统。 Windows.Markup.WpfXamlLoader.TransformNodes(xamlReader xamlReader,XamlObjectWriter的XamlWriter,布尔onlyLoadOneNode,布尔skipJournaledProperties,布尔shouldPassLineNumberInfo,IXamlLineInfo xamlLineInfo,IXamlLineInfoConsumer xamlLineInfoConsumer,XamlContextStack`1栈,IStyleConnector styleConnector)
在System.Windows.Markup.WpfXamlLoader。负载(xamlReader xamlReader,IXamlObjectWriterFactory writerFactory,布尔skipJournaledProperties,对象rootObject,XamlObjectWriterSettings设置,开放的基本URI)
在System.Windows.Markup.WpfXamlLoader.LoadBaml(xamlReader xamlReader,布尔skipJournaledProperties,对象rootObject,XamlAccessLevel ACCESSLEVEL,乌里基本URI)
在System.Windows.Markup.XamlReader.LoadBaml(流流,ParserContext parserContext,对象的父母,布尔closeStream)
在System.Windows.Application.LoadComponent(对象组件,乌里resourceLocator)
在ApuntaNotas.Views.CategoryEditorView.InitializeComponent(c)中:\Users\Jesus\Documents\Visual工作室2010\Projects\ApuntaNotas\ApuntaNotas\Views\CategoryEditorView.xaml:1号线
。在ApuntaNotas.Views.CategoryEditorView..ctor()在C:\Users\Jesus\Documents\Visual工作室2010\Projects\ApuntaNotas\ApuntaNotas\Views\CategoryEditorView.xaml.cs:行18
型System.NullReferenceException的第一次机会异常出现在PresentationFramework.dll


解决方案

看来,RelayCommand将参数铸就价值的通用T.



但你不能投空到一个结构,作为异常告诉你!



如果您使用可空结构初始化RelayCommand,它将按预期工作!

  RelayCommand<&诠释GT?;或RelayCommand<可空< INT>> 



心连心


I'm writing a WPF 4 app (with VS2010 RC) using MVVM Light V3 alpha 3 and am running into some weird behaviour here...

I have a command that opens a Window, and that Window creates the ViewModel and so on - nothing weird there.

In that Window I have some RelayCommands, for example:

CategoryBeenSelected = new RelayCommand(() => OnCategoryUpdate = true);

Nothing weird again - it works as I expected.

The problem is that I cannot have a CanExecute method / lambda expression with a generic RelayCommand.

This works:

DeleteCategoryCommand = new RelayCommand<int>(DeleteCategory);

But this does not:

DeleteCategoryCommand = new RelayCommand<int>(DeleteCategory, CanDeleteCategory);

The Window doesn't show up. I mean, I click the button that opens the window, and the app just gets blocked and some seconds later, The Window's InitializeComponent method throws a NullReferenceException (Object reference not set to an instance of an object)

In short, If I put a CanExecute Method on a RelayCommand<T>, the Window that owns that ViewModel (with the RelayCommand<T>) can't be instantiated. If I remove the CanExecute, the Window shows up.

Where is the problem here? I'm confused.

Thank you.

EDIT: As requested, here is the 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

解决方案

It seems that the RelayCommand will cast the value the parameter to the generic T.

But you cannot cast a null to a struct, as the exception tells you!

If you initialize the RelayCommand with a nullable struct, it will work as expected!

RelayCommand<int?> or RelayCommand<Nullable<int>>

HTH

这篇关于CanExecute上RelayCommand&LT; T&GT;不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆