天天躁日日躁狠狠躁AV麻豆-天天躁人人躁人人躁狂躁-天天澡夜夜澡人人澡-天天影视香色欲综合网-国产成人女人在线视频观看-国产成人女人视频在线观看

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

概述

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

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

簡單示例

編寫一個簡單的示例,在該示例中,選擇一本書籍之后,我們通過Web Client去查詢書籍的價格,并顯示出來,最終的效果如下:

TerryLee_Silverlight2_0059

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

為了模擬查詢價格,我們編寫一個HttpHandler,接收書籍的No,并返回價格:

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

在界面加載時綁定書籍列表,關(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;}

接下來當用戶選擇一本書籍時,需要通過Web Client去獲取書籍的價格,在Silverlight 2中,所有的網(wǎng)絡(luò)通信API都設(shè)計為了異步模式。在聲明一個Web Client實例后,我們需要為它注冊DownloadStringCompleted事件處理方法,在下載完成后將會被回調(diào),然后再調(diào)用DownloadStringAsync方法開始下載。

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的端口號設(shè)置為一個固定的端口號:

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

運行后效果如下:

TerryLee_Silverlight2_0059

當我們選擇其中一本書籍時,將會顯示出它的價格:

TerryLee_Silverlight2_0061

結(jié)束語

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

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

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

主站蜘蛛池模板: 久久精品一卡二卡三卡四卡视频版 | 欧美日韩另类在线专区 | 小SB几天没做SAO死了H | 亚洲 日韩 色 图网站 | 精品无码一区二区三区不卡 | 成人女人A级毛片免费软件 成人免费在线视频 | 美女岔开腿露出粉嫩花苞 | 国产精品夜夜春夜夜爽久久小 | 麻豆久久婷婷五月国产 | 少妇无码吹潮久久精品AV | 日韩精品一卡二卡三卡四卡2021 | 国产精品无码亚洲网 | 成年美女黄网站色app | 我们日本在线观看免费动漫下载 | a级成人免费毛片完整版 | 久久久久琪琪精品色 | 怡春院院日本一区二区久久 | 亚洲呦女专区 | 欧美精品XXXXBBBB | 99视频在线免费看 | 亚洲欧洲日韩国产一区二区三区 | 中国女人内谢69XXXXXA片 | 花蝴蝶高清影视视频在线播放 | 精品无码久久久久久国产百度 | 99久久亚洲精品日本无码 | 99视频久九热精品 | 国产亚洲精品精华液 | 性西欧俄罗斯极品 | 久久久免费观成人影院 | 激情内射亚洲一区二区三区 | free性中国hd护士高清 | 国产精一品亚洲二区在线播放 | 99久久无码一区人妻A片蜜 | 亚洲国产精品自在自线观看 | 越南女子杂交内射BBWXZ | 久久99精品视频 | 男人的天堂色 | 秋霞电影网伦大理电影在线观看 | 亚洲幼女网 | 挺进老师的紧窄小肉六电影完整版 | 掀开奶罩边躁狠狠躁软学生 |