Xamarin.forms Xamarin Listview don';不显示可观察的集合
我正在使用Xamarin.forms Xamarin Listview don';不显示可观察的集合,xamarin.forms,Xamarin.forms,我正在使用Xamarin.FormsMVVM开发我的应用程序,没有发现我做错了什么,我有一个observeCollection,其中包含来自web API的值,当我设置断点时,所有值都是好的,即使在视图中,当我看到绑定源的值时,一切都有价值,但是这些值没有显示在我的列表视图中 这是ViewModel class DatosMedicosViewModel : BaseViewModel { private ApiService apiService; private Obser
Xamarin.Forms
MVVM开发我的应用程序,没有发现我做错了什么,我有一个observeCollection
,其中包含来自web API的值,当我设置断点时,所有值都是好的,即使在视图中,当我看到绑定源的值时,一切都有价值,但是这些值没有显示在我的列表视图中
这是ViewModel
class DatosMedicosViewModel : BaseViewModel
{
private ApiService apiService;
private ObservableCollection<Land> land;
private bool isRefreshing;
public ObservableCollection<Land> Lands
{
get { return this.land; }
set { SetValue(ref this.land, value); }
}
public bool IsRefreshing
{
get { return this.isRefreshing; }
set { SetValue(ref this.isRefreshing, value); }
}
public DatosMedicosViewModel()
{
this.apiService = new ApiService();
this.LoadLand();
}
private async void LoadLand()
{
this.IsRefreshing = true;
var connection = await this.apiService.CheckConnection();
if (!connection.IsSuccess)
{
this.IsRefreshing = false;
await Application.Current.MainPage.DisplayAlert(
"Error",
connection.Message,
"Accept");
await Application.Current.MainPage.Navigation.PopAsync();
return;
}
var response = await this.apiService.GetList<Land>(
"url Base",
"prefix",
"Controller");
if (!response.IsSuccess)
{
this.IsRefreshing = false;
await Application.Current.MainPage.DisplayAlert(
"Error",
response.Message,
"Accept"
);
return;
}
var list = (List<Land>)response.Result;
this.Lands = new ObservableCollection<Land>(list);
this.IsRefreshing = false;
}
public ICommand RefreshCommand
{
get
{
return new RelayCommand(LoadLand);
}
}
}
检查您的BindingContext
。我认为你的观点是错误的
在顶级StackLayout
中,您将BindingContext
设置为您的属性:BindingContext=“{Binding Lands}”
。在列表视图中
也将ItemsSource
设置为该属性:ItemsSource=“{Binding Lands}”
。这将不起作用,因为列表视图
正试图绑定到绑定上下文
中的属性Lands
,该属性也设置为Lands
从顶级堆栈布局中删除BindingContext
,因为您不需要它
确保页面咨询页面的BindingContext
设置为视图模型DatosMedicosViewModel
设置bindingcontext的示例(抽象代码):
这将解决您的绑定问题
旁注:正如Abdul Gani在评论中所说:确保您实现了INotifyPropertyChanged
接口,但我假设您已经在BaseViewModel
中实现了这一点,并在SetValue
-方法中调用NotifyChanged事件。是否可以显示土地实体类?yeap,类和所有类都有我想要的值,唯一错误的是listview没有显示这些值。公共类Land{public int Name{get;set;}公共int Currency{get;set;}公共字符串长度{get;set;}公共十进制语言{get;set;}}您需要继承INotifyPropertyChanged接口,并更改属性,以便在为其赋值时通知。。一些类似于私有字符串的cardImage;公共字符串CardImage{get{return CardImage;}set{CardImage=value;OnPropertyChanged();}
if (this.PageName == "Lands")
{
MainViewModel.GetInstance().Lands= new LandViewModel();
Application.Current.MainPage = new LandMasterPage();
}
var mypage = new ConsultaPage();
mypage.BindingContext = new DatosMedicosViewModel();
await Navigation.PushAsync(mypage);
// Load your data in OnAppearing() of the page-event