2017-01-19 14 views
1

私のViewModelからバインドされている質問付きのラベルを表示するシンプルなビューがあります。今私は私のコンストラクタでプロパティを設定する場合は、私はそれを設定したラベルを表示するラベルを参照してください。私のコマンド機能から移入された場合、私はラベルが変更されて表示されません。面白いのは、Titleプロパティ(getとsetを持つ単純な文字列)を設定した場合、それが設定されている場所に関係なく変更されるということです。何らかの理由でこの特定のプロパティは変更を表示したくありません。私は可能な限りこれを単純化しようとしました。私はViewModelでパブリックな文字列プロパティを定義しようとしましたが、コンストラクタでそれを設定すると、それがコマンド関数に設定されている場合には他のバインドとバインドするよりも変更されません。ここXamarinビューはコンストラクタの後にviewModelからバインドされません

はここ

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
     xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
     x:Class="Pre.MyPage" 
     Title="{Binding Title}" 
     Icon="about.png"> 
<StackLayout VerticalOptions="Center" HorizontalOptions="Center" > 
    <Label Text="{Binding MyClassObj.Question, Mode=TwoWay}"/> 
</StackLayout> 
</ContentPage> 

はここ

public partial class MyPage : ContentPage 
{ 
    MyViewModel vm; 
    MyViewModel ViewModel => vm ?? (vm = BindingContext as MyViewModel); 
    public MyPage() 
    { 
     InitializeComponent(); 
     BindingContext = new MyViewModel(Navigation); 
    } 
    protected override void OnAppearing() 
    { 
     base.OnAppearing(); 
     ViewModel.LoadQuestionCommand.Execute("1"); 
    } 
} 

の背後に私のコードで私のXAMLであるあなたがINofityPropertyChangedイベントを発射されている場合、私のViewModel

public class MyViewModel : ViewModelBase 
{ 
    public MyClass MyClassObj {get;set;} 

    ICommand loadQuestionCommand; 
    public ICommand LoadQuestionCommand => 
     loadQuestionCommand ?? (loadQuestionCommand = new Command<string>(async (f) => await LoadQuestion(f))); 

    public MyViewModel(INavigation navigation) : base(navigation) 
    { 
     Title = "My Title";    
    } 
    async Task<bool> LoadQuestion(string id) 
    { 
     if (IsBusy) 
      return false; 
     try 
     { 
      IsBusy = true; 

      MyClassObj = await StoreManager.QuestionStore.GetQuestionById(id); 
      //MyClassObject is populated when I break here 
     } 
     catch (Exception ex) 
     { 
      Debug.WriteLine(ex.Message); 
     } 
     finally 
     { 
      IsBusy = false; 
     } 
     return true; 
    } 

答えて

1

である私は表示されませんあなたのMyClassObjプロパティのために。代わりに、ただの

:最後の方法は

NotifyPropertyChanged(nameof(MyClassObj)); 

が変更に関する表示を通知され

MyClass myClassObj; 
public MyClass MyClassObj 
{ 
    get {return myClassObj;} 
    set 
    { 
     //if they are the same you should not fire the event. 
     //but since it's a custom object you will need to override the Equals 
     // of course you could remove this validation. 
     if(myClassObj.Equals(value)) 
      return; 

     myClassObj = value; 

     //This method or something has to be in your VieModelBase, similar. 
     NotifyPropertyChanged(nameof(MyClassObj)); 
    } 
}  

public MyClass MyClassObj {get;set;} 

あなたのような何かを持っている必要があります。

関連する問題