【问题标题】:Async issue - not updating UI异步问题 - 不更新 UI
【发布时间】:2013-09-02 21:14:25
【问题描述】:

我在我的 Windows 8 开发中使用异步方法。但它在完成完整方法执行之前不会更新 UI。

这是我的代码。

当用户按下屏幕上的刷新按钮时,下面的方法调用。

public async override void ExceuteRefresh()
    {
        IsBusy = true;             //This is bounded to my UI to show progress bar, but its not updating to UI
        foreach (var category in CategoryObservableColl)
                {
                    CheckforOnlineUpdatesAsync(category, true);
                }          
   }

private async void CheckforOnlineUpdatesAsync(Category category, bool isReFresh)
    {            
           var feed = await _dataService.GetOnlineRssFeedAsync(category.RssFeedLink.URL);
           var newsCategory = _dataService.GetCategoryFromFeed(feed, true, category.RssFeedLink);
            if (newsCategory != null)
           {
                 isRefreshCount +=1;
           }                                    
           if(isRefreshCount ==9)
       {
         IsBusy = false;
       }                                      

     }

IsBusy 已实现 InotifyPropertyChanged。

任何帮助将不胜感激。尝试以下方法。

public override async void ExceuteRefresh()
    {
        await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => IsBusy = true);
        _reloadCategories = false;
        _refreshLinkCount = 0;

        try
        {
            foreach (var category in CategoryObservableColl)
            {
                _refreshLinkCount += 1;
                await CheckforOnlineUpdatesAsync(category, true);
            }
        }
        catch (Exception ex)
        {
            MainPage.ShowMessage("Something Went wrong!. " + ex.Message);
        }


    }

还将以下方法返回类型作为任务。 私有异步任务 CheckforOnlineUpdatesAsync(Category category, bool isReFresh) { }

提前致谢

【问题讨论】:

    标签: c#-4.0 windows-8


    【解决方案1】:

    试试这个

    1. async void CheckforOnlineUpdatesAsync(...)更改为async Task CheckforOnlineUpdatesAsync(...)

    2. 将方法调用更改为await CheckforOnlineUpdatesAsync(category, true);

    3. public async override void ExceuteRefresh() 更改为public async override Task ExceuteRefresh()

    4. IsBusy = false; 替换为以下给定代码。您需要使用 UI 线程来更新 UI。

    await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => IsBusy = false);

    【讨论】:

    • public override async void ExceuteRefresh() { await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => IsBusy = true ); foreach(CategoryObservableColl 中的 var 类别){ _refreshLinkCount += 1;等待 CheckforOnlineUpdatesAsync(类别,真); } }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-16
    • 2015-07-26
    • 1970-01-01
    相关资源
    最近更新 更多