Data binding Xamarin。表单绑定不起作用
我无法在屏幕上显示新数据 C#代码Data binding Xamarin。表单绑定不起作用,data-binding,xamarin.forms,Data Binding,Xamarin.forms,我无法在屏幕上显示新数据 C#代码 public分部类主页面:ContentPage{ 公共类阶段详细信息{ 公开列表聊天{ 获得;设置; } } public new PhaseDetails BindingContext=>(PhaseDetails)base.BindingContext; 公共主页(){ 初始化组件(); base.BindingContext=新阶段详细信息{ Chats=新列表(新字符串[]{“qwe”}), }; Task.Run(()=>newlist(新字符串[
public分部类主页面:ContentPage{
公共类阶段详细信息{
公开列表聊天{
获得;设置;
}
}
public new PhaseDetails BindingContext=>(PhaseDetails)base.BindingContext;
公共主页(){
初始化组件();
base.BindingContext=新阶段详细信息{
Chats=新列表(新字符串[]{“qwe”}),
};
Task.Run(()=>newlist(新字符串[]{“1”、“2”、“3”})).ContinueWith((Task)=>Device.beginInvokeMainThread(()=>{
BindingContext.Chats=task.Result;
OnPropertyChanged(空);
} ) );
}
}
XAML
应为:列表视图中有三行。
但它只显示了一个。
可能是什么问题?这有助于:
public class PhaseDetails {
...
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged([CallerMemberName] string propertyName = null) {
PropertyChanged?.Invoke( this, new PropertyChangedEventArgs( propertyName ) );
}
对property的调用已从property setter更改。您可以在此github页面上尝试此功能。它使用数据绑定模式=从ViewModel到View的双向
如果
task.Result
有任何值,问题是您没有从更改中通知视图(OnPropertyChanged(null);
)。您应该对PropertyChanged(“聊天”)进行代码>相位详细信息不实现INPC。而且您似乎没有从正确的上下文调用PropertyChanged,它通常位于setter中。
<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>
public class PhaseDetails {
...
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged([CallerMemberName] string propertyName = null) {
PropertyChanged?.Invoke( this, new PropertyChangedEventArgs( propertyName ) );
}