2016-04-29 7 views
1

私はこのオブジェクトをViewModel AからViewModel Bに渡しているPersonオブジェクトを持っています。MVVMCrossのキャプチャ編集テキストの変更

View Bには、この情報を次のように表示します。ここで

<EditText 
    style="@style/InputNumbersEditText" 
    android:layout_weight="1" 
    android:layout_width="0dp" 
    android:layout_height="36dp" 
    local:MvxBind="Text EditPerson.Age, Converter=Nullable, ConverterParameter='0.0'" /> 

<EditText 
    style="@style/InputNumbersEditText" 
    android:layout_weight="1" 
    android:layout_width="0dp" 
    android:layout_height="36dp" 
    local:MvxBind="Text EditPerson.Salary, Converter=Nullable, ConverterParameter='0.0'" /> 

は私がこれらのEditTextのいずれかを編集するときに、デバッグモードでは、それがすべてでEditPersonプロパティに当たらないだろViewModel B

public Person EditPerson 
{ 
    get { return _editPerson; } 
    set 
    { 
     _editPerson = value; 
     RaisePropertyChanged(() => EditPerson); 
    } 
} 

EditPersonプロパティです。

SalaryAgeのように、それぞれのボックスでEditTextの変更をキャプチャするにはどうすればよいですか?

+0

どのようにあなたがVM_BにVM_Aからオブジェクトを渡している:次に、あなたは人の任意のプロパティの変更を聞くことができますか?これは複雑なオブジェクト型であるため、直列化が必要です。 –

+0

私は 'ViewModel A'から' ViewModel B'に 'personId'を渡して、対応する' Person'オブジェクトを取得するために再びサービスを呼び出します。それは働いて、私は選択された '人'を見ることができた。 EditTextには選択されたPerson SalaryとAgeも表示されますが、AgeやSalaryなどの 'EditText'を変更すると' EditPerson'プロパティにヒットしません。 – hotspring

+0

なぜそれが 'EditPerson'にヒットするのですか?あなたの 'EditText'に単純な' onTextChangedListener'を使わないのはなぜですか? –

答えて

1

バインドする正しいプロパティは、Mattの代わりにPersonとPlaceHold3rがPersonの代わりにEditPersonです。

あなたの質問については、あなたのEditPerson設定者は、PersonオブジェクトのAgeまたはSalaryを変更すると起動しません。 ViewModel.EditPerson = new Person();

年齢や給与の変更を検出する場合は、PersonオブジェクトにINotifyPropertyChangedを実装する必要があります。など、年齢、給与

UPDATE

public class Person : INotifyPropertyChanged 
{ 
    public event PropertyChangedEventHandler PropertyChanged; 

    private int _age; 
    private decimal _salary; 

    public int Age 
    { 
     get { return _age; } 
     set 
     { 
      _age = value; 
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Age")); 
     } 
    } 

    public decimal Salary 
    { 
     get { return _salary; } 
     set 
     { 
      _salary = value; 
      PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Salary")); 
     } 
    } 
} 

public class YourViewModel : MvxViewModel 
{ 
    private Person _editPerson; 
    public Person EditPerson 
    { 
     get { return _editPerson; } 
     set 
     { 
      if (_editPerson != null) 
       _editPerson.PropertyChanged -= EditPersonOnPropertyChanged; 

      _editPerson = value; 
      RaisePropertyChanged(() => EditPerson); 

      _editPerson.PropertyChanged += EditPersonOnPropertyChanged; 
     } 
    } 

    private void EditPersonOnPropertyChanged(object sender, PropertyChangedEventArgs propertyChangedEventArgs) 
    { 
     switch (propertyChangedEventArgs.PropertyName) 
     { 
      case "Salary": 
       Debug.WriteLine("Salary has changed"); 
       break; 

      case "Age": 
       Debug.WriteLine("Age has changed"); 
       break; 
     } 
    } 
} 
+0

私は次のように変更しましたが、まだ変更していないため、年齢や性別などの編集テキストを変更するとEditPersonを押すことができません。 'Local:MvxBind =" Text EditPerson.Age、Converter = Nullable、ConverterParameter = '0.0' "'あなたは私の質問に変更を見ることができます。 – hotspring

+0

PersonオブジェクトにINotifyPropertyChangedを実装してもよろしいですか?私の更新された答えを見てください – xleon

0

私はまだ詳細情報が表示されていないため、最初の推測です。 Personにバインドしますが、Person ViewModelsの名前はEditPersonです。したがって、これを試してください:

local:MvxBind="Text EditPerson.Age, Converter=Nullable, ConverterParameter='0.0'" 

これは完全な解決策ではありません。私があなたのPersonクラスを尋ねたのは、PersonINotifyPropertyChangedを実装している場合です。これは通常の方法です(MvvmCrossでは可能な他の解決策もあります)、EditTextsがあなたのビューモデルの変更を聞くことができます。人のあなたの財産セッターでは、

RaisePropertyChanged(() => EditPerson); 

を呼び出す必要があり、その後、あなたが使用している場合でも

+0

私は次のように変更しましたが、まだ変更していないため、年齢や性別などの編集テキストを変更するとEditPersonを押すことができません。 'Local:MvxBind =" Text EditPerson.Age、Converter = Nullable、ConverterParameter = '0.0' "'あなたは私の質問に変更を見ることができます。 – hotspring

0

EDITTEXTがあなたのViewModelのEditPersonプロパティを変更することはありません。この位置にブレークポイントを設定することができます。

local:MvxBind="Text EditPerson.Age, Converter=Nullable, ConverterParameter='0.0'" 

editTextで変更されているのは、Personクラスに属するAgeです。 Personクラスのsetで変更を受け取ることができます。

+0

私はその質問を誤解したと思う;) – Matt

+0

私は 'EditPerson'に変更しましたが、それでも同じです。 – hotspring

+0

私は次のように変更しましたが、まだ変更していないため、年齢や性別などの編集テキストを変更するとEditPersonを押すことができません。 'Local:MvxBind =" Text EditPerson.Age、Converter = Nullable、ConverterParameter = '0.0' "'あなたは私の質問に変更を見ることができます。 – hotspring

関連する問題