开发者

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

    <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.LoadM开发者_StackOverflow社区ore = 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. I just started coding with SL.


You need to put LoadMore = true in the GetDataLoadingCompleted method.


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. 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:

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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜