MVVM Light V3 alpha3を使用してWPF4アプリ(VS2010 RCを使用)を作成していますが、ここで奇妙な動作が発生しています...
を開くコマンドがありWindow
、そのウィンドウがViewModelなどを作成します-そこには何も奇妙なことはありません。
その中でWindow
私はいくつかRelayCommand
の例を持っています:
CategoryBeenSelected = new RelayCommand(() => OnCategoryUpdate = true);
再び奇妙なことは何もありません-それは私が期待したように動作します。
問題は、汎用のRelayCommandでCanExecuteメソッド/ラムダ式を使用できないことです。
これは機能します:
DeleteCategoryCommand = new RelayCommand<int>(DeleteCategory);
しかし、これはしません:
DeleteCategoryCommand = new RelayCommand<int>(DeleteCategory, CanDeleteCategory);
ウィンドウが表示されません。つまり、ウィンドウを開くボタンをクリックすると、アプリがブロックされ、数秒後に、ウィンドウのメソッドが(オブジェクトのインスタンスに設定されていないオブジェクト参照)InitializeComponent
をスローします。NullReferenceException
つまり、CanExecute
メソッドをに配置するRelayCommand<T>
と、そのViewModel(を含む)Window
を所有するRelayCommand<T>
はインスタンス化できません。を削除するCanExecute
と、がWindow
表示されます。
ここで問題はどこにありますか?よくわかりません。
ありがとうございました。
編集:要求に応じて、ここにスタックトレースがあります:
タイプ'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 args)で System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex、DependencyProperty dp、PropertyMetadataメタデータ、EffectiveValueEntry oldEntry、EffectiveValueEntry&newEntry、ブールcoerceWithDeferredReference、ブールcoerceWithCurrentValue、OperationType operationType) System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp、Object value、PropertyMetadataメタデータ、ブールcoerceWithDeferredReference、ブールcoerceWithCurrentValue、OperationType operationType、ブールisInternal)で System.Windows.DependencyObject.SetValue(DependencyProperty dp、Object value)で MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(Object inst、XamlMember property、Object value)で MS.Internal.Xaml.Runtime.PartialTrustTolerantRuntime.SetValue(Object obj、XamlMember property、Object value)で System.Xaml.XamlObjectWriter.Logic_ApplyPropertyValue(ObjectWriterContext ctx、XamlMember prop、Object value、Boolean onParent)で System.Xaml.XamlObjectWriter.Logic_DoAssignmentToParentProperty(ObjectWriterContext ctx)で System.Xaml.XamlObjectWriter.WriteEndObject()で System.Windows.Markup.WpfXamlLoader.TransformNodes(XamlReader xamlReader、XamlObjectWriter xamlWriter、Boolean onlyLoadOneNode、Boolean skipJournaledProperties、Boolean shouldPassLineNumberInfo、IXamlLineInfo xamlLineInfo、IXamlLineInfoConsumer xamlLineInfoConsumer、XamlContextStack System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader、IXamlObjectWriterFactory writerFactory、Boolean skipJournaledProperties、Object rootObject、XamlObjectWriterSettings settings、Uri baseUri)で System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader、Boolean skipJournaledProperties、Object rootObject、XamlAccessLevel accessLevel、Uri baseUri)で System.Windows.Markup.XamlReader.LoadBaml(ストリームストリーム、ParserContext parserContext、オブジェクトの親、ブール値closeStream)で System.Windows.Application.LoadComponent(Object component、Uri resourceLocator)で c:\ Users \ Jesus \ Documents \ Visual Studio 2010 \ Projects \ ApuntaNotas \ ApuntaNotas \ Views \ CategoryEditorView.xaml:line 1のApuntaNotas.Views.CategoryEditorView.InitializeComponent() C:\ Users \ Jesus \ Documents \ Visual Studio 2010 \ Projects \ ApuntaNotas \ ApuntaNotas \ Views \ CategoryEditorView.xaml.cs:line 18のApuntaNotas.Views.CategoryEditorView..ctor() タイプ'System.NullReferenceException'の最初のチャンスの例外がPresentationFramework.dllで発生しました