SilverLight中数据与通信之WebClient

SilverLight中数据与通信之WebClient

 

1.       新建一个处理类BookHandler.ashx

注意:IhttpHandler要求必须实现接口ProcessRequest(HttpContext context)bool IsReusable

 

  public class BookHandler : IHttpHandler

    {

        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;

            }

        }

 

        public static readonly string[] PriceList = new string[] {

            "66.00",

            "78.30",

            "56.50",

            "28.80",

            "77.00"

            };

}

 

 

2.       调用

前台代码:

<Grid x:Name="LayoutRoot" 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>

后台代码:

    public partial class MainPage : UserControl

    {

        public MainPage()

        {

            InitializeComponent();

            Loaded += new RoutedEventHandler(MainPage_Loaded);

        }

 

        void MainPage_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:4699/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;

            }

        }

    }

 

    public class Book

    {

        public Book(string name)

        {

            this.Name = name;

        }

        public string Name

        {

            get;

            set;

        }

 }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值