我有一个数据类,可以将一些对象从wcf dataservice返回到silverlight应用程序:
void ExecuteWipReportQuery(DataServiceQuery qry) { context = new StaffKpiServices.HwfStaffKpiEntities(theServiceRoot); qry.BeginExecute(new AsyncCallback(a => { 尝试 { IEnumerable results = qry.EndExecute(a); OnDataLoadingComplete(new WipReportByMonthEventArgs(results)); } catch(Exception ex) { OnDataLoadingError(前); } }), 空值); }
视图模型然后获得这些结果并将它们添加到可观察的集合中:
void wipReportDataContainer_DataLoadingComplete(object sender,Domain.WipReportByMonthEventArgs e) { Application.Current.RootVisual.Dispatcher.BeginInvoke(()=> { this.wipReport.Clear(); string s =“”;
foreach (StaffKpiServices.WipReportByMonth r in e.Results)
{
//this.wipReport.Add(r);
//s += r.ClientCode;
this.wipReport.Add(new StaffKpiServices.WipReportByMonth
{
ClientCode = r.ClientCode,
ClientGroup = r.ClientGroup,
ClientName = r.ClientName,
ClientType = r.ClientType,
FinancialYear = r.FinancialYear,
Month = r.Month,
OSDebt = r.OSDebt,
OSDisb = r.OSDisb,
OSOther = r.OSOther,
OSTime = r.OSTime,
OSTotal = r.OSTotal,
PartnerUserName = r.PartnerUserName,
PracName = r.PracName,
Recov = r.Recov,
RecovFees = r.RecovFees,
RecPerc = r.RecPerc,
SicCode = r.SicCode,
SicParentName = r.SicParentName,
StaffName = r.StaffName,
YTDFees = r.YTDFees,
YTDTime = r.YTDTime
});
s += r.ClientCode + " ";
}
string s2 = "";
foreach (var p in this.wipReport)
{
s2 += p.ClientCode + " ";
}
OnPropertyChanged("WipReport");
if (null != LoadComplete)
{
LoadComplete(this, EventArgs.Empty);
}
});
}
一切正常,但如果数据刷新了两三次,那么集合retrun包含正确数量的对象,但都具有重复属性。似乎没有理由,就好像foreach没有处理集合,但同时没有返回任何错误。有什么想法吗?
答案 0 :(得分:1)
好的,所以这很奇怪..但是通过重新创建获取数据的对象(创建上下文的对象),一切都很好,但是如果viewmodel保持活动状态负责运行dataservice查询的对象,则会出现问题.....
我不知道为什么会这样,但问题已经消失了.....