在 WCF 数据服务 快速入门的最后这个任务中,您将创建一个更高级的客户端应用程序,以使用在前一个任务中创建的 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 作为项目名称,然后单击**“确定”**。
打开文件 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,然后单击“确定”**。
这将在项目中添加一个新的代码文件,其中包含用于作为对象访问数据服务资源并与其交互的数据类。 这些数据类是在命名空间 NorthwindClient.Northwind 中创建的。
在 WPF 应用程序中访问数据服务数据
在**“解决方案资源管理器”中的“NorthwindClient”下,右击项目,然后单击“添加引用”**。
在“添加引用”对话框中,单击**“.NET”选项卡,选择“System.Data.Services.Client.dll”程序集,然后单击“确定”。 在“解决方案资源管理器”中的“NorthwindClient”**下,打开 MainWindow.xaml 文件的代码页,然后添加下列 using 语句(在 Visual Basic 中为 Imports)。
Imports System.Data.Services.Client Imports NorthwindClient.Northwind
using System.Data.Services.Client; using NorthwindClient.Northwind;
将用于查询数据服务并将结果绑定到 DataServiceCollection<T> 的下列代码插入到 MainWindow 类:
备注
必须用承载 Northwind 数据服务的实例的服务器和端口替换主机名 localhost:12345。
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 Sub
private 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 Sub
private 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 键启动该应用程序。
这将生成解决方案并启动客户端应用程序。 将从该服务请求数据并将数据绑定到控件。
编辑数据网格的**“数量”列中的值,然后单击“保存”**。
将更改保存到数据服务。
备注
此版本的 NorthwindClient 应用程序不支持添加和删除实体。
后续步骤
您已成功创建了用于访问 Northwind OData 示例源的客户端应用程序。 您还完成了 WCF 数据服务 快速入门。 有关以下内容的更多信息从 .NET Framework 应用程序访问 OData 源的更多信息,请参见数据客户端 (WCF Data Services)。