有关AutoCompleteBox组件的研究[5][Final]_集成搜索引擎搜索建议(Search Suggestion)——Silverlight学习笔记[40]...
在AutoCompleteBox組件中集成搜索引擎的功能是十分常見(jiàn)的,這有助于我們更好地與Web進(jìn)行交互。本文將為大家講述如何在在AutoCompleteBox組件中集成搜索引擎的搜索建議。
?
實(shí)例:
說(shuō)明:本實(shí)例用的搜索引擎是微軟的Bing(必應(yīng))
詳細(xì)的說(shuō)明在代碼中給出。
WebServiceHelper.cs(業(yè)務(wù)輔助類)文件代碼:
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="請(qǐng)輸入搜索詞:" 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();
??????????? //注冊(cè)事件觸發(fā)處理
??????????? this.Loaded += new RoutedEventHandler(MainPage_Loaded);
??????? }
?
??????? void MainPage_Loaded(object sender, RoutedEventArgs e)
??????? {
??????????? if (WebServiceHelper.CanMakeHttpRequests)//如果能做Http請(qǐng)求
??????????? {
??????????????? //注冊(cè)AutoCompleteBox組件的下拉框內(nèi)容正在生成事件觸發(fā)處理
??????????????? Search.Populating += Search_Populating;
??????????????? //建立打開結(jié)果窗口的事件委托
??????????????? Action go = () => HtmlPage.Window.Navigate(WebServiceHelper.CreateWebSearchUri(Search.Text), "_blank");
??????????????? //注冊(cè)AutoCompleteBox組件的KeyUp事件觸發(fā)處理。當(dāng)按下Enter鍵時(shí),相當(dāng)于提交
??????????????? Search.KeyUp += (s, args) =>
??????????????? {
??????????????????? if (args.Key == System.Windows.Input.Key.Enter)
??????????????????? {
??????????????????????? go();
??????????????????? }
??????????????? };
??????????????? //搜素按鈕的事件觸發(fā)處理
??????????????? GoSearch.Click += (s, args) => go();
??????????? }
??????? }
?
??????? private void Search_Populating(object sender, PopulatingEventArgs e)
??????? {
??????????? AutoCompleteBox autoComplete = (AutoCompleteBox)sender;
?
??????????? //等待結(jié)果
??????????? e.Cancel = true;
?
??????????? //創(chuàng)建一個(gè)搜索建議請(qǐng)求
??????????? 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))//如果沒(méi)有出現(xiàn)異常情況的話
??????????? {
??????????????? List<string> data = new List<string>();
??????????????? try
??????????????? {
????????????? ??????JsonObject jso = ((JsonObject)JsonObject.Parse(e.Result))["SearchSuggestion"] as JsonObject;//創(chuàng)建Json對(duì)象
??????????????????? string originalSearchString = jso["Query"];//原始查詢字符串
??????????????????? if (originalSearchString == autoComplete.SearchText)
??????????????????? {
??????????????????????? foreach (JsonObject suggestion in (JsonArray)jso["Section"])
??????????????????????? {
??????????????????????????? data.Add(suggestion.Values.First());
??????????????????????? }
???????????????????????
??????? ????????????????autoComplete.ItemsSource = data;//填充AutoCompleteBox組件的數(shù)據(jù)源
??????????????????????? autoComplete.PopulateComplete();//結(jié)束AutoCompleteBox組件的下拉框內(nèi)容生成
??????????????????? }
??????????????? }
??????????????? catch
??????????????? {
??????????????? }
??????????? }
??????? }
??? }
}
?
最終效果圖:
?
作者:Kinglee文章出處:Kinglee’s Blog (http://www.cnblogs.com/Kinglee/)
版權(quán)聲明:本文的版權(quán)歸作者與博客園共有。轉(zhuǎn)載時(shí)須注明本文的詳細(xì)鏈接,否則作者將保留追究其法律責(zé)任。
轉(zhuǎn)載于:https://www.cnblogs.com/Kinglee/archive/2009/09/30/1577106.html
總結(jié)
以上是生活随笔為你收集整理的有关AutoCompleteBox组件的研究[5][Final]_集成搜索引擎搜索建议(Search Suggestion)——Silverlight学习笔记[40]...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 按 结构记录的 相关字段 快速排序
- 下一篇: 图形图像显示研究(一)