Multithreading 将数据从后台传输到UI线程
我有一个相当长的项目列表(大约100个),我从服务器上获取这些项目,并在LongListSelector中显示给用户。每件物品都是一张照片、一个名字和一些描述 主要的问题是,当我将列表发送到ObservableCollection时,它明显减慢了ui的速度 当我尝试上下滚动时,情况更糟:有时我可以看到新项目渲染过程中的延迟 我试了两种方法 使用TaskEx.Run():Multithreading 将数据从后台传输到UI线程,multithreading,windows-phone-7,mvvm-light,ui-thread,Multithreading,Windows Phone 7,Mvvm Light,Ui Thread,我有一个相当长的项目列表(大约100个),我从服务器上获取这些项目,并在LongListSelector中显示给用户。每件物品都是一张照片、一个名字和一些描述 主要的问题是,当我将列表发送到ObservableCollection时,它明显减慢了ui的速度 当我尝试上下滚动时,情况更糟:有时我可以看到新项目渲染过程中的延迟 我试了两种方法 使用TaskEx.Run(): var answer = await shoppingCartListDataService.GetShoppingListA
var answer = await shoppingCartListDataService.GetShoppingListAsync();
var groupedObjects = await TaskEx.Run(() => from item in answer.collection
group item by
item.name[0].ToString(CultureInfo.InvariantCulture)
into it
orderby it.Key
select
new ProductSearchCategoryCollection<ProductItem>(
it.Key, it.OrderBy(i => i.SumPrice)));
FoundProductItems = new ObservableCollection<ProductSearchCategoryCollection<ProductItem>>(groupedObjects);
var answer=wait shoppingCartListDataService.GetShoppingListAsync();
var groupedObjects=await TaskEx.Run(()=>from answer.collection中的项
按项目分组
item.name[0]。ToString(CultureInfo.InvariantCulture)
投入其中
按它点菜,钥匙
选择
新产品SearchCategoryCollection(
it.Key,it.OrderBy(i=>i.SumPrice));
FoundProductItems=新的ObservableCollection(GroupedObject);
及
Dispatcher.BeginInvoke()(从ThreadWorker调用)
var answer=wait shoppingCartListDataService.GetShoppingListAsync();
var groupedObjects=来自answer.collection中的项
按项目分组。过道
投入其中
按它点菜,钥匙
选择
新产品SearchCategoryCollection(it.Key、it.OrderBy(i=>i.SumPrice));
Deployment.Current.Dispatcher.BeginInvoke(()=>
FoundProductItems=新的ObservableCollection(GroupedObject));
在这两种情况下,用户界面的放缓是值得注意的。Ritch Melton的最终版本:
_items = groupedObjects.ToList();
Deployment.Current.Dispatcher.BeginInvoke(() =>
RaisePropertyChanged("FoundProductItems", _items, _items, true)); // using shortcut from MVVMlight
如果您从任务中设置属性,并且仅使用dispatcher调用propertychanged事件,会发生什么情况?老实说,如果您不单独更新项目,您可以在GroupedObject上调用.ToList(),也可以在UI线程上引发propertychanged事件。@RitchMelton yep,现在更顺利了,谢谢!
_items = groupedObjects.ToList();
Deployment.Current.Dispatcher.BeginInvoke(() =>
RaisePropertyChanged("FoundProductItems", _items, _items, true)); // using shortcut from MVVMlight