我已经尝试在加载页面时填充绑定到ObservableCollection的ListView失败。目前我使用以下代码处理按钮(绑定到Command)。
查看:
<Button Text="Load Items" Command="{Binding LoadItemsCommand}"></Button>
<ActivityIndicator IsRunning="{Binding IsBusy}" IsVisible="{Binding IsBusy}" />
<ScrollView>
<ListView ItemsSource="{Binding Items}">
.....
</ListView>
</ScrollView>
View.cs:
private ItemsViewModel _itemsViewModel;
public ItemsView()
{
InitializeComponent();
_itemsViewModel = new ItemsViewModel();
BindingContext = _itemsViewModel;
}
视图模型:
public ObservableCollection<Item> Items{ get; set; }
public Command LoadItemsCommand { get; set; }
public ItemsViewModel()
{
Items = new ObservableCollection<Item>();
_isBusy = false;
LoadItemsCommand = new Command(async () => await LoadItems(), () => !IsBusy);
}
public async Task LoadItems()
{
if (!IsBusy)
{
IsBusy = true;
await Task.Delay(3000);
var loadedItems = ItemsService.LoadItemsDirectory();
foreach (var item in loadedItems)
Items.Add(item);
IsBusy = false;
}
}
这与按钮完美配合,但我不知道如何自动完成。我尝试将listview的RefreshCommand属性绑定到我的命令,但没有。
答案 0 :(得分:4)
有两种方法,但最好的方法是在视图模型构造函数中启动加载数据的任务。我也不会将每个项目添加到可观察集合中,因为这意味着在最终添加时更新UI。加载所有数据后,最好完全替换集合。
类似的东西:
public ItemsViewModel()
{
Items = new ObservableCollection<Item>();
_isBusy = false;
Task.Run(async () => await LoadItems());
}
public async Task LoadItems()
{
var items = new ObservableCollection<Item>(); // new collection
if (!IsBusy)
{
IsBusy = true;
await Task.Delay(3000);
var loadedItems = ItemsService.LoadItemsDirectory();
foreach (var item in loadedItems)
items.Add(item); // items are added to the new collection
Items = items; // swap the collection for the new one
RaisePropertyChanged(nameof(Items)); // raise a property change in whatever way is right for your VM
IsBusy = false;
}
}