演练:连接到 Web 服务中的数据(Windows 窗体)
本演练演示如何使用数据源配置向导将应用程序连接到 Web 服务。 您将从应用程序中连接到 Live Search Web 服务并运行搜索查询。 该服务返回的数据(搜索结果)将显示在 Windows 窗体中。
本演练涉及以下任务:
创建新的**“Windows 应用程序”**项目。
将服务引用添加到应用程序(连接到 Live Search 服务。)
添加控件以运行搜索查询(调用服务公开的方法)。
编写代码以访问服务和返回数据。
将服务返回的数据绑定到 BindingSource。
在网格中显示服务返回的数据。
获取 AppID
Live Search 中的 AppID 免费提供,并向 Live Search 服务唯一标识您的应用程序。 需要 AppID 才能访问服务。
获取 AppID
导航到 http://search.live.com/developer 并获取免费的 AppID
提示
向服务验证 AppID 可能需要一段时间(30-60 分钟)。 如果在这段时间内运行应用程序时遇到一般“客户端错误”,则可能 AppID 仍处于在 Live Search 服务器上启用的过程中。
创建项目
创建新项目
从**“文件”**菜单创建一个新的项目。
选择**“Windows 窗体应用程序”**,并将其命名为 WebServiceWalkthrough。
单击**“确定”**。
项目即被创建并添加到**“解决方案资源管理器”**中。
连接到服务
通过运行**“数据源配置向导”**连接到 Web 服务。
连接到 Live Search Web 服务
在**“数据”菜单上,单击“显示数据源”**。
在**“数据源”窗口中,选择“添加新数据源”**。
在**“选择数据源类型”页上选择“服务”,然后单击“下一步”**。
在**“添加服务引用”对话框的“URL”**框中键入 https://soap.search.msn.com/webservices.asmx?wsdl。
单击**“转到”**。
找到 Web 服务后,将命名空间更改为:LiveSearchService。
单击**“确定”,然后单击“完成”**将服务引用添加到项目。
即会将服务引用添加到项目,并且将基于该服务返回的项填充**“数据源”**窗口。
提示
由于不同的 Web 服务公开不同的功能,因此,本演练中接下来的步骤将特定于使用 Live Search Web 服务。 使用服务中的数据的典型过程是:创建服务的实例,然后调用服务公开的方法。 从“数据源”窗口拖出项目后,您的窗体应包含一个 BindingSource 组件;将 DataSource 属性设置为服务返回的数据。
创建 DataGridView 以显示服务返回的数据
通过将某些项从**“数据源”**窗口拖到窗体上来创建数据绑定数据网格。 添加网格后,配置各列以仅显示需要呈现的列。 然后,将“Url”列设置为一个链接,以便用户能够单击 url 并导航到搜索查询返回的网站。
创建数据绑定 DataGridView
在**“数据源”窗口中展开“SearchResponse”**节点。
展开**“Responses”**节点。
将**“Results"**节点拖到窗体上。
即会将 DataGridView、BindingSource 和 BindingNavigator 添加到窗体。
选择已添加到窗体的 resultsDataGridView。
在**“属性”窗口中,选择“列”**属性,然后单击省略号 (…) 以打开“编辑列”对话框(“设计”视图)。
选择**“Url”**列并进行以下设置:
将**“ColumnType”属性设置为“DataGridViewLinkColumn”**。
将**“AutoSizeMode”属性设置为“AllCells”**。
移除除**“标题”、“说明”和“Url”**列之外的所有其他列。
单击**“确定”**。
添加用于输入搜索条件和运行搜索查询的控件
向现有工具条中添加用于运行搜索查询的控件。
向窗体中添加文本框和按钮
右击窗体工具条上灰显的磁盘图标,并依次选择“插入”、“文本框”。
在**“属性”窗口中,将“Name”**属性设置为 searchCriteriaTextBox。
将**“Text”**属性设置为 Visual Studio。
向工具条中添加一个按钮,并将其命名为 searchButton。
在**“属性”窗口中,将“DisplayStyle”** 属性设置为**“Text”**。
将**“Text”**属性设置为 Search。
创建事件处理程序以打开在网格中单击的网站
为 CellContentClick 事件添加事件处理程序。
创建 CellContentClick 事件处理程序
在窗体上选择 resultsDataGridView,并在**“属性”**窗口中单击“事件”按钮。 “事件”按钮带有闪电形图标。
双击**“CellContentClick”**事件以创建并导航到处理程序存根。
添加代码以检查单击了哪个列,并在单击了“Url”列的情况下导航到网页。
Private Sub ResultsDataGridView_CellContentClick( _ ByVal sender As System.Object, _ ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _ Handles ResultsDataGridView.CellContentClick ' When the content in a cell is clicked check to see if it is the Url column. ' If it is, pass the url to the Process.Start method to open the web page. If ResultsDataGridView.Columns(e.ColumnIndex).DataPropertyName = "Url" Then System.Diagnostics.Process.Start(ResultsDataGridView.SelectedCells(0).Value) End If End Sub
private void resultsDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e) { // When the content in a cell is clicked check to see if it is the Url column. // If it is, pass the url to the Process.Start method to open the web page. if (resultsDataGridView.Columns[e.ColumnIndex].DataPropertyName == "Url") { System.Diagnostics.Process.Start(resultsDataGridView.SelectedCells[0].Value.ToString()); } }
添加代码以访问 Live Search 服务并运行搜索查询
通过在应用程序中实例化服务的实例并调用服务公开的方法来访问服务。
访问和使用服务
在代码编辑器中打开 Form1。
将以下方法添加到 Form1:
Private Sub RunSearchRequest() ' Create an instance of the service. Dim searchService As New LiveSearchService.MSNSearchPortTypeClient ' Instantiate a new SearchRequest. Dim searchRequest As New LiveSearchService.SearchRequest ' Create a new SourceRequest. Dim sourceRequest(1) As LiveSearchService.SourceRequest sourceRequest(0) = New LiveSearchService.SourceRequest ' To search the web, set the SourceType to Web. sourceRequest(0).Source = LiveSearchService.SourceType.Web ' Set the columns you want the query to return. sourceRequest(0).ResultFields = _ LiveSearchService.ResultFieldMask.Description And _ LiveSearchService.ResultFieldMask.Url And _ LiveSearchService.ResultFieldMask.Title ' Search for the text in the textbox. searchRequest.Query = searchCriteriaTextBox.Text ' Set the SearchRequest to the SourceRequest array. searchRequest.Requests = sourceRequest ' Replace with a valid AppID. Obtain a free AppID at: ' http://search.live.com/developer searchRequest.AppID = "AppID" searchRequest.CultureInfo = "en-US" ' Create a SearchResponse, then call the Search method ' and assign the return value to the response object. Dim searchResponse As LiveSearchService.SearchResponse = _ searchService.Search(searchRequest) ' Bind the results to the form's BindingSource. ResultsBindingSource.DataSource = searchResponse.Responses(0).Results End Sub
private void RunSearchRequest() { // Create an instance of the service. LiveSearchService.MSNSearchPortTypeClient searchService = new LiveSearchService.MSNSearchPortTypeClient(); // Instantiate a new search request. LiveSearchService.SearchRequest searchRequest = new LiveSearchService.SearchRequest(); // Create a new SourceRequest. LiveSearchService.SourceRequest[] sourceRequest = new LiveSearchService.SourceRequest[1]; sourceRequest[0] = new LiveSearchService.SourceRequest(); // Set the number of results to return. sourceRequest[0].Count = 7; // To search the web, set the SourceType to Web. sourceRequest[0].Source = LiveSearchService.SourceType.Web; // Set the columns to be returned from the search query. sourceRequest[0].ResultFields = LiveSearchService.ResultFieldMask.Description | LiveSearchService.ResultFieldMask.Url | LiveSearchService.ResultFieldMask.Title; // Set the search query to the value in the text box. searchRequest.Query = searchCriteriaTextBox.Text; // Set the search request to the array of source requests. searchRequest.Requests = sourceRequest; // Replace with a valid AppID. Obtain a free AppID at: // http://search.live.com/developer searchRequest.AppID = "AppID"; searchRequest.CultureInfo = "en-US"; // Create a SearchResponse, then call the search method // and assign the return value to the response object. LiveSearchService.SearchResponse searchResponse = searchService.Search(searchRequest); // Bind the results from the search query to the form's BindingSource. resultsBindingSource.DataSource = searchResponse.Responses[0].Results; }
提示
确保将 searchRequest.AppID = "AppID" 替换为从 Live Search 服务获得的 AppID 值。
创建在单击了搜索按钮时运行搜索的事件处理程序
为 searchButton.Click 事件创建事件处理程序,并调用 RunSearchRequest 方法。
在单击了按钮时实现搜索
在设计视图中打开**“Form1”**。
双击**“搜索”**按钮。
在生成的处理程序中添加以下代码行:
Private Sub searchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles searchButton.Click RunSearchRequest() End Sub
private void searchButton_Click(object sender, EventArgs e) { RunSearchRequest(); }
运行应用程序
运行应用程序并执行搜索。
运行应用程序并执行搜索
运行应用程序(按 F5)。
单击**“搜索”以在 Web 中搜索“Visual Studio”**(searchCriteriaTextBox 中的默认文本)。
网格将显示前十个搜索结果。
单击 URL 之一以导航到该网站。
在文本框中键入 Redmond WA Weather,并单击**“搜索”**。
网格将使用新的搜索结果进行更新。
后续步骤
访问 Live Search 服务仅是了解如何利用 Windows 窗体应用程序中的数据绑定功能显示从服务返回的数据第一步。 连接到其他服务并将项从**“数据源”**窗口拖出后,您的窗体应包含一个 BindingSource 组件;将DataSource 属性设置为服务返回的数据。 有关更多信息,请参见 BindingSource 组件概述。
提示
“数据源”窗口中显示的项依赖于 Web 服务返回的信息。 某些 Web 服务可能没有为“数据源配置向导”创建可绑定的对象提供足够的信息。 例如,如果 Web 服务返回一个未提供任何可发现架构的对象,则在完成该向导时不会在“数据源”窗口中显示任何项。
在应用程序中添加功能
在**“数据源”**窗口中选择项并将其拖动到一个窗体上。 有关更多信息,请参见在 Visual Studio 中将 Windows 窗体控件绑定到数据。
在窗体上创建服务的实例。
将生成的 DataSource 属性设置为 Web 服务返回的数据。