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;
}
}
总结以上是内存溢出为你收集整理的SilverLight中数据与通信之WebClient全部内容,希望文章能够帮你解决SilverLight中数据与通信之WebClient所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)