2009-11-29 90 views

回答

1

使用WCF服務。只要您的對象是可序列化的,運行時就會透明地爲您進行編碼和解碼。

一個簡單的Silverlight啓用WCF服務看起來是這樣的:

using System.ServiceModel; 
using System.ServiceModel.Activation; 

[ServiceContract(Namespace = "")] 
[AspNetCompatibilityRequirements(RequirementsMode = 
    AspNetCompatibilityRequirementsMode.Allowed)] 
public class YourService 
{ 
    [OperationContract] 
    public string DoStuff(string arg) 
    { 
     return arg + "stuff"; 
    } 
} 

您可以通過創建一個[DataContract]與您的數據類型替換「字符串」。

0

在我看來,最好是使用web服務將任何需要的東西運送到您的Silverlight應用程序。我建議您將WebClient類與URI類結合使用以獲取數據。例如:

Uri uri = new Uri(//the url of you webservice, UriKind.RelativeOrAbsolute); 

現在創建WebClient類的實例,並添加回調時,從Web服務讀取完成被稱爲:

WebClient wc = new WebClient(); 
wc.OpenReadCompleted += new OpenReadCompletedEventHandler(CallbackMethod); 
wc.OpenReadAsync(uri); 

當數據從服務器檢索, CallbackMethod被調用。該方法有一個EventArgs對象,其中包含一個名爲result的屬性。您可以使用該屬性獲取您的數據。

0

Silverlight並不需要ASP函數,如果你有一個單獨的服務器上的數據庫檢出WCF,然後讓Silverlight與WCF服務通信,然後與數據庫通信,如果你想要更透明的東西,然後嘗試WCF RIA服務,這可以讓你在Silverlight中擁有一箇中等層次的數據訪問方法

相關問題