我试图用包含配置列表的项目列表填充列表框。
所以我要做的就是先用我的项目列表和所有子项填充列表框。这一切都很顺利。但是对于每个配置,我需要调用休息服务来获取状态。所以我不想立即发出所有的请求(首先绑定项目和配置,然后为每个配置获取状态并将配置状态属性更新为异步。但是当我开始加载所有配置时(可以达到aprox 200)列表框冻结了。
RED:经过一番思考后,当每个listitem(subitem)的状态对用户可见时,加载每个listitem(subitem)的状态应该更好,但是我找不到任何好的事件。有关如何处理此事的任何消息?
这是我的解决方案:
MainViewModel
//Fired on pageLoad
_projectRepository.FindBy(_serverCityConfiguration, BuildListBoxGroups);
private void BuildListBoxGroups(IEnumerable<Project> projects)
{
var projectGroups = new List<ListBoxGroup<BuildConfigurationModel>>();
foreach (Project project in projects)
{
var buildConfigurations = project.BuildConfigurations.Select(buildConfiguration
=> new BuildConfigurationModel(buildConfiguration.Id, buildConfiguration.Name, new List<BuildModel>())).ToList();
projectGroups.Add(new ListBoxGroup<BuildConfigurationModel>(
new ProjectModel(project.Id, project.Name), buildConfigurations));
}
Deployment.Current.Dispatcher.BeginInvoke(() =>
Projects = projectGroups.ToObservableCollection());
//It's here my problem starts, where my listbox freezes
ThreadPool.QueueUserWorkItem(FireBuildsFetching, projects);
}
private void FireBuildsFetching(object stuff)
{
var projects = stuff as IEnumerable<Project>;
foreach (var project in projects)
{
foreach (var buildConfiguration in project.BuildConfigurations)
{
ThreadPool.QueueUserWorkItem(RetrieveBuils, buildConfiguration);
}
}
}
private void RetrieveBuils(object buildConfiguration)
{
Debug.WriteLine("RetrieveBuils");
var localBuildConfig = buildConfiguration as BuildConfiguration;
//Here is some logic to find correct buildconfiguration to add status to
...
Deployment.Current.Dispatcher.BeginInvoke(() =>
buildConfigurationModel.AddBuilds(buildModels));
}
完成对休息服务的所有呼叫后,列表框会立即更新所有状态。
有人对我的问题有任何好的解决方案吗?
答案 0 :(得分:0)
可以在后台循环中添加50ms的wait(thread.Sleep)。在我看来,您的列表框会不断被后台威胁更新。从线程编组+更新UI声明需要资源。通过最小化往返次数来优化代码,批量更新数据(每10或20项)可以提供帮助。希望这会有所帮助。