2017-10-10 19 views
-1

新しいデータが画面に表示されません。Xamarin.Formsバインディングが機能しない

C#コード

public partial class MainPage : ContentPage { 
    public class PhaseDetails { 
     public List<string> Chats { 
      get; set; 
     } 
    } 

    public new PhaseDetails BindingContext => (PhaseDetails) base.BindingContext; 

    public MainPage() { 
     InitializeComponent(); 
     base.BindingContext = new PhaseDetails { 
      Chats = new List<string>(new string[] { "qwe" }), 
     }; 
     Task.Run(() => new List<string>(new string[] { "1", "2", "3" })).ContinueWith((task) => Device.BeginInvokeOnMainThread(() => { 
      BindingContext.Chats = task.Result; 
      OnPropertyChanged(null); 
     })); 
    } 
} 

XAML期待

<StackLayout> 
    <ListView ItemsSource="{Binding Chats}"> 
     <ListView.ItemTemplate> 
      <DataTemplate> 
       <ViewCell> 
        <StackLayout Padding="6" BackgroundColor="Aquamarine"> 
         <Label Text="Service Area"/> 
        </StackLayout> 
       </ViewCell> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 
</StackLayout> 

:リストビューに3列。 しかし、それは1つだけを示しています。 何が問題なのでしょうか?

+0

'task.Result'は、任意の値を持っている場合、問題は、あなたがあなたの変化からビューを通知されていないということです(' OnPropertyChangedを(NULL); ')。 'OnPropertyChanged(" Chats ");' –

+0

PhaseDetailsはINPCを実装していません。そして、あなたは正しいコンテキストからPropertyChangedを呼び出すようには見えません。通常、これはセッターにあります。 – Jason

答えて

0

これは助け:

 public class PhaseDetails { 
... 
      public event PropertyChangedEventHandler PropertyChanged; 
      void OnPropertyChanged([CallerMemberName] string propertyName = null) { 
       PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
      } 

をプロパティのセッターからOnPropertyChangedをコール。

1

このgithubページで利用可能なsimple project solutionを試すことができます。 ViewModelからViewへのデータバインディングモード= TwoWayを使用します。

enter image description here