概述 Silverlight 2 Beta 1版本发布了无论从Runtime还是Tools都给我们带来了很多惊喜如支持框架语言Visual Basic, Visual C#, IronRuby, Ironpython对JSON、Web Service、WCF以及Sockets支持等系列新特性步步学Silverlight 2系列文章将从Silverlight 2基础知识、数据和通信、自定义Control控件、动画、图形图像等几个方面带您快速进入Silverlight 2开发
本节将综合前面几篇介绍和浏览器交互部分内容做个综合举例——Live Search
准备知识 在本举例中我们将通过Live Search API在Silverlight中动态创建DOM结构将搜索结果展现出来在使用Live Search API的前需要先去Live Search Developer Center申请个应用ID
申请完成后应用ID大约在10分钟左右生效有关Live Search API有关详细信息请大家参考这里
编写ASMX 直接API返回信息可能有很多为了简单起见我们对返回结果做些处理编写个SearchResultItem类:
public SearchResultItem
{
public Title { get; ; }
public Url { get; ; }
public Description { get; ; }
}
添加对Live Search APIService引用地址为:http://soap.search.live.com/webservices.asmx?wsdl在ASMX中对返回结果进行些处理Silverlight最后将直接ASMX在Live Search时需要指定应用ID以及本地化信息等查询参数将在Silverlight中时传入
[WebMethod]
public SearchResultItem DoSearch( query)
{
MSNSearchPortTypeClient s = MSNSearchPortTypeClient;
SearchRequest searchRequest = SearchRequest;
.gif' />Size = 1;
SourceRequest sr = SourceRequest[.gif' />Size];
sr[0] = 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 = List<SearchResultItem>;
foreach (SourceResponse sourceResponse in searchResponse.Responses)
{
Result sourceResults = sourceResponse.Results;
foreach (Result sourceResult in sourceResults)
{
SearchResultItem item = SearchResultItem;
((sourceResult.Title != null) && (sourceResult.Title != String.Empty))
item.Title = sourceResult.Title;
((sourceResult.Description != null) && (sourceResult.Description != String.Empty))
item.Description = sourceResult.Description;
((sourceResult.Url != null) && (sourceResult.Url != String.Empty))
item.Url = sourceResult.Url;
lists.Add(item);
}
}
lists.ToArray;
}
测试下我们服务是否正常:修改测试页 在测试ASPX中修改Silverlight插件样式控制并添加个div用来显示搜索结果:
<div style="height:100%;">
<asp:Silverlight ID="Xaml1" runat="server"
Source="~/ClientBin/TerryLee.SilverlightGoogleSearch.xap"
Version="2.0" Width="857" Height="140" />
<div id="result"></div>
</div>
定义几个简单样式:<style type="text/css">
#result
{
margin-left:20px;
}
.urlstyle
{
color:#59990E;
}
.itemstyle
{
border-bottom:dotted 1px #59990E;
margin-bottom:20px;
}
</style>
实现Silverlight 编写个简单Silverlight界面使其看起来如图所示: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>
在Silverlight项目中添加对于ASMX引用并编写“Search”按钮实现对于如何ASMX可以参考步步学Silverlight 2系列(15):数据和通信的ASMX动态创建DOM结构并将结果显示出来:private void btnSearch_Click(object sender, RoutedEventArgs e)
{
LiveSearchWebServiceSoapClient client = LiveSearchWebServiceSoapClient;
client.DoSearchCompleted EventHandler<DoSearchCompletedEventArgs>(client_DoSearchCompleted);
client.DoSearchAsync(this.txtQuery.Text);
}
void client_DoSearchCompleted(object sender, DoSearchCompletedEventArgs e)
{
(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);
}
}
}
运行看下效果查询博客园:结束语 本文综合了前面有关浏览器集成以及数据和通信部分内容开发了个综合举例——Live Search你可以下载本文举例代码
最新评论