silverlight 的数据通讯(他是异步的模式)
//listBox的SelectionChanged事件
private void book_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
Uri endPoint = new Uri(string.Format("http://localhost:7503/SilverlightApplication5Web/Handler.ashx?No={0}", Books.SelectedIndex));//启动一个handler的路径,得到的方法,只要浏览Handler.ashx就可以得到。
WebClient client = new WebClient();//调用webclient
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);//webclient的一个代理
client.DownloadStringAsync(endPoint);//需要通过Web Client去获取字符串,在Silverlight 2中,所有的网络通信API都设计为了异步模式。在声明一个Web Client实例后,我们需要为它注册DownloadStringCompleted事件处理方法,在下载完成后将会被回调,然后再调用DownloadStringAsync方法开始下载
}
void client_DownloadStringCompleted(object sentder,DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
txtPrice.Text = "price" + e.Result;
}
else
{
txtPrice.Text = e.Error.Message;
}
}
handler 页面的代码
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"])]);
//注意这里如何传过来的NO
}
public bool IsReusable
{
get
{
return false;
}
}
}
浙公网安备 33010602011771号