.NEt专家博客!
【李会军】一步一步学Silverlight 2系列(13):数据与通信之WebRequest
概述
Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, Ironpython,对JSON、Web Service、WCF以及Sockets的支持等一系列新的特性。《一步一步学Silverlight 2系列》文章带您快速进入Silverlight 2开发。
本文将简单介绍在Silverlight 2中如何使用WebRequest进行数据的提交和获取。
简单示例
在本文中,我们仍然使用在一步一步学Silverlight 2系列(12):数据与通信之WebClient中用过的示例,只不过稍微做一点小的改动,使用WebRequest提交书籍编号数据,并根据书籍号返回价格信息。最终运行的结果如下图:
F3o^7u.BCs0
:tWy{Y(E0
编写界面布局,XAML如下:
<GridBackground="#46461F">ITPUB个人空间lBz I+`R7_编写HttpHandler,注意我使用了context.Request.Form["No"],在后面我们将使用WebRequest在RequestReady方法中将数据写入请求流:
<Grid.RowDefinitions>ITPUB个人空间 jw8SA%eV
<RowDefinitionHeight="40"></RowDefinition>
B{5q ];sh0<RowDefinitionHeight="*"></RowDefinition>
5f4B@)J7Go!O;X0<RowDefinitionHeight="40"></RowDefinition>
y-Nh]"Ms:ja&c-Q0</Grid.RowDefinitions>
*I*rt0P Zo:v0<Grid.ColumnDefinitions>ITPUB个人空间I%e'~}(D`%@
<ColumnDefinition></ColumnDefinition>
B_ be*?0</Grid.ColumnDefinitions>
ig WV F6j:L2p0<BorderGrid.Row="0"Grid.Column="0"CornerRadius="15"ITPUB个人空间@5pMbZ,g9P
Width="240"Height="36"
E(G` Io2vOMED0Margin="20 0 0 0"HorizontalAlignment="Left">
aJ,bWj"b!J[0<TextBlockText="书籍列表"Foreground="White"ITPUB个人空间L e6]6]%D}.@
HorizontalAlignment="Left"VerticalAlignment="Center"ITPUB个人空间Q }a wK#Z+P.ha`
Margin="20 0 0 0"></TextBlock>
,n)j%?O~0</Border>ITPUB个人空间$zf({9N2wz {
<ListBoxx:Name="Books"Grid.Row="1"Margin="40 10 10 10"ITPUB个人空间3KfI'HDK,p4Y
SelectionChanged="Books_SelectionChanged">ITPUB个人空间 ]$s Y*i;vJ5B-Q:FP
<ListBox.ItemTemplate>ITPUB个人空间c{{~$z-M
<DataTemplate>ITPUB个人空间;s/J ~z(xt/p
<StackPanel>
'Cd3R;W4ZgE0<TextBlockText="{BindingName}"Height="32"></TextBlock>ITPUB个人空间D!e$`Pt/a
</StackPanel>ITPUB个人空间1iNS2y`DteVm
</DataTemplate>
9KZ3H$U0h i)D5m*I0</ListBox.ItemTemplate>ITPUB个人空间0jD)}3M8RJ
</ListBox>ITPUB个人空间H)Z3] f*Ga
<BorderGrid.Row="2"Grid.Column="0"CornerRadius="15"ITPUB个人空间}E(uSCF)[
Width="240"Height="36"Background="Orange"ITPUB个人空间 S+O4y6w*p5AO(jn
Margin="20 0 0 0"HorizontalAlignment="Left">
U[4kD|kUl8r0<TextBlockx:Name="lblPrice"Text="价格:"Foreground="White"
D6{-S8~:h}z Fr0HorizontalAlignment="Left"VerticalAlignment="Center"ITPUB个人空间 AM/P$c[ }
Margin="20 0 0 0"></TextBlock>
V7PH&Z4IRp0</Border>
c5ku;B&V}(\2v/L0</Grid>
public classBookHandler:IHttpHandler
%DwlIh l,u#j0{
]GZ4_MKM u1j y0public static readonly string[] PriceList =new string[] {ITPUB个人空间;pd,E9S rJ'Q"D7c+f$W L
"66.00",
@V:bWC| Xn~0"78.30",ITPUB个人空间*o6nWOQKeZ@.j
"56.50",