【发布时间】:2017-04-12 22:37:08
【问题描述】:
我尝试在加载页面时填充绑定到 ObservableCollection 的 ListView,但未成功。目前,我使用以下代码使用按钮(绑定到命令)。
查看:
<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;
}
}
这与按钮完美配合,但我不知道如何自动完成。我尝试将列表视图的 RefreshCommand 属性绑定到我的命令,但没有。
【问题讨论】:
标签: c# .net mvvm xamarin xamarin.forms