概述Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, Ironpython,对JSON、Web Service、WCF以及Sockets的支持等一系列新的特性。《一步一步学Silverlight 2系列》文章带您快速进入Silverlight 2开发。
øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø本文将简单介绍在Silverlight 2中如何使用WebRequest进行数据的提交和获取。
øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø简单示例在本文中,我们仍然使用在
一步一步学Silverlight 2系列(12):数据与通信之WebClient中用过的示例,只不过稍微做一点小的改动,使用WebRequest提交书籍编号数据,并根据书籍号返回价格信息。最终运行的结果如下图:
øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø编写界面布局,XAML如下:
øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø<Grid Background="#46461F"> øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø<Grid.RowDefinitions> øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø<RowDefinition Height="40">øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø</RowDefinition> øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø<RowDefinition Height="*">øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø</RowDefinition> øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø<RowDefinition Height="40">øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø</RowDefinition> øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø</Grid.RowDefinitions> øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø<Grid.ColumnDefinitions> øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø<ColumnDefinition>øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø</ColumnDefinition> øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø</Grid.ColumnDefinitions> øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø<Border Grid.Row="0" Grid.Column="0" CornerRadius="15" Width="240" Height="36" Margin="20 0 0 0" HorizontalAlignment="Left"> øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø<TextBlock Text="书籍列表" Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="20 0 0 0">øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø</TextBlock> øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø</Border> øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø<ListBox x:Name="Books" Grid.Row="1" Margin="40 10 10 10" SelectionChanged="Books_SelectionChanged"> øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø<ListBox.ItemTemplate> øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø<DataTemplate> øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø<StackPanel> øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø<TextBlock Text="{Binding Name}" Height="32"></TextBlock> øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø</StackPanel> øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø</DataTemplate> øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø</ListBox.ItemTemplate> øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø</ListBox> øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø<Border Grid.Row="2" Grid.Column="0" CornerRadius="15" Width="240" Height="36" Background="Orange" Margin="20 0 0 0" HorizontalAlignment="Left"> øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø<TextBlock x:Name="lblPrice" Text="价格:" Foreground="White" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="20 0 0 0">øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø</TextBlock> øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø</Border>øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø</Grid>øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø编写HttpHandler,注意我使用了context.Request.Form["No"],在后面我们将使用WebRequest在RequestReady方法中将数据写入请求流:
øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔøøÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔøpublic class BookHandler :
IHttpHandlerøÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø{
øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø public static readonly string[] PriceList =
new string[]{
"66.00",
"78.30",
"56.50",
"28.80",
"77.00"};
øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø public void ProcessRequest(
HttpContext context)
øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø {
øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔøcontext.Response.ContentType = "text/plain"; øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
context.Response.Write(PriceList[Int32.Parse(context.Request.Form["No"])]);øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
} øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
public bool IsReusable øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
{ øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
get øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
{ øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
return false; øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
} øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
}øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
}
øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔøøÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø在界面加载时绑定书籍列表,关于数据绑定可以参考
一步一步学Silverlight 2系列(11):数据绑定。
øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔøøÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔøprivate void UserControl_Loaded(
object sender,
RoutedEventArgs e)
øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø{
øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔøList<Book> books = new List<Book>() øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
{øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
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")øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
};øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
Books.ItemsSource = books;øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
}
øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔøøÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø接下来在SelectionChanged事件中实现用户选择书籍时,我们使用WebRequest提交书籍编号,并且获得价格数据,仍然采用异步模式,提供RequestReady和ResponseReady两个回调函数:
øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔøøÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔøprivate string bookNo;
void Books_SelectionChanged(
object sender,
SelectionChangedEventArgs e)
øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø{
øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔøbookNo = Books.SelectedIndex.ToString(); øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
Uri endpoint = new Uri(http://localhost:49955/BookHandler.ashx);øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
WebRequest request = WebRequest.Create(endpoint); øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
request.Method = "POST"; øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
request.ContentType = "application/x-www-form-urlencoded"; øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
request.BeginGetRequestStream(new AsyncCallback(RequestReady), request); request.BeginGetResponse(new AsyncCallback(ResponseReady), request); øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
}
øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔøøÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø实现RequestReady方法,将书籍的编号写入请求流中。
øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔøøÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔøvoid RequestReady(
IAsyncResult asyncResult)
øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø{
øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔøWebRequest request = asyncResult.AsyncState as WebRequest; øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
Stream requestStream = request.EndGetRequestStream(asyncResult); øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
using (StreamWriter writer = new StreamWriter(requestStream)) øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
{ øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
writer.Write(String.Format("No={0}", bookNo)); øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
writer.Flush();øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
}øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
}
øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔøøÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø实现ResponseReady方法,显示返回的结果。
øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔøøÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔøvoid ResponseReady(
IAsyncResult asyncResult)
øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø{
øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔøWebRequest request = asyncResult.AsyncState as WebRequest; øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
WebResponse response = request.EndGetResponse(asyncResult); øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
using (Stream responseStream = response.GetResponseStream()) øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
{ øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
StreamReader reader = new StreamReader(responseStream); øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
lblPrice.Text = "价格:" + reader.ReadToEnd(); øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
}øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
}
øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔøøÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø最后运行的结果如下:
øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø用户选择一本书籍后,将显示其价格:
øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø
øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø结束语本文简单介绍了在Silverlight 2中如何使用WebRequest提交和获取数据,你可以从
这里下载示例程序。
øÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø作者:
TerryLeeøÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø出处:
http://terrylee.cnblogs.comøÕc:Êg°ðÍwww.netcsharp.cn¥3³á¢íÔø