Reputation: 8385
public async Task ShowIndicator(Func<Task> action)
{
actionEnabled = false;
IndicatorVisibility = true;
await Task.Delay(5000);
await action.Invoke();
IndicatorVisibility = false;
actionEnabled = true;
}
I am using the above code to run some Task while showing the indicator by setting the viewmodel properties of the IndicatorVisibility. If I placed the Task.Delay, it will ok but will slow down the code. If I am not placed it, the indicator won't show straightaway coz it will set to second to last indicator visibility to false.
Is it possible to wait the indicator displayed itself before execute the action from the above code?
Upvotes: 2
Views: 1626
Reputation: 5038
I had the same issue. This has the exact problem, the activity indicator never appers or appears too late:
async void GoDetail(object obj)
{
Busy = true;
DetailsViewModel.Initialize(_items, this.SelectedItem);
await App.Current.MainPage.Navigation.PushAsync(new xxxPage());
Busy = false;
}
This fixes it, activity indicator appears instantly...:
async void GoDetail(object obj)
{
Busy = true;
Device.BeginInvokeOnMainThread(async () =>
{
DetailsViewModel.Initialize(_items, this.SelectedItem);
await App.Current.MainPage.Navigation.PushAsync(new xxxPage());
Busy = false;
});
}
Upvotes: 2
Reputation: 549
Try this code
public Task ShowIndicator(Func<Task> action)
{
actionEnabled = false;
IndicatorVisibility = true;
Device.BeginInvokeOnMainThread(async () =>
{
await action.Invoke();
IndicatorVisibility = false;
actionEnabled = true;
});
}
Upvotes: 0