我想让获取数据不在ui线程中而是在后台线程中加载。我已经尝试了几个谷歌的例子,但它仍然无法正常工作。
ThreadPool.QueueUserWorkItem((o) =>
{
IList<Asana> asanasRepo = null;
var asanasRepository = this.GetService<IAsanasRepository>();
asanasRepo = asanasRepository.GetAllAsanas();
Asanas = asanasRepo.Select(x => new AsanasListItemViewModel
{
AsanaId = x.AsanaId,
AsanaLevel = InfrastructureHelper.GetLevel(x.AsanaLevel),
CoverImagePath = string.Format("/Content/Images/{0}", x.CoverImageFileName),
UsualAsanaTitle = x.UsualTitle,
YogaAsanaTitle = x.YogaTitle
}).ToObservableCollection();
asanasDispatcher.BeginInvoke(() =>
{
AsanasItems.Clear();
AsanasItems = (from asana in Asanas
group asana by asana.AsanaLevel into c
orderby c.Key
select new Group<AsanasListItemViewModel>(c.Key, c)
).ToObservableCollection();
});
});
主要思想是从数据库中加载数据,而不是在ui线程中,而是在后台,当数据加载时将其渲染到ui。我已经尝试过类似上面的代码,但它不起作用。你能帮我解决这个问题吗?谢谢!