我更新我的MVVM应用程序以使用async和await获取数据,但由于某种原因,我的ObservableCollection被搞砸了; 我同时加载其他收藏集,如人物,地点,它们有时混合在一起。
如何/为何会发生这种情况,我该如何解决?
视图模型:
private async void Load()
{
Agents = await DAL.Agent.LoadAsync();
}
OR(用于测试目的)
private async void Load()
{
var task = DAL.Agent.LoadAsync();
task.Wait();
Agents = await task;
}
OR(用于测试目的)
public async void Load()
{
AgentsVW = await Task.Run(() => DAL.Agent.LoadVW(null));
}
DAL :
public static Task<ObservableCollection<Models.Agent>> LoadAsync()
{
return Task.Factory.StartNew(() => Load());
}
public static ObservableCollection<Models.Agent> Load()
{
ObservableCollection<Models.Agent> Collection = new ObservableCollection<Models.Agent>();
Collection.Add(new Models.Agent());
try
{
using (DAL.EF.csEntities_PersonData ef = new DAL.EF.csEntities_PersonData())
{
var x = (from r in ef.usp_tblAgents_S(null) select r);
foreach (var item in x)
{
Collection.Add(new Models.Agent()
{
//Fields
});
}
}
}
catch (Exception ex)
{
//Log it
}
return Collection;
}
修改 值是正确的,但它只是在组合框中显示错误/其他值。