有关AutoCompleteBox组件的研究[5][Final]_集成搜索引擎搜索建议(Search Suggestion)——Silverlight学习笔记[40]...
在AutoCompleteBox組件中集成搜索引擎的功能是十分常見的,這有助于我們更好地與Web進行交互。本文將為大家講述如何在在AutoCompleteBox組件中集成搜索引擎的搜索建議。
?
實例:
說明:本實例用的搜索引擎是微軟的Bing(必應)
詳細的說明在代碼中給出。
WebServiceHelper.cs(業務輔助類)文件代碼:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Globalization;
using System.Diagnostics.CodeAnalysis;
using System.Net;
using System.Windows.Browser;
using System.Windows.Controls;
?
namespace SilverlightClient
{
?? public static class WebServiceHelper
??? {
??????? private const string LiveSuggestionsJsonUriFormat = "http://api.search.live.net/qson.aspx?query={0}";
??????? private const string LiveSearchUriFormat = "http://search.live.com/results.aspx?q={0}";
???????
??????? public static bool CanMakeHttpRequests
??????? {
??????????? get
?????????? ?{
??????????????? if (!HtmlPage.IsEnabled)
??????????????? {
??????????????????? return false;
??????????????? }
?
??????????????? string scheme = HtmlPage.Document.DocumentUri.Scheme ?? string.Empty;
??????????????? return string.Compare(scheme, "http", StringComparison.OrdinalIgnoreCase) == 0;
??????????? }
??????? }
?
??????? public static Uri CreateWebSearchUri(string searchText)
??????? {
??????????? return new Uri(string.Format(CultureInfo.InvariantCulture, LiveSearchUriFormat, HttpUtility.UrlEncode(searchText)));
??????? }
?
??????? public static Uri CreateWebSearchSuggestionsUri(string searchText)
??????? {
??????????? return new Uri(string.Format(CultureInfo.InvariantCulture, LiveSuggestionsJsonUriFormat, HttpUtility.UrlEncode(searchText)));
??????? }
??? }
}
?
MainPage.xaml文件代碼:
<UserControl
?? ?xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
?? ?xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
?? ?xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
?? ?mc:Ignorable="d" xmlns:input="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Input" x:Class="SilverlightClient.MainPage"
?? ?d:DesignWidth="320" d:DesignHeight="240">
??? <Grid x:Name="LayoutRoot" Width="320" Height="240" Background="White">
?
??????? <TextBlock Height="26" HorizontalAlignment="Left" Margin="8,8,0,0" VerticalAlignment="Top" Width="120" FontSize="16" Text="請輸入搜索詞:" TextWrapping="Wrap"/>
??????? <input:AutoCompleteBox x:Name="Search" FontSize="14" FilterMode="None" Height="26" Margin="8,55,112,0" VerticalAlignment="Top" Width="200"/>
??????? <Button x:Name="GoSearch" Height="26" HorizontalAlignment="Right" Margin="0,55,34,0" VerticalAlignment="Top" Width="74" Content="Search!" FontSize="13.333"/>
?
??? </Grid>
</UserControl>
?
MainPage.xaml.cs文件代碼:
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Json;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.ComponentModel;
?
namespace SilverlightClient
{
??? public partial class MainPage : UserControl
??? {
??????? public MainPage()
??????? {
??????????? InitializeComponent();
??????????? //注冊事件觸發處理
??????????? this.Loaded += new RoutedEventHandler(MainPage_Loaded);
??????? }
?
??????? void MainPage_Loaded(object sender, RoutedEventArgs e)
??????? {
??????????? if (WebServiceHelper.CanMakeHttpRequests)//如果能做Http請求
??????????? {
??????????????? //注冊AutoCompleteBox組件的下拉框內容正在生成事件觸發處理
??????????????? Search.Populating += Search_Populating;
??????????????? //建立打開結果窗口的事件委托
??????????????? Action go = () => HtmlPage.Window.Navigate(WebServiceHelper.CreateWebSearchUri(Search.Text), "_blank");
??????????????? //注冊AutoCompleteBox組件的KeyUp事件觸發處理。當按下Enter鍵時,相當于提交
??????????????? Search.KeyUp += (s, args) =>
??????????????? {
??????????????????? if (args.Key == System.Windows.Input.Key.Enter)
??????????????????? {
??????????????????????? go();
??????????????????? }
??????????????? };
??????????????? //搜素按鈕的事件觸發處理
??????????????? GoSearch.Click += (s, args) => go();
??????????? }
??????? }
?
??????? private void Search_Populating(object sender, PopulatingEventArgs e)
??????? {
??????????? AutoCompleteBox autoComplete = (AutoCompleteBox)sender;
?
??????????? //等待結果
??????????? e.Cancel = true;
?
??????????? //創建一個搜索建議請求
??????????? WebClient wc = new WebClient();
??????????? wc.DownloadStringCompleted += OnDownloadStringCompleted;
??????????? wc.DownloadStringAsync(WebServiceHelper.CreateWebSearchSuggestionsUri(autoComplete.SearchText), autoComplete);
??????? }
?
??????? private void OnDownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
??????? {
??????????? AutoCompleteBox autoComplete = e.UserState as AutoCompleteBox;
???????????
??????????? if (autoComplete != null && e.Error == null && !e.Cancelled && !string.IsNullOrEmpty(e.Result))//如果沒有出現異常情況的話
??????????? {
??????????????? List<string> data = new List<string>();
??????????????? try
??????????????? {
????????????? ??????JsonObject jso = ((JsonObject)JsonObject.Parse(e.Result))["SearchSuggestion"] as JsonObject;//創建Json對象
??????????????????? string originalSearchString = jso["Query"];//原始查詢字符串
??????????????????? if (originalSearchString == autoComplete.SearchText)
??????????????????? {
??????????????????????? foreach (JsonObject suggestion in (JsonArray)jso["Section"])
??????????????????????? {
??????????????????????????? data.Add(suggestion.Values.First());
??????????????????????? }
???????????????????????
??????? ????????????????autoComplete.ItemsSource = data;//填充AutoCompleteBox組件的數據源
??????????????????????? autoComplete.PopulateComplete();//結束AutoCompleteBox組件的下拉框內容生成
??????????????????? }
??????????????? }
??????????????? catch
??????????????? {
??????????????? }
??????????? }
??????? }
??? }
}
?
最終效果圖:
?
作者:Kinglee文章出處:Kinglee’s Blog (http://www.cnblogs.com/Kinglee/)
版權聲明:本文的版權歸作者與博客園共有。轉載時須注明本文的詳細鏈接,否則作者將保留追究其法律責任。
轉載于:https://www.cnblogs.com/Kinglee/archive/2009/09/30/1577106.html
總結
以上是生活随笔為你收集整理的有关AutoCompleteBox组件的研究[5][Final]_集成搜索引擎搜索建议(Search Suggestion)——Silverlight学习笔记[40]...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 按 结构记录的 相关字段 快速排序
- 下一篇: 图形图像显示研究(一)