次の方法で共有


方法 : Web サービスにバインドする

この例では、Web サービス メソッド呼び出しによって返されるオブジェクトにバインドする方法について説明します。

使用例

この例では、MSDN/TechNet Publishing System (MTPS) コンテンツ サービスを使用して、指定されたドキュメントによってサポートされている言語の一覧を取得します。

Web サービスを呼び出す前に、その Web サービスへの参照を作成する必要があります。 Microsoft Visual Studio を使用して MTPS サービスへの Web 参照を作成するには、次の手順に従います。

  1. Visual Studio でプロジェクトを開いてみてください。

  2. [プロジェクト] メニューの [Web 参照の追加] をクリックします。

  3. ダイアログ ボックスで、[URL] を「http://services.msdn.microsoft.com/contentservices/contentservice.asmx?wsdl」に設定します。

  4. [移動] をクリックし、[参照の追加] をクリックします。

次に、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 が設定されているオブジェクトのプロパティへのバインディングを作成できます。 この例では、DataContextGetContent メソッドによって返された getContentResponse オブジェクトに設定されます。 次の例では、ItemsControlgetContentResponseavailableVersionsAndLocaleslocale 値にバインドされ、その値が表示されます。

<ItemsControl Grid.Column="1" Grid.Row="2" Margin="0,3,0,0"
              ItemsSource="{Binding Path=availableVersionsAndLocales}"
              DisplayMemberPath="locale"/>

getContentResponse の構造の詳細については、Content Service のドキュメントを参照してください。

参照

処理手順

方法 : XAML でデータをバインディング可能にする

概念

データ バインディングの概要

バインディング ソースの概要