2015-11-10 10 views
7

こんにちは、私はWPFとMVVMデザインパターンで新しくなりました。私はPRISMでBRIAN LAGUNASのブログやビデオからたくさんのことを学んできました。 ..私のコードで間違っているのは私にとってはうまくいく...どんな助けもありがとう。 は、ここに私のコードです:Prism 6 DelegateCommand Observesプロパティコード

MYビューモデル別のクラスファイルに宣言

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> 

デ私はすでにこれを読んだが、それは私のために働くdoes notの...と私はそれを正しくコーディングすることができます方法を理解カント..私は任意の返信soon..thx

ために、この問題の..HOPEをragarding助けてください

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

+0

ボタンは何をしているのですが欠けていますか? – Jens

+0

メッセージボックス "HOLA"を掲示する。 – 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を試してみてください)あなたのコンストラクタを変更can`t

public Person() 
    { 
     MyPerson = new myPErson(); 
     updateCommand = new DelegateCommand(Execute, CanExecute); 
    } 
+0

thanks .. !!!それは私のために働いています@ galakt .. – Neil

+0

@ニールこれはあなたが助けてくれた場合は、解決策としてマークしてください – 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パターンを使用することをお勧めします。MVVMをよく理解したら、まだプリズムに行くことができます。 お手伝いをしてください。

+0

あなたに感謝の先生@ジェンスHorstmann..andこれは私のコーディングを改善するのに役立ちます..好奇心のだけPRISMライブラリを使用して、それを勉強したいと思っています。将来の研究のために... please please ... thx very much – Neil

+0

@Neilはあなたのリンクであなたがしたいことを行います= P – Jens

+0

haha​​ ..あなたからの提案されたリンク^ _^....私の将来の参考資料... i勉強してください。please please ... @Jens Horstmann – 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" 
関連する問題