मैं एक WPF 4 एप्लिकेशन (VS2010 आर सी) के साथ MVVM लाइट वी 3 अल्फा 3 का उपयोग लिख रहा हूँ और कुछ अजीब व्यवहार यहाँ में चल रहा हूँ ...CanExecute RelayCommand <T> पर काम नहीं कर
मैं एक कमांड एक खुलती है Window
, और वह विंडो ViewModel बनाता है और इसी तरह - वहां कुछ भी अजीब नहीं है। फिर
CategoryBeenSelected = new RelayCommand(() => OnCategoryUpdate = true);
कुछ भी नहीं अजीब - यह काम करता है के रूप में मैं उम्मीद:
कि Window
में मैं उदाहरण के लिए कुछ RelayCommand
रों है,।
समस्या यह है कि मेरे पास एक सामान्य रिलेकॉमैंड के साथ CanExecute विधि/लैम्ब्डा अभिव्यक्ति नहीं हो सकती है।
यह काम करता है:
DeleteCategoryCommand = new RelayCommand<int>(DeleteCategory);
लेकिन यह नहीं करता है:
DeleteCategoryCommand = new RelayCommand<int>(DeleteCategory, CanDeleteCategory);
विंडो दिखाई नहीं देता है। मेरा मतलब है, मैं बटन खुलने वाली विंडो क्लिक करें, और एप्लिकेशन सिर्फ अवरुद्ध हो जाता है और कुछ सेकंड बाद, खिड़की के InitializeComponent
विधि
संक्षेप में, अगर एक NullReferenceException
(एक वस्तु का एक उदाहरण के लिए सेट नहीं ऑब्जेक्ट संदर्भ) फेंकता मैंने RelayCommand<T>
पर Window
पर का उपयोग किया है जो ViewModel (RelayCommand<T>
के साथ) को तत्काल नहीं किया जा सकता है। अगर मैं 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
शायद आप एक स्टैक ट्रेस संलग्न कर सकते हैं? यह समझने में मदद कर सकता है कि क्या गलत हुआ। – Vlad
क्षमा करें, मैं भूल गया हूं, वहां है :) –
यह अजीब बात है: प्रतिबिंबक बताता है कि 'CanExecute' फ़ंक्शन को इस तरह से परिभाषित किया गया है:' सार्वजनिक बूल CanExecute (ऑब्जेक्ट पैरामीटर) {वापसी (this._canExecute == null) | | this._canExecute ((टी) पैरामीटर)); } '। अपवाद फेंकने के लिए कुछ भी नहीं है। – Vlad