数据未从WCF RIA加载到SIlverlight MainPage.xaml.cs

时间:2013-12-23 13:42:58

标签: c# silverlight wcf-ria-services

我创建了一个Silverlight应用程序,我正在使用WCF RIA服务。 我的MainPage.xaml.cs无法从域服务中获取数据。域服务中的方法是

public IQueryable<Measurement> GetMeasurements()
{
    return this.ObjectContext.Measurements;
}

MainPage.xaml.cs中的代码如下

EntityQuery<Measurement> query = from p in service.GetMeasurementsQuery() select p;

LoadOperation<Measurement> measurement = service.Load(query);

请告知我一些意见和建议。

1 个答案:

答案 0 :(得分:1)

RIA Services最常见的误解是期望Web服务调用是同步的。实际上,Web服务调用是异步发生的。您不能指望在Load()调用后立即加载结果。

// creates a query object. It is the equivalent of 
// "SELECT ... FROM ..." as a string. It doesn't actually
// execute the query.
EntityQuery<Measurement> query = 
  from p in service.GetMeasurementsQuery() select p;

// sends the query to the server, but the server doesn't
// return a result in the LoadOperation. The LoadOperation
// will call you back when it is completed.
LoadOperation<Measurement> measurement = service.Load(query);

// when results are available, MeasurementLoaded is called
measurement.Completed += MeasurementLoaded;

// WARNING: do not have any code that expects results here.
// var myMeasurements = service.Measurements;


public void MeasurementLoaded(object sender, EventArgs eventArgs)
{
   // you can use service.Measurements now.
   var myMeasurements = service.Measurements;
}