2015-11-10 6 views
7

हाय शुभ दिन मैं डब्ल्यूपीएफ और एमवीवीएम डिजाइन पैटर्न के साथ बस नया हूं और मैंने प्रिंस में सर ब्रायन लैगुनस के ब्लॉग और वीडियो से बहुत कुछ सीखा है .. लेकिन सिर्फ एक नोब सवाल पूछना है .. मेरे कोड के साथ गलत है यह मेरे लिए काम करता है ... किसी भी मदद की बहुत सराहना की है धन्यवाद। यहाँ मेरी कोड है:प्रिज्म 6 प्रतिनिधिमंडल पर्यवेक्षणप्रदर्शन कोड

मेरे विचार मॉडल

public class Person : BindableBase 
{ 
    private myPErson _MyPerson; 
    public myPErson MyPerson 
    { 
     get { return _MyPerson; } 
     set 
     { 
      SetProperty(ref _MyPerson, value); 
     } 
    } 

    public Person() 
    { 
     _MyPerson = new myPErson(); 
     updateCommand = new DelegateCommand(Execute, CanExecute).ObservesProperty(() => MyPerson.FirstName).ObservesProperty(() => MyPerson.Lastname); 

    // updateCommand = new DelegateCommand(Execute).ObservesCanExecute((p) => CanExecute); /// JUST WANNA TRY THIS BUT DUNNO HOW 
    } 

    private bool CanExecute() 
    { 
     return !String.IsNullOrWhiteSpace(MyPerson.FirstName) && !String.IsNullOrWhiteSpace(MyPerson.Lastname); 
    } 

    private void Execute() 
    { 
     MessageBox.Show("HOLA"); 
    } 

    public DelegateCommand updateCommand { get; set; } 
} 

myModel

एक और वर्ग फ़ाइल को घोषित

public class myPErson : BindableBase 
{ 
    private string _firstName; 
    public string FirstName 
    { 
     get { return _firstName; } 
     set 
     { 
      SetProperty(ref _firstName, value); 
     } 
    } 

    private string _lastname; 
    public string Lastname 
    { 
     get { return _lastname; } 
     set 
     { 
      SetProperty(ref _lastname, value); 
     } 
    } 
} 

देखें Xaml सह डी

<Window x:Class="Prism6Test.Views.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     xmlns:myVM="clr-namespace:Prism6Test.ViewModel" 
     Title="MainWindow" Height="350" Width="525"> 
    <Window.Resources> 
     <myVM:Person x:Key="mainVM"/> 
    </Window.Resources> 
<Grid DataContext="{StaticResource mainVM}"> 
     <TextBox HorizontalAlignment="Left" Height="23" Margin="217,103,0,0" TextWrapping="Wrap" Text="{Binding MyPerson.FirstName,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/> 
     <TextBox HorizontalAlignment="Left" Height="23" Margin="217,131,0,0" TextWrapping="Wrap" Text="{Binding MyPerson.Lastname,UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Top" Width="120"/> 
     <Button Content="Button" Command="{Binding updateCommand}" HorizontalAlignment="Left" Margin="242,159,0,0" VerticalAlignment="Top" Width="75"/> 

    </Grid> 
</Window> 

मैंने पहले से ही इस पढ़ा है, लेकिन यह मेरे लिए काम नहीं करता है .. और नहीं कर सकते समझ कैसे मैं ठीक से यह कोड सकते हैं .. कृपया मेरी मदद किसी भी उत्तर soon..thx

के लिए इस मामले ..HOPE ragarding

ObservesProperty method isn't observing model's properties at Prism 6

+0

बटन क्या कर रहा है याद कर रहे हैं? – Jens

+0

संदेश बॉक्स "होला" पोस्ट करें। – Neil

+0

ठीक है। अब मैं इसे देखता हूँ। कोड की समस्या क्या है? आप टेक्स्टबॉक्स में कुछ नहीं देखते हैं? – Jens

उत्तर

4

1) आप जटिल डेटामॉडल का उपयोग नहीं कर सकते हैं की तरह आप चाहते हैं, तो यह

private myPErson _MyPerson; 
    public myPErson MyPerson 
    { 
     get { return _MyPerson; } 
     set 
     { 
      if (_MyPerson != null) 
       _MyPerson.PropertyChanged -= MyPersonOnPropertyChanged; 

      SetProperty(ref _MyPerson, value); 


      if (_MyPerson != null) 
       _MyPerson.PropertyChanged += MyPersonOnPropertyChanged; 
     } 
    } 

    private void MyPersonOnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs) 
    { 
     updateCommand.RaiseCanExecuteChanged(); 
    } 

2 की कोशिश) अपने निर्माता बदलें

+०१२३५१६४१०६१
public Person() 
    { 
     MyPerson = new myPErson(); 
     updateCommand = new DelegateCommand(Execute, CanExecute); 
    } 
+0

धन्यवाद .. !!! यह मेरे लिए अब काम करता है सर @galakt .. – Neil

+0

@Neil इस उत्तर को समाधान के रूप में चिह्नित करें, अगर यह आपको – galakt

3

सबसे पहले मुझे आपके नामकरण के बारे में कुछ कहना है। अपने वर्गों को स्पष्ट करें। अपने पर कॉल करें ViewModel उदा। PersonViewModel या सिर्फ ViewModel जब आपका एप्लिकेशन इतना बड़ा नहीं है और Person नहीं है, क्योंकि Person स्पष्ट रूप से मॉडल है। इसके अलावा myPErson एक बहुत बुरा नाम है, क्योंकि यह आपके अन्य Person वर्ग के समान है और आपको PascalCase अपने क्लास नाम होना चाहिए।

अब आपके कोड पर। मुझे Prism के बारे में कुछ भी नहीं पता है, इसलिए मेरा कोड केवल प्रिज्म पुस्तकालयों के समर्थन के बिना MVVM पैटर्न पर निर्भर करता है।

सबसे पहले मैं मॉडलPerson बदलना चाहता हूं। वर्ग मेरी कोड में बहुत आसान लग रहा है (बस ऑटो गुण का उपयोग करता है): क्योंकि यह INotifyPropertyChanged इंटरफ़ेस लागू करता

public class Person 
{ 
    public string FirstName { get; set; } 

    public string LastName { get; set; } 

    public Person() 
    { 
     this.LastName = string.Empty; 
     this.FirstName = string.Empty; 
    } 
} 

PersonViewModel एक littlebit ज्यादा जटिल है। मैं बहुत आम RelayCommand का भी उपयोग कर रहा हूं जिसे आप स्वीकृत उत्तर के तहत लिंक में पा सकते हैं।

public class PersonViewModel : INotifyPropertyChanged 
{ 
    private Person person; 

    private ICommand updateCommand; 

    public PersonViewModel() 
    { 
     this.Person = new Person(); 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    public Person Person 
    { 
     get 
     { 
      return this.person; 
     } 

     set 
     { 
      this.person = value; 

      // if you use VS 2015 or/and C# 6 you also could use 
      // this.OnPropertyChanged(nameof(Person)); 
      this.OnPropertyChanged("Person"); 
     } 
    } 

    public ICommand UpdateCommand 
    { 
     get 
     { 
      if (this.updateCommand == null) 
      { 
       this.updateCommand = new RelayCommand<Person>(this.OpenMessageBox, this.OpenMessageBoxCanExe); 
      } 

      return this.updateCommand; 
     } 
    } 

    [NotifyPropertyChangedInvocator] 
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) 
    { 
     PropertyChangedEventHandler handler = this.PropertyChanged; 
     if (handler != null) 
     { 
      handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 
    } 

    private void OpenMessageBox(Person person) 
    { 
     MessageBox.Show("Hola"); 
    } 

    private bool OpenMessageBoxCanExe(Person person) 
    { 
     if (person == null) 
     { 
      return false; 
     } 

     if (string.IsNullOrWhiteSpace(person.FirstName) || string.IsNullOrWhiteSpace(person.LastName)) 
     { 
      return false; 
     } 

     return true; 
    } 
} 

मैंने आपके दृश्य को थोड़ा सा साफ़ कर दिया, क्योंकि यह अब बहुत छोटा है। लेकिन सबकुछ सब कुछ वही रहा। मैं सिर्फ गुण और सामान का नाम बदला:

<Window ...> 
    <Window.Resources> 
     <wpfTesst:PersonViewModel x:Key="ViewModel" /> 
    </Window.Resources> 
    <Grid DataContext="{StaticResource ViewModel}"> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="Auto"/> 
     </Grid.RowDefinitions> 
     <TextBox Grid.Row="0" TextWrapping="Wrap" Text="{Binding Person.FirstName, UpdateSourceTrigger=PropertyChanged}" /> 
     <TextBox Grid.Row="1" TextWrapping="Wrap" Text="{Binding Person.LastName, UpdateSourceTrigger=PropertyChanged}" /> 
     <Button Grid.Row="2" Content="Button" Command="{Binding UpdateCommand}" CommandParameter="{Binding Person}"/> 
    </Grid> 
</Window> 

सभी सब मैं प्रिज्म लाइब्रेरी के बिना आम MVVM पैटर्न का उपयोग करने के लिए आप के लिए सिफारिश करेंगे में।जब आप एमवीवीएम को समझते थे तो भी आप प्रिज्म के लिए जा सकते हैं। उम्मीद है कि यह मदद करता है।

+0

मदद करता है तो आपके सुझाव के लिए धन्यवाद सर @ जेन्स होर्स्टमान..और इससे मुझे मेरी कोडिंग में सुधार करने में मदद मिलेगी .. जिज्ञासा के बिना PRISM लाइब्रेरी का उपयोग करना चाहते हैं और इसके संबंध में अध्ययन करना चाहते हैं .. क्या मैं आपसे एक लिंक पूछ सकता हूं, मेरे संदर्भ सर के लिए किताब? भविष्य के अध्ययन के लिए .. कृपया कृपया ... thx बहुत अधिक – Neil

+0

@Neil जो आप अपने लिंक के साथ चाहते हैं = पी – Jens

+0

हाहा .. आपके भविष्य के संदर्भ के लिए सर^_^.... से कोई सुझाव दिया गया लिंक .. पढ़ाई .. कृपया कृपया ... @ जेन्स हॉर्स्टमैन – Neil

0

देखें XAML कोड

<Window x:Class="Prism6Test.Views.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:myVM="clr-namespace:Prism6Test.ViewModel" 
    Title="MainWindow" Height="350" Width="525"> 

आप ViewModelLocator

xmlns:prism="clr-namespace:Prism.Mvvm;assembly=Prism.Forms" 
    prism:ViewModelLocator.AutowireViewModel="True" 
संबंधित मुद्दे