日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

有关AutoCompleteBox组件的研究[5][Final]_集成搜索引擎搜索建议(Search Suggestion)——Silverlight学习笔记[40]...

發(fā)布時(shí)間:2024/1/17 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 有关AutoCompleteBox组件的研究[5][Final]_集成搜索引擎搜索建议(Search Suggestion)——Silverlight学习笔记[40]... 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。