WCF Data Services クイック スタートのこの最終タスクでは、前のタスクで作成した Northwind サービスを使用するさらに高度なクライアント アプリケーションを作成します。 新しい Windows Presentation Foundation (WPF) アプリケーションをソリューションに追加して、書き込み可能な Northwind サービスに参照を追加し、生成されたクライアント データ サービス クラスおよびクライアント ライブラリを使用してクライアント アプリケーションから OData フィードにアクセスします。 次に、このアプリケーションを使用し、OData を使用して Northwind データを更新します。
注意
既定では、Visual Studio によって、コンピューター上の localhost URI にポート番号が自動的に割り当てられます。このタスクでは、URI の例でポート番号 12345 を使用しています。詳細情報: Visual Studio プロジェクトの指定のポート番号をセットする方法は、「Northwind データ サービスの作成 (WCF Data Services クイック スタート)」を参照してください。
Visual Studio を使用してクライアント アプリケーションを作成するには
ソリューション エクスプローラーでソリューションを右クリックし、[追加]、[新しいプロジェクト] の順にクリックします。
[プロジェクトの種類] で [Windows] をクリックして、[テンプレート] ペインで [WPF アプリケーション] を選択します。
プロジェクト名として「NorthwindClient」を入力し、[OK] をクリックします。
ファイル MainWindow.xaml を開き、XAML コードを次のコードに置き換えます。
<Window x:Class="MainWindow" xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" Title="Northwind Orders" Height="335" Width="425" Name="OrdersWindow" Loaded="Window1_Loaded"> <Grid Name="orderItemsGrid"> <ComboBox DisplayMemberPath="OrderID" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="true" Height="23" Margin="92,12,198,0" Name="comboBoxOrder" VerticalAlignment="Top"/> <DataGrid ItemsSource="{Binding Path=Order_Details}" CanUserAddRows="False" CanUserDeleteRows="False" Name="orderItemsDataGrid" Margin="34,46,34,50" AutoGenerateColumns="False"> <DataGrid.Columns> <DataGridTextColumn Header="Product" Binding="{Binding ProductID, Mode=OneWay}" /> <DataGridTextColumn Header="Quantity" Binding="{Binding Quantity, Mode=TwoWay}" /> <DataGridTextColumn Header="Price" Binding="{Binding UnitPrice, Mode=TwoWay}" /> <DataGridTextColumn Header="Discount" Binding="{Binding Discount, Mode=TwoWay}" /> </DataGrid.Columns> </DataGrid> <Label Height="28" Margin="34,12,0,0" Name="orderLabel" VerticalAlignment="Top" HorizontalAlignment="Left" Width="65">Order:</Label> <StackPanel Name="Buttons" Orientation="Horizontal" HorizontalAlignment="Right" Height="40" Margin="0,257,22,0"> <Button Height="23" HorizontalAlignment="Right" Margin="0,0,12,12" Name="buttonSave" VerticalAlignment="Bottom" Width="75" Click="buttonSaveChanges_Click">Save Changes </Button> <Button Height="23" Margin="0,0,12,12" Name="buttonClose" VerticalAlignment="Bottom" Width="75" Click="buttonClose_Click">Close</Button> </StackPanel> </Grid> </Window>
データ サービス参照をプロジェクトに追加するには
NorthwindClient プロジェクトを右クリックして、[サービス参照の追加]、[探索] の順にクリックします。
最初のタスクで作成した Northwind データ サービスが表示されます。
[名前空間] テキスト ボックスに「Northwind」と入力し、[OK] をクリックします。
プロジェクトに新しいコード ファイルが追加されます。このコード ファイルには、データ サービス リソースにアクセスし、オブジェクトとしてデータ サービス リソースと対話するデータ クラスが含まれています。 データ クラスは、名前空間 NorthwindClient.Northwind で作成されます。
WPF アプリケーションのデータ サービスにアクセスするには
NorthwindClient の下のソリューション エクスプローラーでプロジェクトを右クリックして、[参照の追加] をクリックします。
[参照の追加] ダイアログ ボックスで、[.NET] タブをクリックし、System.Data.Services.Client.dll アセンブリを選択して、[OK] をクリックします。 NorthwindClient の下のソリューション エクスプローラーで、MainWindow.xaml ファイルのコード ページを開き、次の using ステートメント (Visual Basic の場合は Imports) を追加します。
Imports System.Data.Services.Client Imports NorthwindClient.Northwindusing System.Data.Services.Client; using NorthwindClient.Northwind;次のコードを挿入します。このコードは、データ サービスをクエリし、DataServiceCollection<T> に対する結果を MainWindow クラスにバインドします。
注意
ホスト名 localhost:12345 を Northwind データ サービスのインスタンスをホストするサーバーとポートで置き換えます。
Private context As NorthwindEntities Private customerId As String = "ALFKI" ' Replace the host server and port number with the values ' for the test server hosting your Northwind data service instance. Private svcUri As Uri = New Uri("https://localhost:12345/Northwind.svc") Private Sub Window1_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs) Try ' Instantiate the DataServiceContext. context = New NorthwindEntities(svcUri) ' Define a LINQ query that returns Orders and ' Order_Details for a specific customer. Dim ordersQuery = From o In context.Orders.Expand("Order_Details") _ Where o.Customer.CustomerID = customerId _ Select o ' Create an DataServiceCollection(Of T) based on ' execution of the LINQ query for Orders. Dim customerOrders As DataServiceCollection(Of Order) = New _ DataServiceCollection(Of Order)(ordersQuery) ' Make the DataServiceCollection<T> the binding source for the Grid. Me.orderItemsGrid.DataContext = customerOrders Catch ex As Exception MessageBox.Show(ex.ToString()) End Try End Subprivate NorthwindEntities context; private string customerId = "ALFKI"; // Replace the host server and port number with the values // for the test server hosting your Northwind data service instance. private Uri svcUri = new Uri("https://localhost:12345/Northwind.svc"); private void Window1_Loaded(object sender, RoutedEventArgs e) { try { // Instantiate the DataServiceContext. context = new NorthwindEntities(svcUri); // Define a LINQ query that returns Orders and // Order_Details for a specific customer. var ordersQuery = from o in context.Orders.Expand("Order_Details") where o.Customer.CustomerID == customerId select o; // Create an DataServiceCollection<T> based on // execution of the LINQ query for Orders. DataServiceCollection<Order> customerOrders = new DataServiceCollection<Order>(ordersQuery); // Make the DataServiceCollection<T> the binding source for the Grid. this.orderItemsGrid.DataContext = customerOrders; } catch (Exception ex) { MessageBox.Show(ex.ToString()); } }次のコードを挿入します。このコードは、変更内容を MainWindow クラスに保存します。
Private Sub buttonSaveChanges_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) Try ' Save changes made to objects tracked by the context. context.SaveChanges() Catch ex As DataServiceRequestException MessageBox.Show(ex.ToString()) End Try End Sub Private Sub buttonClose_Click(ByVal sender As Object, ByVal a As RoutedEventArgs) Me.Close() End Subprivate void buttonSaveChanges_Click(object sender, RoutedEventArgs e) { try { // Save changes made to objects tracked by the context. context.SaveChanges(); } catch (DataServiceRequestException ex) { MessageBox.Show(ex.ToString()); } } private void buttonClose_Click(object sender, RoutedEventArgs e) { this.Close(); }
NorthwindClient アプリケーションをビルドして実行するには
ソリューション エクスプローラーで NorthwindClient プロジェクトを右クリックして [スタートアップ プロジェクトに設定] を選択します。.
F5 キーを押してアプリケーションを起動します。
ソリューションがビルドされ、クライアント アプリケーションが起動します。 データがサービスから要求され、コントロールにバインドされます。
データ グリッドの Quantity 列の値を編集し、[保存] をクリックします。
変更内容はデータ サービスに保存されます。
注意
このバージョンの NorthwindClient アプリケーションでは、エンティティの追加と削除はサポートされません。
次の手順
ここでは、サンプル Northwind の OData フィードにアクセスするクライアント アプリケーションを作成しました。 WCF Data Services クイック スタートも完了しました。 詳細情報: .NET Framework アプリケーションから OData フィードへのアクセスについては、「データ クライアント (WCF Data Services)」を参照してください。