निम्नलिखित रूटेड कॉमांड उदाहरण काम करता है।व्यू-कोडबेंड से व्यूमोडेल तक मेरे रूटेड कमांडर हैंडलर को कैसे स्थानांतरित करें?
हालांकि, कमांड को निष्पादित करने वाले बटन के लिए हैंडलिंग के कोडबेंड में है। जिस तरह से मैं एमवीवीएम को समझता हूं, यह व्यूमोडेल में होना चाहिए।
हालांकि, जब मैं विधि को व्यूमोडेल (और इसे सार्वजनिक रूप से बदलता हूं) में ले जाता हूं, तो मुझे त्रुटि मिलती है "प्रबंधित क्लाइंटर्स व्यू में ऑनसेव की परिभाषा नहीं है"। यहां तक कि अगर मैं टाइप करने के लिए रूटेड कॉमांड दूसरा पैरामीटर बदलता हूं (ManageCustomersViewModel), मुझे एक ही त्रुटि मिलती है।
मैं दृश्य-कोडबेंड से व्यू हैंडलर को व्यूमोडेल में कैसे स्थानांतरित कर सकता हूं?
ManageCustomersView.xaml:
<UserControl.CommandBindings>
<CommandBinding Command="local:Commands.SaveCustomer" Executed="OnSave"/>
</UserControl.CommandBindings>
...
<Button Style="{StaticResource formButton}"
Content="Save"
Command="local:Commands.SaveCustomer"
CommandParameter="{Binding Id}"/>
ManageCustomersView.xaml.cs:
private void OnSave(object sender
, System.Windows.Input.ExecutedRoutedEventArgs e)
{
int customerId = ((int)e.Parameter);
MessageBox.Show(String.Format
("You clicked the save button for customer with id {0}.", customerId));
}
Commands.cs:
using System.Windows.Input;
using TestDynamicForm123.View;
namespace TestDynamicForm123
{
public class Commands
{
public static RoutedCommand SaveCustomer =
new RoutedCommand("SaveCustomer", typeof(ManageCustomersView));
}
}
हम्म, मैंने कोड के उन दो बिट जोड़े और अब मेरे बटन पर क्लिक करने से कुछ भी नहीं है। मैं UserControl.CommandBindings निकाल सकता हूं या इसे छोड़ सकता हूं लेकिन इसका कोई प्रभाव नहीं पड़ता है। आउटपुट में कुछ भी नहीं। मुझे और बदलने की क्या ज़रूरत है? –
रूटेड कॉमांड पर एक नज़र डालें जो इसे बहुत आसान बनाता है। आप अपने दृष्टिकोण मॉडल के भीतर से नए रूटेड कॉमांड (पी => संदेश बॉक्स। शो ("सेविंग।") वापस कर सकते हैं। –
मुझे लगता है कि पिछली टिप्पणीकर्ता का मतलब रिलेकॉमैंड था। –