大佬教程收集整理的这篇文章主要介绍了一步一步学Silverlight 2系列(12):数据与通信之WebClient,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
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" @H_258_37@margin="20 0 0 0" HorizontalAlignment="Left"> <TextBlock Text="书籍列表" Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Center" @H_258_37@margin="20 0 0 0"></TextBlock> </Border> <ListBox x:Name="Books" Grid.Row="1" @H_258_37@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" @H_258_37@margin="20 0 0 0" HorizontalAlignment="Left"> <TextBlock x:Name="lblPrice" Text="价格:" Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Center" @H_258_37@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("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非常丰富,后面将会介绍其他的方式。你可以从这里下载本文示例代码。
以上是大佬教程为你收集整理的一步一步学Silverlight 2系列(12):数据与通信之WebClient全部内容,希望文章能够帮你解决一步一步学Silverlight 2系列(12):数据与通信之WebClient所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。