vs2010 学习Silverlight学习笔记(11):数据与通信之WebClient

概要:

    基础知识终于学完了,我今天又从第一篇看到第十篇,发现明白了一些东西,还有忘记了部分东西。呵呵,咱不能猴子掰玉米,学了新的忘记旧的。
要经常去复习,去用。这一篇是数据通信部分的第一篇,有些东西没接触过,不要紧,万事开头难。前面也是这么走过来的么。。。

WebClient:

  webClient顾名思义,是用来上传或下载数据的服务。
  webClient提供的上传数据常用的有4种:
    OpenWrite   返回一个用于将数据发送到资源的  Stream。     
     UploadData   将字节数组发送到资源并返回包含任何响应的字节数组。     
      UploadFile   将本地文件发送到资源并返回包含任何响应的字节数组。     
      UploadValues   将   NameValueCollection   发送到资源并返回包含任何响应的字节数组。
  下载的有三种:
    DownloadData   从资源下载数据并返回字节数组。     
      DownloadFile   从资源将数据下载到本地文件。     
     OpenRead   从资源以   Stream   的形式返回数据。
  上面是从网上查来的,不一定对,但我认为已经可以解释清楚WebClient是干什么的了。
  这个例子是用client.DownloadStringAsync(地址);来下载数据的。

接口部分:

  using System.Web.Services;提供了
    [WebService(Namespace = "http://tempuri.org/")]//提供特性部分
       [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]//Web服务互操作性规范
  一般这些都不要管的,其实我也不太明白这些具体是跟什么的。
  此接口继承于IHttpHandler接口,我们要做的就是在声明接口后返回什么样的值。

代码:

  MainPage.xaml代码:
代码
    
      
< UserControl x:Class = " SilverlightAppDemo12.MainPage "
xmlns
= " http://schemas.microsoft.com/winfx/2006/xaml/presentation "
xmlns:x
= " http://schemas.microsoft.com/winfx/2006/xaml "
xmlns:d
= " http://schemas.microsoft.com/expression/blend/2008 "
xmlns:mc
= " http://schemas.openxmlformats.org/markup-compatibility/2006 "
mc:Ignorable
= " d "
d:DesignHeight
= " 300 " d:DesignWidth = " 400 " Loaded = " UserControl_Loaded " >

< 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 >
</ UserControl >

 

BookHandler.ashx.cs代码,此文件创立在web的文件夹中:
代码
   
     
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 ;
}
}

 

MainPage.xaml.cs代码:
代码
    
      
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)
{
// 端口49955是设定了的提供此服务的端口
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属性web里
vs2010 学习Silverlight学习笔记(11):数据与通信之WebClient
运行:
vs2010 学习Silverlight学习笔记(11):数据与通信之WebClient

总结:

  这个数据传输的第一篇看似简单,其实用到了很多的知识,查询了大量资料后才明白他们有什么用。尽管只是将李老师的代码
照抄过来,但我相信只要一步步,一点点的学下去,收获是很大的。
总目录
上一篇:vs2010 学习Silverlight学习笔记(10):数据绑定
下一篇:vs2010 学习Silverlight学习笔记(12):数据与通信之WebRequest

你可能感兴趣的:(silverlight)