一区二区久久-一区二区三区www-一区二区三区久久-一区二区三区久久精品-麻豆国产一区二区在线观看-麻豆国产视频

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

概述

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ā)。

本文將介紹如何在Silverlight 2中使用Web Client進(jìn)行通信。

簡(jiǎn)單示例

編寫(xiě)一個(gè)簡(jiǎn)單的示例,在該示例中,選擇一本書(shū)籍之后,我們通過(guò)Web Client去查詢書(shū)籍的價(jià)格,并顯示出來(lái),最終的效果如下:

TerryLee_Silverlight2_0059

編寫(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>

為了模擬查詢價(jià)格,我們編寫(xiě)一個(gè)HttpHandler,接收書(shū)籍的No,并返回價(jià)格:

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

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

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)當(dāng)用戶選擇一本書(shū)籍時(shí),需要通過(guò)Web Client去獲取書(shū)籍的價(jià)格,在Silverlight 2中,所有的網(wǎng)絡(luò)通信API都設(shè)計(jì)為了異步模式。在聲明一個(gè)Web Client實(shí)例后,我們需要為它注冊(cè)DownloadStringCompleted事件處理方法,在下載完成后將會(huì)被回調(diào),然后再調(diào)用DownloadStringAsync方法開(kāi)始下載。

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 = "價(jià)格:" + e.Result;    }    else    {        lblPrice.Text = e.Error.Message;    }}

 

 

注意大家可以在Web Application Project的屬性頁(yè)中,把ASP.NET Development Server的端口號(hào)設(shè)置為一個(gè)固定的端口號(hào):

TerryLee_Silverlight2_0060

最后完整的代碼如下:

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 = "價(jià)格:" + e.Result;        }        else        {            lblPrice.Text = e.Error.Message;        }    }}

運(yùn)行后效果如下:

TerryLee_Silverlight2_0059

當(dāng)我們選擇其中一本書(shū)籍時(shí),將會(huì)顯示出它的價(jià)格:

TerryLee_Silverlight2_0061

結(jié)束語(yǔ)

本文簡(jiǎn)單介紹了Silverlight 2中使用Web Client進(jìn)行通信的知識(shí),在Silverlight 2中,提供的通信API非常豐富,后面將會(huì)介紹其他的方式。你可以從這里下載本文示例代碼。

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

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

主站蜘蛛池模板: 欧美在线一二三区 | 免费国产一区二区在免费观看 | 国产小视频91 | 黄色小视频在线观看 | 精品无人区乱码麻豆1区2区 | 免费精品美女久久久久久久久久 | 国产福利专区精品视频 | 免费黄色在线 | 四虎久久精品国产 | 七七国产福利在线二区 | 亚洲精品第一国产综合高清 | 国产成人免费高清视频网址 | 国产激情小说 | 狠狠综合久久久久综合小说网 | 国产成人理在线观看视频 | 国产成人精品一区二区秒拍 | 久久噜噜久久久精品66 | 成人午夜视频在线观 | 最近更新中文字幕第一页 | 六月婷婷精品视频在线观看 | 亚洲美女视频一区 | 久久国内精品自在自线观看 | 激情五月激情综合 | 四虎在线视频免费观看 | 国产情侣自拍小视频 | 国产全部理论片线观看 | 精品国产一区二区三区免费 | 免费国产一区 | 2020最新国产精品视频 | 国产精品九九视频 | 99精品99| 五月婷在线 | 欧洲精品一区二区三区在线观看 | www.五月| 亚洲国产中文字幕在线观看 | 91视频麻豆视频 | 1区2区3区4区产品乱码入口 | 91精品久久久久久久99蜜桃 | 色综合五月 | 久久精品国产国语对白 | 91视频国产精品 |