一步一步学Silverlight 2系列(12):数据与通信之WebClient
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                一步一步学Silverlight 2系列(12):数据与通信之WebClient
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                | 版權聲明:原創作品,允許轉載,轉載時請務必以超鏈接形式標明文章 原始出處 、作者信息和本聲明。否則將追究法律責任。http://terrylee.blog.51cto.com/342737/67249 | 
| 概述Silverlight 2 Beta 1版本發布了,無論從Runtime還是Tools都給我們帶來了很多的驚喜,如支持框架語言Visual Basic, Visual C#, IronRuby, Ironpython,對JSON、Web Service、WCF以及Sockets的支持等一系列新的特性。《一步一步學Silverlight 2系列》文章帶您快速進入Silverlight 2開發。 本文將介紹如何在Silverlight 2中使用Web Client進行通信。簡單示例編寫一個簡單的示例,在該示例中,選擇一本書籍之后,我們通過Web Client去查詢書籍的價格,并顯示出來,最終的效果如下: ? 編寫界面布局,XAML如下: <Grid Background="#46461F"><Grid.RowDefinitions><RowDefinition Height="40"></RowDefinition><RowDefinition Height="*"></RowDefinition><RowDefinition Height="40"></RowDefinition></Grid.RowDefinitions><Grid.ColumnDefinitions><ColumnDefinition></ColumnDefinition></Grid.ColumnDefinitions><Border Grid.Row="0" Grid.Column="0" CornerRadius="15"Width="240" Height="36"Margin="20 0 0 0" HorizontalAlignment="Left"><TextBlock Text="書籍列表" Foreground="White"HorizontalAlignment="Left" VerticalAlignment="Center"Margin="20 0 0 0"></TextBlock></Border><ListBox x:Name="Books" Grid.Row="1" Margin="40 10 10 10"SelectionChanged="Books_SelectionChanged"><ListBox.ItemTemplate><DataTemplate><StackPanel><TextBlock Text="{Binding Name}" Height="32"></TextBlock></StackPanel></DataTemplate></ListBox.ItemTemplate></ListBox><Border Grid.Row="2" Grid.Column="0" CornerRadius="15"Width="240" Height="36" Background="Orange"Margin="20 0 0 0" HorizontalAlignment="Left"><TextBlock x:Name="lblPrice" Text="價格:" Foreground="White"HorizontalAlignment="Left" VerticalAlignment="Center"Margin="20 0 0 0"></TextBlock></Border></Grid> 為了模擬查詢價格,我們編寫一個HttpHandler,接收書籍的No,并返回價格: public class BookHandler : IHttpHandler{public static readonly string[] PriceList = new string[] {"66.00","78.30","56.50","28.80","77.00"};public void ProcessRequest(HttpContext context){context.Response.ContentType = "text/plain";context.Response.Write(PriceList[Int32.Parse(context.Request.QueryString["No"])]);}public bool IsReusable{get{return false;}}} 在界面加載時綁定書籍列表,關于數據綁定可以參考一步一步學Silverlight 2系列(11):數據綁定。 void UserControl_Loaded(object sender, RoutedEventArgs e){List<Book> books = new List<Book>() {new Book("Professional ASP.NET 3.5"),new Book("ASP.NET AJAX In Action"),new Book("Silverlight In Action"),new Book("ASP.NET 3.5 Unleashed"),new Book("Introducing Microsoft ASP.NET AJAX")};Books.ItemsSource = books;} 接下來當用戶選擇一本書籍時,需要通過Web Client去獲取書籍的價格,在Silverlight 2中,所有的網絡通信API都設計為了異步模式。在聲明一個Web Client實例后,我們需要為它注冊DownloadStringCompleted事件處理方法,在下載完成后將會被回調,然后再調用DownloadStringAsync方法開始下載。 void Books_SelectionChanged(object sender, SelectionChangedEventArgs e){Uri endpoint = new Uri(String.Format("http://localhost:49955/BookHandler.ashx?No={0}",Books.SelectedIndex));WebClient client = new WebClient();client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);client.DownloadStringAsync(endpoint);}void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e){if (e.Error == null){lblPrice.Text = "價格:" + e.Result;}else{lblPrice.Text = e.Error.Message;}} 注意大家可以在Web Application Project的屬性頁中,把ASP.NET Development Server的端口號設置為一個固定的端口號: ? 最后完整的代碼如下: public partial class Page : UserControl{public Page(){InitializeComponent();}void UserControl_Loaded(object sender, RoutedEventArgs e){List<Book> books = new List<Book>() {new Book("Professional ASP.NET 3.5"),new Book("ASP.NET AJAX In Action"),new Book("Silverlight In Action"),new Book("ASP.NET 3.5 Unleashed"),new Book("Introducing Microsoft ASP.NET AJAX")};Books.ItemsSource = books;}void Books_SelectionChanged(object sender, SelectionChangedEventArgs e){Uri endpoint = new Uri(String.Format("http://localhost:49955/BookHandler.ashx?No={0}",Books.SelectedIndex));WebClient client = new WebClient();client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);client.DownloadStringAsync(endpoint);}void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e){if (e.Error == null){lblPrice.Text = "價格:" + e.Result;}else{lblPrice.Text = e.Error.Message;}}} 運行后效果如下: ? 當我們選擇其中一本書籍時,將會顯示出它的價格: ?結束語本文簡單介紹了Silverlight 2中使用Web Client進行通信的知識,在Silverlight 2中,提供的通信API非常豐富,后面將會介紹其他的方式。你可以從這里下載本文示例代碼。本文出自 “TerryLee技術專欄” 博客,請務必保留此出處http://terrylee.blog.51cto.com/342737/67249本文出自 51CTO.COM技術博客 | 
轉載于:https://www.cnblogs.com/hdjjun/archive/2008/12/24/1361491.html
總結
以上是生活随笔為你收集整理的一步一步学Silverlight 2系列(12):数据与通信之WebClient的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: ASP.NET中数据库数据导入Excel
- 下一篇: cocos2dx 引入 libpomel
