2012-04-12 116 views
1

我一直在網上尋找一種簡單的方法來通過Silverlight應用程序調用Web服務,並且不能決定如何去做。連接到這個網站最簡單的方法是什麼:http://wsf.cdyne.com/WeatherWS/Weather.asmx並返回一個xml與指定的郵政編碼(我已經完成了使用AJAX,我只是想嘗試使用silverlight作爲替代工作)。很感謝任何形式的幫助!使用SilverLight連接到Web服務

謝謝!

回答

0

嗯..很簡單。在您的Silverligt項目中點擊「參考」選擇「添加服務參考..」並添加天氣服務的wsdl(http://wsf.cdyne.com/WeatherWS/Weather.asmx?WSDL)。 Visual Studio 11 Beta與basicHttpBinding生成WCF代理。並將其用作任何服務。

UODATE:添加下一個參考

using SilverlightApplication1.ServiceReference1; 

和實施。

 public MainPage() 
     { 
      InitializeComponent(); 

      var weather = new WeatherSoapClient(); 
      weather.GetWeatherInformationCompleted 
       += new EventHandler<GetWeatherInformationCompletedEventArgs>(OnGetWeatherInformationCompleted); 
      weather.GetWeatherInformationAsync(); 
     } 

     private void OnGetWeatherInformationCompleted(object sender, GetWeatherInformationCompletedEventArgs e) 
     { 
      // Get data from e.Result 
     } 

在我看來,你應該讀一些關於Web服務和WCF的東西。

+0

我已經做到了,但我不知道如何進行調用以及如何返回信息。 – Kevin 2012-04-12 21:35:35

+0

我是否將C#代碼放在服務器端?以及如何傳遞參數?萬分感謝! – Kevin 2012-04-12 22:11:04

+0

如果它是你的服務器,你可以。對於當前的服務實現,您可以不再傳遞郵政編碼。 – RredCat 2012-04-13 06:02:04