xamarin.forms - Xamarin Listview 不显示可观察的集合
问题描述
我正在使用Xamarin.Forms
MVVM 开发我的应用程序,但没有发现我做错了什么,我有ObservableCollection
来自 web API 的值,当我设置断点时,即使在视图中,所有值都很好查看绑定源的值一切都有值,但值没有显示在我的ListView
.
这是视图模型
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);
}
}
}
这是视图
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="ARLAPP.Views.ConsultaPage"
BackgroundColor="White"
BindingContext="{Binding Main, Source={StaticResource Locator}}"
Title="Lands">
<ContentPage.Content>
<StackLayout
BindingContext="{Binding Lands}"
Padding="5">
<StackLayout>
<Image
VerticalOptions="Center"
WidthRequest="300"
Source="UserIcon"
BackgroundColor="Transparent"/>
<Label Text="Mark"
VerticalOptions="Center"
HorizontalOptions="CenterAndExpand"
FontAttributes="Bold"
FontSize="Medium"/>
</StackLayout>
<StackLayout>
<ListView
SeparatorVisibility="Default"
FlowDirection="LeftToRight"
BackgroundColor="White"
ItemsSource="{Binding Lands}"
HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label
Grid.Column="2"
VerticalOptions="Center"
TextColor="Black"
Text="{Binding Currency}"/>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</StackLayout>
</ContentPage.Content>
</ContentPage>
这里我如何称呼视图
if (this.PageName == "Lands")
{
MainViewModel.GetInstance().Lands= new LandViewModel();
Application.Current.MainPage = new LandMasterPage();
}
解决方案
检查您的BindingContext
. 我认为您认为您的设置是错误的。
在您的顶层中,StackLayout
您将 the 设置BindingContext
为您的属性:BindingContext="{Binding Lands}"
。并在您的ListView
设置中ItemsSource
也将此属性设置为:ItemsSource="{Binding Lands}"
. 这不起作用,因为ListView
试图绑定到Lands
您的 内部的属性BindingContext
,该属性也设置为Lands
.
BindingContext
从您的顶层删除StackLayout
,因为您不需要它。
确保BindingContext
您的页面ConsultaPage
设置为您的 view-model DatosMedicosViewModel
。
设置绑定上下文的示例(抽象代码):
var mypage = new ConsultaPage();
mypage.BindingContext = new DatosMedicosViewModel();
await Navigation.PushAsync(mypage);
// Load your data in OnAppearing() of the page-event
这应该可以解决您的绑定问题。
旁注:正如 Abdul Gani 在评论中所说:确保您实现INotifyPropertyChanged
接口,但我假设您已经在您的 -Method 中执行此操作BaseViewModel
并调用 NotifyChanged-Event SetValue
。
推荐阅读
- firebase - Firebase 云存储上的签名 URL 突然变得无法访问
- c - mbrtowc 在嵌入式设备上为非 ASCII 字符返回 -1,但在 linux 计算机上不返回
- sql - SQL查询:修改返回的列值
- python - 按值查找元素 Selenium/Python
- html - 如何更改引导程序中每个药丸(选项卡)的选项卡内容颜色?
- c++ - 添加 char 和 int 的结果是什么?
- left-join - SQLBase,从一个表列中查询多个值
- r - 从不同向量/数据框的 nrow 结果创建数据框
- unit-testing - 我必须使用很多模拟来测试我的服务层,可以吗?
- continuous-integration - 无法在构建后事件命令行中执行 .cmd 文件