在Silverlight 4 RC应用程序中手动连接DomainContext并加载数据

时间:2010-04-19 22:29:30

标签: silverlight silverlight-4.0 wcf

我是WCF& RIA服务并且看起来像是一个基本问题。我有多次在我的silverlight 4 RC应用程序中从我的数据源窗口拖放到我的表单并从数据库返回信息。但是我需要查询数据库以获取其他信息(构建报告)当我尝试使用以下代码时,我没有收到任何错误,但我也没有从服务中获得任何信息。

//Global
public UMSDomainContext _umsDomainContext = new UMSDomainContext();

//In the Initialization portion
_umsDomainContext.Load(_umsDomainContext.GetUMOptionsQuery());
//Queries
 var name = from n in _umsDomainContext.UMOptions
                              select n.DistrictName;

                var street1 = from c in _umsDomainContext.UMOptions
                              select c.Address1;

                var street2 = from c in _umsDomainContext.UMOptions
                              select c.Address2;

                var city = from c in _umsDomainContext.UMOptions
                              select c.City;

                var zip = from c in _umsDomainContext.UMOptions
                              select c.Zip;

我正在调用当前的其他参考文献。

using System.Linq;
using System.ServiceModel.DomainServices.Client;
using System.ComponentModel.DataAnnotations;
using MTT.Web;

1 个答案:

答案 0 :(得分:2)

答案很简单。加载数据时立即被查询。虽然有些应用程序会在程序上处理这个问题,但似乎Silverlight在继续之前并没有等待数据加载。所以我做了以下事情:

public void LoadCustomerInformation()
{
     //Load the Query
     var loadOperation = _umsDomainContext.Load<UMOption>(_umsDomainContext.GetUMOptionsQuery());
     //Create a event handler to run after the data is fully loaded.
     loadOperation.Completed += new EventHandler(loadOperation_Completed);
}

void loadOperation_Completed(object sender, EventArgs e)
        {
            var name = (from n in _umsDomainContext.UMOptions
                       select n.DistrictName).First();

            var street1 = from c in _umsDomainContext.UMOptions
                          select c.Address1;

            var street2 = from c in _umsDomainContext.UMOptions
                          select c.Address2;

            var city = from c in _umsDomainContext.UMOptions
                       select c.City;

            var zip = from c in _umsDomainContext.UMOptions
                      select c.Zip;

            //Other code to work with the data, etc...
        }