SilverLight:一步一步学Silverlight 2系列(1):数据与通信之WebClient
来源:互联网 发布:岁月号事件真相知乎 编辑:程序博客网 时间:2024/04/29 04:51
编写界面布局,XAML如下:
<Grid Background="#46461F" x:Name="LayoutRoot" Loaded="LayoutRoot_Loaded">
<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>
<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>
在web项目中添加一个。ashx文件
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;
}
}
}
...{
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;
}
}
}
在page.xmal.cs写道
public partial class Page : UserControl
...{
public Page()
...{
InitializeComponent();
}
private void LayoutRoot_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;
}
private 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;
}
}
}
...{
public Page()
...{
InitializeComponent();
}
private void LayoutRoot_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;
}
private 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;
}
}
}
最终实现为
- SilverLight:一步一步学Silverlight 2系列(1):数据与通信之WebClient
- 一步一步学Silverlight 2系列(12):数据与通信之WebClient
- 一步一步学Silverlight 2系列(12):数据与通信之WebClient
- Silverlight 2系列(12):数据与通信之WebClient
- 一步一步学Silverlight 2系列(13):数据与通信之WebRequest
- 一步一步学Silverlight 2系列(14):数据与通信之WCF
- 一步一步学Silverlight 2系列(15):数据与通信之ASMX
- 一步一步学Silverlight 2系列(16):数据与通信之JSON
- 一步一步学Silverlight 2系列(17):数据与通信之ADO.NET Data Services
- 一步一步学Silverlight 2系列(13):数据与通信之WebRequest
- 一步一步学Silverlight 2系列(14):数据与通信之WCF
- 一步一步学Silverlight 2系列(15):数据与通信之ASMX
- 一步一步学Silverlight 2系列(16):数据与通信之JSON
- 一步一步学Silverlight 2系列(17):数据与通信之ADO.NET Data Services
- (12):Silverlight 2 数据与通信之WebClient
- SilverLight中数据与通信之WebClient
- 一步一步学Silverlight 2系列(23):Silverlight与HTML混合之无窗口模式
- 一步一步学Silverlight 2系列(23):Silverlight与HTML混合之无窗口模式
- WebService中使用自定义类的解决方法(5种)
- Morphx基本数据类型2 + 报表开发
- BASIS--笨办法解决ALV_GRID的列剪切丢失纪录的错误
- SQL Anywhere 11 (Panorama) 评审指南 Part II
- 链表翻转程序
- SilverLight:一步一步学Silverlight 2系列(1):数据与通信之WebClient
- Javascript中showModalDialog以及open用法简介
- 打表法解 1012 -- Joseph
- SQL Anywhere 11 (Panorama) 评审指南 Part III
- 设置下拉菜单的长度
- 简单的java计算器
- SQL Anywhere 11 (Panorama) 评审指南 Part IV
- web2.0到底给我们给我们带来了什么?
- SQL Anywhere 11 (Panorama) 评审指南 Part V