簡體   English   中英

如何隱藏wp7中的異步回調按鈕?

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

所需的場景:當我點擊按鈕時,我希望它被隱藏,直到異步調用完成。

我有像這樣的xaml按鈕

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

和點擊事件

 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;
    }

這僅在我注釋掉異步調用時才有效

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

但這不是我要評論的功能:)我知道我缺少一些delagete或調度員。 我剛開始用SL編碼。

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

發生了什么事就是這條線

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

不阻塞調度線程,因此立即調用LoadMore = true get。 最簡單的方法可能是獲取數據后調用的委托。

因此,您可以將GetData方法更改為:

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

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

完成后,只需調用方法:

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

您需要使用Dispatcher的原因是回調正在后台線程中執行,並且由於LoadMore屬性正在影響Gui Elements,因此需要在UI線程上完成。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM