中文字幕日韩一区二区_国产一区二区av_国产毛片av_久久久久国产一区_色婷婷电影_国产一区二区精品

一步一步學(xué)Silverlight :數(shù)據(jù)與通信之WebRequest

概述

Silverlight 2 Beta 1版本發(fā)布了,無(wú)論從Runtime還是Tools都給我們帶來(lái)了很多的驚喜,如支持框架語(yǔ)言Visual Basic, Visual C#, IronRuby, IronPython,對(duì)JSON、Web Service、WCF以及Sockets的支持等一系列新的特性。《一步一步學(xué)Silverlight 2系列》文章帶您快速進(jìn)入Silverlight 2開(kāi)發(fā)。

本文將簡(jiǎn)單介紹在Silverlight 2中如何使用WebRequest進(jìn)行數(shù)據(jù)的提交和獲取。

簡(jiǎn)單示例

在本文中,我們?nèi)匀皇褂迷?a >一步一步學(xué)Silverlight 2系列(12):數(shù)據(jù)與通信之WebClient中用過(guò)的示例,只不過(guò)稍微做一點(diǎn)小的改動(dòng),使用WebRequest提交書(shū)籍編號(hào)數(shù)據(jù),并根據(jù)書(shū)籍號(hào)返回價(jià)格信息。最終運(yùn)行的結(jié)果如下圖:

TerryLee_Silverlight2_0062

 

 

編寫(xiě)界面布局,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="書(shū)籍列表" 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="價(jià)格:" Foreground="White"                   HorizontalAlignment="Left" VerticalAlignment="Center"                   Margin="20 0 0 0"></TextBlock>    </Border></Grid>

編寫(xiě)HttpHandler,注意我使用了context.Request.Form["No"],在后面我們將使用WebRequest在RequestReady方法中將數(shù)據(jù)寫(xiě)入請(qǐng)求流:

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.Form["No"])]);    }    public bool IsReusable    {        get        {            return false;        }    }}

在界面加載時(shí)綁定書(shū)籍列表,關(guān)于數(shù)據(jù)綁定可以參考一步一步學(xué)Silverlight 2系列(11):數(shù)據(jù)綁定

private 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;}

接下來(lái)在SelectionChanged事件中實(shí)現(xiàn)用戶選擇書(shū)籍時(shí),我們使用WebRequest提交書(shū)籍編號(hào),并且獲得價(jià)格數(shù)據(jù),仍然采用異步模式,提供RequestReady和ResponseReady兩個(gè)回調(diào)函數(shù):

private string bookNo;void Books_SelectionChanged(object sender, SelectionChangedEventArgs e){    bookNo = Books.SelectedIndex.ToString();    Uri endpoint = new Uri("http://localhost:49955/BookHandler.ashx");    WebRequest request = WebRequest.Create(endpoint);    request.Method = "POST";    request.ContentType = "application/x-www-form-urlencoded";    request.BeginGetRequestStream(new AsyncCallback(RequestReady), request);    request.BeginGetResponse(new AsyncCallback(ResponseReady), request); }

實(shí)現(xiàn)RequestReady方法,將書(shū)籍的編號(hào)寫(xiě)入請(qǐng)求流中。

void RequestReady(IAsyncResult asyncResult){    WebRequest request = asyncResult.AsyncState as WebRequest;    Stream requestStream = request.EndGetRequestStream(asyncResult);    using (StreamWriter writer = new StreamWriter(requestStream))    {        writer.Write(String.Format("No={0}", bookNo));        writer.Flush();    }}

實(shí)現(xiàn)ResponseReady方法,顯示返回的結(jié)果。

void ResponseReady(IAsyncResult asyncResult){    WebRequest request = asyncResult.AsyncState as WebRequest;    WebResponse response = request.EndGetResponse(asyncResult);    using (Stream responseStream = response.GetResponseStream())    {        StreamReader reader = new StreamReader(responseStream);        lblPrice.Text = "價(jià)格:" + reader.ReadToEnd();    }}

最后運(yùn)行的結(jié)果如下:

TerryLee_Silverlight2_0059

用戶選擇一本書(shū)籍后,將顯示其價(jià)格:

TerryLee_Silverlight2_0062

結(jié)束語(yǔ)

本文簡(jiǎn)單介紹了在Silverlight 2中如何使用WebRequest提交和獲取數(shù)據(jù),你可以從這里下載示例程序。

下一篇:一步一步學(xué)Silverlight 2系列(14):數(shù)據(jù)與通信之WCF

NET技術(shù)一步一步學(xué)Silverlight :數(shù)據(jù)與通信之WebRequest,轉(zhuǎn)載需保留來(lái)源!

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

主站蜘蛛池模板: 日韩一区二区久久 | 精品久久久一区二区 | 欧美日韩在线一区二区三区 | 久久伊人在 | 在线观看久草 | 成人av在线大片 | 天堂综合网久久 | 在线观看视频福利 | 亚洲欧美日韩一区二区 | 精品粉嫩aⅴ一区二区三区四区 | a毛片| 国产一区二区小视频 | 超碰97在线免费 | 综合色久 | 亚洲一级黄色 | 琪琪午夜伦伦电影福利片 | 国产精品污www在线观看 | 91综合在线视频 | 久久久久久久久久久久亚洲 | 一区二区三区四区在线视频 | 一级毛片成人免费看a | 欧美一级二级三级视频 | 日韩一区二区三区精品 | 中文字幕在线观 | 欧美性网 | aaa天堂 | 亚洲免费视频网址 | 在线成人一区 | 综合五月 | 天堂综合 | 有码在线| 在线播放中文字幕 | 秋霞国产 | 伊人精品在线 | 国产乱码精品1区2区3区 | 天天影视网天天综合色在线播放 | 曰批视频在线观看 | 九九精品视频在线 | 日韩不卡视频在线观看 | 国产成人精品一区二区三区网站观看 | 欧美精品二区三区 |