listview - Xamarin Forms - 刷新 ListView 时连接到远程数据库
问题描述
我正在做一个预订应用程序,当用户刷新 ListView 时,它应该连接到远程数据库以获取所有预订并更新 ListView。
我的onAppearing
方法中有代码
我尝试将以下代码添加到刷新绑定中,但没有成功:
ItemsPage ip = new ItemsPage();
ArrayList reservations = new ArrayList();
reservations = await ip.CheckReservations(ItemsPage.currentDate);
ItemsPage.reservations = reservations;
OnAppearing()
void中连接数据库的代码
protected async override void OnAppearing()
{
base.OnAppearing();
reservations = new ArrayList();
reservations = await CheckReservations(currentDate);
}
刷新绑定:
public class ItemsViewModel : BaseViewModel
{
public ObservableCollection<Termin> Items { get; set; }
public Command LoadItemsCommand { get; set; }
public ItemsViewModel()
{
Title = "Rezervácie";
Items = new ObservableCollection<Termin>();
LoadItemsCommand = new Command(async () => await ExecuteLoadItemsCommand());
}
async Task ExecuteLoadItemsCommand()
{
if (IsBusy)
return;
IsBusy = true;
try
{
var items = await DataStore.GetItemsAsync(true);
foreach (var item in items)
{
Items.Add(item);
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
finally
{
IsBusy = false;
}
}
}}
GetItemsAsync:
public async Task<IEnumerable<Termin>> GetItemsAsync(bool forceRefresh = false)
{
return await Task.FromResult(items);
}
ItemsPage 是我的观点。
在我的 viewModel 中调用了 refresh 方法。导航到另一个页面并刷新作品。
预订后我打电话给:
ItemsListView.BeginRefresh();
这确实有效,但是从刷新命令中引用该行会使 LV 不填充。
XAML 代码:
<ListView x:Name="ItemsListView"
ItemsSource="{Binding Items}"
VerticalOptions="FillAndExpand"
HasUnevenRows="true"
BackgroundColor="DarkGreen"
SeparatorColor="Black"
RefreshCommand="{Binding LoadItemsCommand}"
IsPullToRefreshEnabled="true"
IsRefreshing="{Binding IsBusy, Mode=OneWay}"
CachingStrategy="RecycleElement"
ItemSelected="OnItemSelected">
解决方案
ViewModel 中的对象列表应该是 ObservableCollection,以便列表检测到列表中的更改并相应地更新 UI。
在您的模型中:
public ObservableCollection<YourModel> ListViewItems { get; set; }
在 XAML 中
<ListView ItemsSource="{Binding ListViewItems}" />
推荐阅读
- python - 如何在图像中找到区域最大值/最小值?
- javascript - 为什么这会解析为日期?
- java - 是否有一致的方法来跟踪 Java 中剪辑(或音频文件)中的位置?
- php - ODBC 不想与 pdo 连接
- jquery - 加载 Autodesk Forge 查看器时是否有任何 jquery 方法可以调用属性面板?
- python - 图像在烧瓶创建的报告中出现拉伸
- asp.net-core - 在暂存服务器上设置 ASPNETCORE_ENVIRONMENT 不起作用
- java - 无法识别 accessExternalSchema
- docker - 詹金斯管道异常 - 找不到码头工人
- java - 获取 java.lang.NoClassDefFoundError: javax/ws/rs/Path 当依赖项在buiild.sbt