[英]Is there a way to pass in callbacks when calling Async methods in WP8/Silverlight
I've been writing Windows Phone 8 code that calls a SOAP web service backend. 我一直在编写Windows Phone 8代码,该代码调用SOAP Web服务后端。 From what I've read, the typical pattern is this:
根据我的阅读,典型的模式是这样的:
var client = new KitchenPCSoapClient();
client.LogonCompleted += client_LogonCompleted;
client.LogonAsync(username, password);
To me, this just seems counter intuitive. 对我来说,这似乎与直觉相反。 If I call
LogonAsync
multiple times with the same client, I don't necessarily want it to use the same LogonCompleted
callback every time. 如果我使用同一客户端多次调用
LogonAsync
,则不必每次都使用相同的LogonCompleted
回调。
What I'd like is a pattern more like JavaScript, where you pass in a reference to the callback function. 我想要的是一种更像JavaScript的模式,您可以在其中传递对回调函数的引用。 More like:
更像:
var client = new KitchenPCSoapClient();
client.LogonAsync(username, password, client_LogonCompleted);
Is there a way to implement such a pattern, or should I just force myself to get used to setting the LogonCompleted
property before I call LogonAsync
, or set the userState
property if I want to differentiate between different contexts? 有没有一种方法可以实现这种模式,还是我应该强迫自己
LogonCompleted
在调用LogonAsync
之前习惯设置LogonCompleted
属性,或者如果我想在不同的上下文之间进行区分,请设置userState
属性?
You can make use of Dispatcher and call the function on UI side for this ... 您可以使用Dispatcher并为此在UI端调用函数。
I am doing like this 我就是这样
callback function 回调函数
private void AddPricesHandler(CompletedEventArgs response, Exception e)
{
//your code gose here
}
Call proxy calss function 呼叫代理通话功能
Proxy.AddData(AddPricesHandler, request);
proxy class calling webservice 代理类调用Web服务
public void AddData(Action<CompletedEventArgs, Exception> callback, IPVWorkflowService.CreateEditDeletePriceSourceRequest request)
{
_workflowProxy.CreateEditDeletePriceSourceAsync(request, callback);
_workflowProxy.CreateEditDeletePriceSourceCompleted+=new EventHandler<CreateEditDeletePriceSourceCompletedEventArgs>(_workflowProxy_CreateEditDeletePriceSourceCompleted);
}
completer function use dispatcher to call callback function completer函数使用调度程序来调用回调函数
void _workflowProxy_CreateEditDeletePriceSourceCompleted(object sender, CreateEditDeletePriceSourceCompletedEventArgs e)
{
try
{
this.dispatcher.BeginInvoke((Action)(() =>
{
(e.UserState as Action<CompletedEventArgs, Exception>)(e, null);
}));
}
catch (Exception ex)
{
this.dispatcher.BeginInvoke((Action)(() =>
{
(e.UserState as Action<CompletedEventArgs, Exception>)(e, ex);
}));
}
finally
{
_workflowProxy.CreateEditDeletePriceSourceCompleted -= _workflowProxy_CreateEditDeletePriceSourceCompleted;
_workflowProxy = null;
}
}
The beauty of Async/Await is that you don't have to write callbacks, you just write code that looks like synchronous, but in the background it's executed asynchronously: Async / Await的优点在于您不必编写回调,而只需编写看起来像同步的代码,但是在后台它是异步执行的:
var client = new KitchenPCSoapClient();
await client.LogonAsync(username, password);
// you get here after the async logon is completed
// do stuff after logon
But if you really want to use callbacks, you can just use the method ContinueWith on the Task object, that is returned from asynchronous method: 但是,如果您确实想使用回调,则可以只使用Task对象上的ContinueWith方法,该方法是从异步方法返回的:
var client = new KitchenPCSoapClient();
Task task = client.LogonAsync(username, password);
// this method is called when async execution of task is finished
task.ContinueWith(client_LogonCompleted);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.