简体   繁体   English

如何隐藏wp7中的异步回调按钮?

[英]How to hide button on async callback in wp7?

The desired scenario: When I click on the button, I want it to be hidden until async call is completed. 所需的场景:当我点击按钮时,我希望它被隐藏,直到异步调用完成。

I have a button in xaml like this 我有像这样的xaml按钮

    <Button Name="btnLoadNextTransactions" Content="Button" Click="btnLoadNextTransactions_Click"  Visibility="{Binding LoadMore, Converter={StaticResource converter}}" />

and a click event to 和点击事件

 private void btnLoadNextTransactions_Click(object sender, RoutedEventArgs e)
    {
        App.ViewModel.LoadMore = false;
        ApplicationBl<Transaction>.GetDataLoadingCompleted += GetDataLoadingCompleted;
        ApplicationBl<Transaction>.GetData(++offset*10, 10);//works only if I comment out this line
        App.ViewModel.LoadMore = true;
    }

This only works if I comment out async call 这仅在我注释掉异步调用时才有效

//ApplicationBl<Transaction>.GetData(++offset*10, 10);

But that's not a feature I want to comment out :) I know I'm missing some delagete or dispatcher. 但这不是我要评论的功能:)我知道我缺少一些delagete或调度员。 I just started coding with SL. 我刚开始用SL编码。

您需要在GetDataLoadingCompleted方法中放置LoadMore = true。

What's happening is that the line 发生了什么事就是这条线

ApplicationBl<Transaction>.GetData(++offset*10, 10);

Isn't blocking the dispatch thread, so the LoadMore=true get's called right away. 不阻塞调度线程,因此立即调用LoadMore = true get。 The easiest way to do it would probably be with a delegate that you call after getting the data. 最简单的方法可能是获取数据后调用的委托。

So you would change your GetData method to look like this: 因此,您可以将GetData方法更改为:

public void GetData( int offset, int pageSize, Action callback)
{
  //Existing code.

  //Notify the callback that we are done.
  callback();
}

Once that done just call the method like so: 完成后,只需调用方法:

ApplicationBl<Transaction>.GetData(++offset*10, 10, () =>
{
  Deployment.Current.Dispatcher.BeginInvoke(() => App.ViewModel.LoadMore = true;);
});

The reason why you'll need to use the Dispatcher is that the callback is being executed in a background thread, and since the LoadMore property is effecting Gui Elements it needs to be done on the UI thread. 您需要使用Dispatcher的原因是回调正在后台线程中执行,并且由于LoadMore属性正在影响Gui Elements,因此需要在UI线程上完成。

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

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