2012-03-19 55 views
0

我想從wcf服務的數據綁定到網格silverlight應用程序。Silverlight Wcf綁定網格

我正在這樣做。

這是我的服務類

public class Employee 
    { 
     public string Adres { get; set; } 
     public string Country { get; set; } 
     public string City { get; set; } 
     public string Region { get; set; } 
    } 

    public List<Employee> CustomerList() 
      { 

       OracleConnection conn = new OracleConnection("connectionstring"); 
       OracleCommand cmd = new OracleCommand(); 
       var custList = new List<Employee>(); 
       cmd.CommandText = "select * from EMPLOYEES"; 

       cmd.CommandType = CommandType.Text; 
       cmd.Connection = conn; 
       conn.Open(); 

       OracleDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); 


       if (dr != null) 
       { 
        while (dr.Read()) 
        { 
         var cust = new Employee 
         { 
          Adres = dr.GetString(7), 
          Country = dr.GetString(11), 
         }; 
         custList.Add(cust); 
        } 
       } 
       return custList; 
      } 

和Silverlight應用程序代碼

client.CustomerListCompleted += new EventHandler<CustomerListCompletedEventArgs>(client_CustomerListCompleted); 
      client.CustomerListaAsync(); 
     } 

     void client_CustomerListCompleted(object sender, CustomerListCompletedEventArgs e) 
     { 
      gridControl1.ItemsSource = e.Result[0]; 
     } 

這是工作,但我不得不執行多個SQL查詢,我無法實現對每一類one.how我可以做這個任務。

謝謝。

+0

你是什麼意思的「必須執行多個SQL查詢」?除了客戶名單之外,您是否還有其他需要獲取的數據集,並且正在尋找一種將其返回客戶端的優化方法? – KodeKreachor 2012-03-20 00:34:22

回答

0

您將需要更多地分解您的代碼。我假設你的意思是你有多個使用Employee的服務。如果你這樣做,那麼你會發現你在silverlight方面有多個Employee對象(按服務名稱空間區分)。

要解決此問題: 首先爲您的業務實體/數據傳輸對象創建一個服務器端項目。員工應該在那裏。

然後創建一個Silverlight項目並將「添加現有類」作爲鏈接類。

現在,當您執行更新服務時,VS2010服務生成的代碼將不再包含Employee ...而是利用共享業務對象。所以現在多個服務參考是指同一個員工。

然後在Silverlight中,我還會進一步分解從svc調用中收到的結果。創建一個ObservableCollection然後遍歷結果並添加到這個集合中。綁定到Obs集合是讓Silverlight保持高興的方式。 (你還需要一些關於員工的INotifyPropertyChanged的東西......但希望這是一個有用的開始)。