zoukankan      html  css  js  c++  java
  • vs2010 学习Silverlight学习笔记(20):LiveSearch

    概要:

           这个例子是应用在API的一个实用的例子。也算是对一些API使用的例子吧。

    内容:

    1,  引入要使用的ServiceAPI。(SL,web都要有)

    2,  添加一个webService,用来解析返回的数据

    using System;

    usingSystem.Collections.Generic;

    usingSystem.Linq;

    usingSystem.Web;

    usingSystem.Web.Services;

    usingSystem.ComponentModel;

    using System.Data;

    usingSystem.Web.Services.Protocols;

    usingSystem.Xml.Linq;

    usingSLDemo25LiveSearch.Web.LiveSearchService;

    namespaceSLDemo25LiveSearch.Web

    {

        /// <summary>

        /// Summary description forLiveSearchWebService

        /// </summary>

        [WebService(Namespace ="http://tempuri.org/")]

        [WebServiceBinding(ConformsTo =WsiProfiles.BasicProfile1_1)]

        [System.ComponentModel.ToolboxItem(false)]

        // To allow this Web Service to be calledfrom script, using ASP.NET AJAX, uncomment the following line.

        //[System.Web.Script.Services.ScriptService]

        public class LiveSearchWebService :System.Web.Services.WebService

        {

            [WebMethod]

            public SearchResultItem[]DoSearch(string query)

            {

                MSNSearchPortTypeClient s = newMSNSearchPortTypeClient();

                SearchRequest searchRequest = newSearchRequest();

                int arraySize = 1;

                SourceRequest[] sr = newSourceRequest[arraySize];

                sr[0] = new SourceRequest();

                sr[0].Source = SourceType.Web;

                searchRequest.Query = query;

                searchRequest.Requests = sr;

                searchRequest.AppID ="C0680205851CCC0E38946DB8FF74156C1C826A86";

                searchRequest.CultureInfo ="zh-CN";

                SearchResponse searchResponse;

                searchResponse =s.Search(searchRequest);

                List<SearchResultItem> lists= new List<SearchResultItem>();

                foreach (SourceResponsesourceResponse in searchResponse.Responses)

                {

                    Result[] sourceResults =sourceResponse.Results;

                    foreach (Result sourceResult insourceResults)

                    {

                        SearchResultItem item = newSearchResultItem();

                        if ((sourceResult.Title !=null) && (sourceResult.Title != String.Empty))

                            item.Title =sourceResult.Title;

                        if((sourceResult.Description != null) && (sourceResult.Description !=String.Empty))

                            item.Description =sourceResult.Description;

                        if ((sourceResult.Url !=null) && (sourceResult.Url != String.Empty))

                            item.Url =sourceResult.Url;

                        lists.Add(item);

                    }

                }

                return lists.ToArray();

            }

        }

    }

    3,  为了显示方便,实例化一个类包含要使用的数据声明

    public class SearchResultItem
    {
        public string Title { get; set; }
     
        public string Url { get; set; }
     
        public string Description { get; set; }
    }
    4,xaml的布局
    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.RowDefinitions>
            <RowDefinition Height="55"></RowDefinition>
            <RowDefinition Height="50"></RowDefinition>
            <RowDefinition Height="35"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        
        <Image Source="LiveSearch.png" Grid.Column="0"></Image>
        <StackPanel Grid.Row="1" Orientation="Horizontal">
            <TextBox x:Name="txtQuery" Width="400" Height="35"
                     Margin="50 0 0 0" BorderBrush="#3F7801"></TextBox>
            <Button x:Name="btnSearch" Width="120" Height="35"
                    Background="#62A21D" Margin="20 0 0 0"
                    Content="Search" FontSize="16" Click="btnSearch_Click"></Button>
        </StackPanel>
        <TextBlock Grid.Row="2" Text="网页搜索结果" Foreground="#59990E"
                   FontSize="16" Margin="20 0 0 0"></TextBlock>
    </Grid>
     
    5,MainPage.Xaml.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Animation;
    using System.Windows.Shapes;
    using System.Windows.Browser;
    using SLDemo25LiveSearch.LiveSearchService;
     
    namespace SLDemo25LiveSearch
    {
        public partial class MainPage : UserControl
        {
            public MainPage()
            {
                InitializeComponent();
            }
            private void btnSearch_Click(object sender, RoutedEventArgs e)
            {
                LiveSearchWebServiceSoapClient client = new LiveSearchWebServiceSoapClient();
     
                client.DoSearchCompleted += new EventHandler<DoSearchCompletedEventArgs>(client_DoSearchCompleted);
                client.DoSearchAsync(this.txtQuery.Text);
            }
     
            void client_DoSearchCompleted(object sender, DoSearchCompletedEventArgs e)
            {
                if (e.Error == null)
                {
                    SearchResultItem[] results = e.Result as SearchResultItem[];
     
                    HtmlElement result = HtmlPage.Document.GetElementById("result");
     
                    foreach (SearchResultItem item in results)
                    {
                        HtmlElement itemElement = HtmlPage.Document.CreateElement("div");
                        itemElement.CssClass = "itemstyle";
     
                        HtmlElement titleElement = HtmlPage.Document.CreateElement("a");
                        titleElement.SetAttribute("href", item.Url);
                        titleElement.SetAttribute("innerText", item.Title);
     
                        HtmlElement descriptElement = HtmlPage.Document.CreateElement("div");
                        descriptElement.SetAttribute("innerText", item.Description);
     
                        HtmlElement urlElement = HtmlPage.Document.CreateElement("span");
                        urlElement.SetAttribute("innerText", item.Url);
                        urlElement.CssClass = "urlstyle";
     
                        itemElement.AppendChild(titleElement);
                        itemElement.AppendChild(descriptElement);
                        itemElement.AppendChild(urlElement);
     
                        result.AppendChild(itemElement);
                    }
                }
            }
        }
    }
     
     
    总结:
       这是一个很实用的例子,因为在没有联网的情况下,LiveService无法添加。在有网的时候要从新写一遍。
  • 相关阅读:
    [Angular] HostListener Method Arguments
    [Docker] Create Docker Volumes for Persistent Storage
    [Algorithms] Binary Search Algorithm using TypeScript
    [Node] Setup an Nginx Proxy for a Node.js App
    [Node] Run Local DevDependencies from the Command Line with npx
    [HTML] Change an HTML5 input's placeholder color with CSS
    [Node] Run Any Version of a Node Tool with npx
    [Angular] Learn How To Use ng-template Inputs
    [Angular] Learn Angular Multi-Slot Content Projection
    Jquery读取URL参数
  • 原文地址:https://www.cnblogs.com/yaoge/p/1822612.html
Copyright © 2011-2022 走看看