I'm looking to use an asynchronous web request in silverlight and would like to look at an example 开发者_如何转开发which isn't as confusing as the documentation on msdn.
The secret of asynchronous method calls is that you provide a callback method (defined inline as lambda expression below) and then call the Async method which will immediately return. When the asynchronous operation finished, the callback method will be called.
var wc = new WebClient();
wc.DownloadStringCompleted += (sender, e) =>
{
using (sender as IDisposable)
{
myTextBox.Text = e.Result;
}
};
wc.DownloadStringAsync(new Uri("http://stackoverflow.com"));
var wc = new WebClient();
wc.DownloadStringCompleted += wc_DownloadStringCompleted;
wc.DownloadStringAsync(new Uri("http://stackoverflow.com"));
with
void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
using (sender as IDisposable)
{
myTextBox.Text = e.Result;
}
}
精彩评论