【问题标题】:How to get the result from an event handler from another class?如何从另一个类的事件处理程序中获取结果?
【发布时间】:2014-05-09 16:54:48
【问题描述】:

我必须从网络服务中获取结果。这是我用的:

编辑:

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());
    }

有什么方法可以将所有这些函数甚至处理程序放到一个类中,当我想从我的网络服务中获取数据时,我会这样做:

string json = MyWebService.GetData();

【问题讨论】:

  • 为什么不呢?您可以使用/利用 c# 的扩展方法。
  • 创建一个返回 Json 字符串或者你所期望的,而不是 void 的方法
  • 我创建了一个返回 int 的方法,但不知何故客户端上的 Async 方法是无效的。我真的不知道为什么。这是我第一次使用它。
  • 这已经不是原来的问题了...

标签: c# windows-phone


【解决方案1】:

首先,您对DownloadStringAsync 的调用建议传入用户名和密码,但它不是那样工作的。 Check the documentation.

要(不是真的 :-))回答您的问题:如今更好的方法是使用 C# 5 中提供的新的“async/await”功能。请参阅this link 以获得全面的概述。

然后您的示例变得微不足道(不再需要连接单独的事件处理程序)

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;
}

您仍然可以在一个单独的、可重用(异步)的方法中提取它:

private async Task<string> GetDataAsync()
{
    WebClient webClient = new WebClient();
    // TODO set up credentials
    string result = await webclient.DownloadStringTaskAsync("http://your-url-here"); 
    return result;
}

如果您想坚持使用基于事件的方法,您可以将功能包装在一个单独的类中,并使用它自己的事件,但这不会给您带来太多的 IMO 收益。

【讨论】:

  • 感谢您非常有帮助的回答!实际上,这只是我在 Internet 上找到的一些代码块,看起来有点像我的代码。我认为 DownloadStringAsync 是某人创建的函数。这仍然适用于我自己在 web 服务上的功能吗?一些我的所有 int 方法如何在客户端变为 void,并且 await 不像它所说的那样与 void 一起工作。
  • 不完全确定您在这里问的是什么(“我的 int 方法在客户端变得无效”是什么意思?),但 async/await 是该语言的一个功能,因此它完全适用相同的。我建议您熟悉该功能。试试谷歌“异步等待介绍”。
  • 我的意思是我在 WCF 服务器上的所有返回 int 的方法都成为在客户端返回 void 的方法。例如:我在服务器上有一个 int MyMethod(),但是当我在我的客户端使用它时,它变成了 void service.MyMethodAsync()。谢谢你的建议。
【解决方案2】:

我从这篇文章中弄清楚了:How to use async-await with WCF in VS 2010 for WP7?

更多:Async CTP - How can I use async/await to call a wcf service?

这是我在另一堂课上写的:

 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;
    }

然后我可以从我的班级调用它:

 int su = await WebServiceHelper.SignUp("blabla", "0123465","huehuehue");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多