我在PCL中调用UIThread时遇到问题。
我做了一个这样的方法:
private async void PopulateContent ()
{
Task <List <Department>> taskDep = new Task <List<Department>> (() =>
{
departments = departmentDal.GetAll();
return departments.ToList ();
});
TaskDep.Start();
TaskDep.Wait();
PopulateCarouselDeps();
Task <List<Category>> taskCat = new Task <List <Category>> (() =>
{
Categories = categoryDal.GetAllByDepartment (carouselViewDepartments.Item as Department).ToList ();
return categories.ToList();
});
TaskCat.Start();
TaskCat.Wait();
PopulateCarouselCats();
Task <List<Product>> taskProd = new Task <List<Product>> (() =>
{
Products = productDal.GetAllByCategory (carouselViewCategories.Item Category);
return products.ToList();
});
TaskProd.Start();
TaskProd.Wait();
PopulateListViewProducts();
}
private void PopulateCarouselDeps ()
{
Device.BeginInvokeOnMainThread (() => {
Try
{
carouselViewDepartments.ItemsSource = departments;
}
Catch (Exception e) // handle whatever exceptions you expect
{
// Handle exceptions
}
});
}
当我调用PopulateCarouselDeps()方法时,会发生以下情况:它不会进入try-catch语句块。
因此,Carousel没有填充,我需要它来推广其他内容。
所有流行的方法都非常相似。
有谁知道如何解决这个问题?
THX,