I'm working on xamarin. The tabbed page on load when is clicked. And i want to load the data by api behind the scene and showing the loading indicator.
protected override void OnAppearing()
{
base.OnAppearing();
if (!_appeared)
{
// Want this to be run behind
ProductViewData productViewData = new ProductViewData();
products = productViewData.GetProductList("10");
count = 10;
productListView.ItemsSource = products;
_appeared = true;
}
}
Thanks for any helping.
You can make the method async and await your response. You shouldn't update UI thread from another thread, so using Task.Run is a bad idea. Take a look at Async Programming if you need more details
protected override async void OnAppearing()
{
base.OnAppearing();
if (!_appeared)
{
try
{
ProductViewData productViewData = new ProductViewData();
// make the method asynchronous
products = productViewData.GetProductListAsync("10");
count = 10;
productListView.ItemsSource = products;
_appeared = true;
}
catch(Exception exception)
{
// good idea to catch any network exceptions
}
}
}
Just put inside the Task.Run and it will work
protected override void OnAppearing()
{
base.OnAppearing();
if (!_appeared) // Avoid repeat loding
{
activity.IsEnabled = true;
activity.IsRunning = true;
activity.IsVisible = true;
var task = Task.Run(() =>
{
ProductViewData productViewData = new ProductViewData();
products = productViewData.GetProductList("10");
count = 10;
productListView.ItemsSource = products;
});
_appeared = true;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.