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

C# 数据未从WCF RIA加载到SIlverlight MainPage.xaml.cs,c#,silverlight,wcf-ria-services,C#,Silverlight,Wcf Ria Services,我已经创建了一个Silverlight应用程序,我正在其中使用WCF RIA服务。 My MainPage.xaml.cs无法从域服务获取数据。域服务中的方法是 public IQueryable<Measurement> GetMeasurements() { return this.ObjectContext.Measurements; } public IQueryable GetMeasurements() { 返回this.ObjectContext.Measur

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

public IQueryable<Measurement> GetMeasurements()
{
    return this.ObjectContext.Measurements;
}
public IQueryable GetMeasurements()
{
返回this.ObjectContext.Measurements;
}
MainPage.xaml.cs中的代码如下

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

LoadOperation<Measurement> measurement = service.Load(query);
EntityQuery=来自服务中的p。GetMeasurementsQuery()选择p;
LoadOperation measurement=service.Load(查询);

请告诉我一些建议。

对RIA服务最常见的误解是希望web服务调用是同步的。实际上,web服务调用是异步进行的。不能期望在Load()调用后立即加载结果

//创建一个查询对象。它相当于
//“选择…从…”作为字符串。事实上并不是这样
//执行查询。
EntityQuery查询=
从服务中的p.GetMeasurementsQuery()选择p;
//将查询发送到服务器,但服务器不发送
//在LoadOperation中返回一个结果。加载操作
//完成后会给你回电话。
LoadOperation measurement=service.Load(查询);
//当结果可用时,将调用MeasurementLoaded
测量。完成+=测量加载;
//警告:此处没有任何预期结果的代码。
//var myMeasurements=服务度量;
已加载公共void度量值(对象发送方,EventArgs EventArgs)
{
//您现在可以使用服务测量。
var myMeasurements=服务度量;
}

如果您描述您的期望和实际发生的情况,您将得到更好的帮助。正如您所描述的,我认为您的代码没有问题。
// 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;
}