学Silverlight 2系列(12):数据与通信之WebClient
2008-10-03 11:36:09 来源:WEB开发网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非常丰富,后面将会介绍其他的方式。你可以下载本文示例代码。
Tags:Silverlight 系列 数据
编辑录入:爽爽 [复制链接] [打 印]- ››数据库大型应用解决方案总结
- ››数据结构2--数组
- ››数据源架构模式的活动记录
- ››数据结构C#版线性表(Data Structure)之单链表(Lin...
- ››数据结构C#版线性表(Data Structure)之顺序表(顺序...
- ››Silverlight for Windows Phone 7开发系列(1):...
- ››Silverlight for Windows Phone 7开发系列(2):...
- ››Silverlight for Windows Phone 7开发系列(3):...
- ››Silverlight for Windows Phone 7开发系列(4):...
- ››数据就是利润:在欧洲两端,IBM Information Cham...
- ››数据架构师: 您要治理什么?
- ››数据驱动的网络营销和网站运营笔记
更多精彩
赞助商链接