[英]How to get the result from an event handler from another class?
I have to get the result from a web service. 我必须从Web服务获得结果。 This is what I used:
这是我使用的:
private void Button_Click(object sender, RoutedEventArgs e)
{
webService.WebServiceHuscSoapClient a = new webService.WebServiceHuscSoapClient();
a.InsertNewUserCompleted += a_InsertNewUserCompleted;
a.InsertNewUserAsync("name", "phonenumber", "address");
}
void a_InsertNewUserCompleted(object sender, webService.InsertNewUserCompletedEventArgs e)
{
MessageBox.Show(e.Result.ToString());
}
Is there any way that I could put all this function and even handler to a class and when I want to get data from my webservice I would do something like this: 有什么办法可以将所有这些功能甚至处理程序放到一个类中,当我想从Web服务中获取数据时,我会执行以下操作:
string json = MyWebService.GetData();
First, your call of DownloadStringAsync
suggests passing in username and password, but it doesn't work like that. 首先,您对
DownloadStringAsync
的调用建议您输入用户名和密码,但这样不能正常工作。 Check the documentation . 检查文档 。
To (not really :-) ) answer your question: a far better approach these days is to use the new 'async/await' functionality available in C# 5. See this link for a comprehensive overview. 要(不是真的:-))回答您的问题:如今,更好的方法是使用C#5中提供的新的“异步/等待”功能。有关完整的概述,请参见此链接 。
Your example then becomes trivial (no need to hook up a separate event handler anymore) 然后,您的示例变得无关紧要(不再需要连接单独的事件处理程序)
private async void Button_Click(object sender, RoutedEventArgs e)
{
WebClient webclient = new WebClient();
// TODO set up credentials
string result = await webclient.DownloadStringTaskAsync("http://your-url-here");
textBlock1.Text = str;
}
You could then still extract this in a separate, reusable (async) method: 然后,您仍然可以使用单独的可重用(异步)方法将其提取:
private async Task<string> GetDataAsync()
{
WebClient webClient = new WebClient();
// TODO set up credentials
string result = await webclient.DownloadStringTaskAsync("http://your-url-here");
return result;
}
If you want to stick with the event-based approach, you could wrap the functionality in a separate class with it's own event, but that wouldn't bring you much gain IMO. 如果您想坚持使用基于事件的方法,则可以将功能与其自身的事件包装在单独的类中,但这不会给IMO带来太大的好处。
I figured it out from this article: How to use async-await with WCF in VS 2010 for WP7? 我从这篇文章中弄清楚了: 如何在VS 2010中为WP7使用WCF中的async-await?
More: Async CTP - How can I use async/await to call a wcf service? 更多: 异步CTP-如何使用异步/等待来调用wcf服务?
I wrote this on another class: 我在另一堂课上写道:
public static Task<int> InsertNewUser(string name, string phonenumber,string address) //can make it an extension method if you want.
{
TaskCompletionSource<int> tcs = new TaskCompletionSource<int>();
service.InsertNewUserCompleted += (object sender, WebService.InsertNewUserCompletedEventArgs e) => //change parameter list to fit the event's delegate
{
if (e.Error != null) tcs.SetResult(-1);
else
tcs.SetResult((int)e.Result);
};
service.InsertNewUserAsync(name, phonenumber,address);
return tcs.Task;
}
then I could call it from my class: 然后我可以在班上叫它:
int su = await WebServiceHelper.SignUp("blabla", "0123465","huehuehue");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.