加载数据时显示进度条

时间:2017-06-19 19:15:05

标签: c# android xamarin xamarin.android mvvmcross

我正在尝试在加载数据时显示进度条,一旦加载了数据,我想隐藏进度条。

对于测试目的,我刚加了10秒就可以了。

private async Task ProgressBarAsync()
{
    _isProgressVisible = true;
   RaisePropertyChanged(() => IsProgressVisible);

   await Task.Delay(TimeSpan.FromSeconds(10)).ConfigureAwait(false);

    isProgressVisible = false;
    RaisePropertyChanged(() => IsProgressVisible);
}

但是,在以下实际代码中,不会显示进度条。

private async Task RefreshStudentList()
{
   //display progressbar
   _isProgressVisible = true;
   RaisePropertyChanged(() => IsProgressVisible);

   // load data
   var task =  _studentService.GetStudentAsync();
   await task;
   Students = new ObservableCollection<StudentViewModel>(task.Result.ConvertAll(x => new ClassViewModel(x)));

   //hide progressbar
   _isProgressVisible = false;
    RaisePropertyChanged(() => IsProgressVisible);
}

1 个答案:

答案 0 :(得分:0)

使用InvokeOnMainThread尝试此操作:

private async Task RefreshStudentList()
{
   //display progressbar
   _isProgressVisible = true;
   InvokeOnMainThread(() => RaisePropertyChanged(() => IsProgressVisible));

   // load data
   var data =  await _studentService.GetStudentAsync();
   Students = new ObservableCollection<StudentViewModel>
       (data.ConvertAll(x => new ClassViewModel(x)));

   //hide progressbar
   _isProgressVisible = false;
   InvokeOnMainThread(() => RaisePropertyChanged(() => IsProgressVisible));
}