HOW TO:繫結至 Web 服務
這個範例顯示如何繫結至 Web 服務方法呼叫所傳回的物件。
範例
這個範例會使用 MSDN/TechNet 發行系統 (MTPS) 內容服務 (英文) 擷取指定的文件所支援的語言清單。
在呼叫 Web 服務之前,您必須先建立其參考。 若要使用 Microsoft Visual Studio 建立 MTPS 服務的 Web 參考,請遵循下列步驟:
在 Visual Studio 中開啟專案。
在 [專案] 功能表中,按一下 [加入 Web 參考]。
在對話方塊中,將 [URL] 設為 http://services.msdn.microsoft.com/contentservices/contentservice.asmx?wsdl。
按 [執行],再按 [加入參考]。
接著,您會呼叫 Web 服務方法,並將適當控制項或視窗的 DataContext 設為傳回的物件。 MTPS 服務的 GetContent 方法會採用 getContentRequest 物件的參考。 因此,下列範例會先設定要求物件:
' 1. Include the web service namespace
Imports BindtoContentService.com.microsoft.msdn.services
...
' 2. Set up the request object
' To use the MSTP web service, we need to configure and send a request
' In this example, we create a simple request that has the ID of the XmlReader.Read method page
Dim request As New getContentRequest()
request.contentIdentifier = "abhtw0f1"
' 3. Create the proxy
Dim proxy As New ContentService()
' 4. Call the web service method and set the DataContext of the Window
' (GetContent returns an object of type getContentResponse)
Me.DataContext = proxy.GetContent(request)
// 1. Include the web service namespace
using BindtoContentService.com.microsoft.msdn.services;
...
// 2. Set up the request object
// To use the MSTP web service, we need to configure and send a request
// In this example, we create a simple request that has the ID of the XmlReader.Read method page
getContentRequest request = new getContentRequest();
request.contentIdentifier = "abhtw0f1";
// 3. Create the proxy
ContentService proxy = new ContentService();
// 4. Call the web service method and set the DataContext of the Window
// (GetContent returns an object of type getContentResponse)
this.DataContext = proxy.GetContent(request);
在設定 DataContext 後,您可以針對由 DataContext 所設定而成的物件設定其屬性的繫結。 在這個範例中,DataContext 會設為 GetContent 方法所傳回的 getContentResponse 物件。 在下列範例中,ItemsControl 會繫結至並顯示 availableVersionsAndLocales 的 locale 值 getContentResponse。
<ItemsControl Grid.Column="1" Grid.Row="2" Margin="0,3,0,0"
ItemsSource="{Binding Path=availableVersionsAndLocales}"
DisplayMemberPath="locale"/>
如需 getContentResponse 結構的詳細資訊,請參閱內容服務文件 (英文)。