开发者

How to block input while WCF Asynchronous call runs?

开发者 https://www.devze.com 2023-04-12 04:32 出处:网络
I\'m working on an application on Windows Phone, and I am using silverlight. I have some bugs where the user can quickly press a button twice, which will effectively do 2 WCF calls since the action is

I'm working on an application on Windows Phone, and I am using silverlight. I have some bugs where the user can quickly press a button twice, which will effectively do 2 WCF calls since the action is called 2 times.

The obvious solution is simply to disable the button until the call completes but开发者_如何转开发 I'm wondering if there's a more global solution where I wouldn't have to implement this for every action. My application uses about 50 WCF methods so it would be tedious to implement this for every single action/every screens.

There's also the situation where users can click the phone back button while a call is running and start clicking on other buttons etc...

Anyone know a clean solution for this?


Simple solution: use a boolean variable that will be set to true after first click and to false when result from server has arrived back. In click handler just check the value of this variable, if it is true do not call the service again.


Are you using a design pattern like MVVM in your application? If not, you probably should. In this case you could create a bool property called IsIdle or something like it. Then just set <Button IsEnabled="{Binding IsIdle}". Now whenever you start doing an asynchronous call, set IsIdle to false. This will disable the buttons. When loading is finished set it back to true, so all the buttons are enabled again.


This might not be very elegant, but I think it would prevent any input without creeping all over your views and code.

You could prevent input by temporarily overlaying a transparent rectangle over your screen with IsHitTestable = true. You could write a small utility method to push that rectangle on any of your screens and remove it when you want to restore input.

You could also use that screen to eventually show something like a busy indicator when the network is slow.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号