简体   繁体   English

如何在WP7中使用带有异步调用的Dispatcher?

[英]How to use Dispatcher with async call in WP7?

I haven't been able to find an example like this, though I'm sure there must be a few out there. 我无法找到这样的例子,但我确信那里肯定会有一些例子。

When the user clicks a button to log in, an event handler on the button click calls a function that logs the user in. Based on the user, they can be taken to one of many start screens. 当用户单击按钮进行登录时,按钮单击上的事件处理程序会调用记录用户的功能。根据用户,可以将它们带到多个启动屏幕之一。 The information for which screen is returned from a service call. 从服务调用返回屏幕的信息。 From what I can tell, Dispatcher.BeginInvoke is only used to update the UI thread, so the logic that determines which page to navigate to should be in the method passed to Dispatcher.BeginInvoke, right? 据我所知,Dispatcher.BeginInvoke仅用于更新UI线程,因此确定要导航到哪个页面的逻辑应该在传递给Dispatcher.BeginInvoke的方法中,对吗?

I need to make a service call and take action based on the result. 我需要进行服务调用并根据结果采取行动。 Do I have to make the async service call first and call Dispatcher from the callback? 我是否必须首先进行异步服务调用并从回调中调用Dispatcher? Do I put the function that does the validation, calls the service, and handles the callback as the delegate that is passed to the Dispatcher? 我是否将执行验证的函数调用,调用服务,并将回调作为传递给Dispatcher的委托来处理?

Sorry if this is a basic question. 对不起,如果这是一个基本问题。 The examples I've found only use the Dispatcher to update a textbox or some other trivial item. 我发现的示例只使用Dispatcher来更新文本框或其他一些简单的项目。 I haven't found anything where the UI thread needs to take action based on the result of an async call. 我没有找到UI线程需要根据异步调用的结果采取行动的任何内容。

It's not clear what validation you're talking about but: 目前尚不清楚你在说什么验证,但是:

  • Call the service asynchronously, with a callback to execute when the service call finishes 异步调用服务,并在服务调用完成时执行回调
  • In the callback, do whatever non-UI related work is involved, and then call Dispatcher.BeginInvoke to perform any UI-related operations. 在回调中,执行涉及的非UI相关工作,然后调用Dispatcher.BeginInvoke以执行任何与UI相关的操作。

If you need to do validation before the service call, that could be part of your button's event handler... at least as long as it isn't a long-running piece of validation. 如果你需要在服务调用之前进行验证,那么这可能是你的按钮的事件处理程序的一部分...至少只要它不是一个长期运行的验证。

If you could give more details about what steps are logically involved in your process, that would help. 如果您可以提供有关在流程中逻辑上涉及哪些步骤的更多详细信息,那将有所帮助。

See, Dispatcher actually holds the UI thread. 请参阅,Dispatcher实际上拥有UI线程。 As every control has strong thread affinity, you need to update this using Dispatcher. 由于每个控件都具有强大的线程关联性,因此您需要使用Dispatcher更新它。

From any thread you can access Dispatcher using DispatcherObject.Dispatcher. 从任何线程,您都可以使用DispatcherObject.Dispatcher访问Dispatcher。

this.Dispatcher.Invoke((Action)(()=>{
 .//// your action
});

Now whenever you need to update the elements in the thread, you can wrap around the context to call service anytime. 现在,无论何时需要更新线程中的元素,您都可以随时环绕上下文来调用服务。 You can use the Background thread or you can call from inside of Dispatcher thread to invoke the Service call. 您可以使用Background线程,也可以从Dispatcher线程内部调用以调用Service调用。

Calling From dispatcher will hold the UI if the call is synchronous. 如果呼叫是同步的,则调用来自调度程序将保留UI。 So I recommend you to do the call from the Background thread just below updating the Invoker. 所以我建议你在更新Invoker下面的后台线程中进行调用。

All service call are asynchronous in silverlight (and hence windows phone 7) so yes, what you describe is way you do it. 所有服务调用在silverlight中都是异步的(因此也就是windows phone 7)所以是的,你所描述的就是你这样做的方式。

Different service libraries provide different ways to call their methods - some offer a 'call complete' method, others take a event hander passed in, but either way if you want to update the UI (and I assume this includes moving page) you will need to do this on the UI thread, which is what the dispatcher is for. 不同的服务库提供了不同的方法来调用他们的方法 - 一些提供了'call complete'方法,另一些提供了一个事件处理器传入,但是如果你想要更新UI(我认为这包括移动页面),你将需要在UI线程上执行此操作,这是调度程序的用途。

Dispatcher.BeginInvoke( () => {
   // This code is on the UI thread.
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM