代码之家  ›  专栏  ›  技术社区  ›  Jacob Saylor

在Silverlight 4 RC应用程序中连接域上下文并手动加载数据

  •  1
  • Jacob Saylor  · 技术社区  · 16 年前

    我是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 回复  |  直到 16 年前
        1
  •  2
  •   Jacob Saylor    16 年前

    答案很简单。数据在加载时立即被查询。虽然有些应用程序将按程序处理此问题,但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...
            }