c# - WinRT - 在保持 UI 响应的同时加载数据

标签 c# .net windows-8 microsoft-metro windows-runtime

我正在开发 Windows Metro 应用程序,但遇到了 UI 变得无响应的问题。据我所知,原因如下:

    <ListView
...
        SelectionChanged="ItemListView_SelectionChanged"            
...

此事件在这里处理:

    async void ItemListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (this.UsingLogicalPageNavigation()) this.InvalidateVisualState();

        MyDataItem dataItem = e.AddedItems[0] as MyDataItem;
        await LoadMyPage(dataItem);
    }

    private async Task LoadMyPage(MyDataItem dataItem)
    {            
        SyndicationClient client = new SyndicationClient();
        SyndicationFeed feed = await client.RetrieveFeedAsync(new Uri(FEED_URI));                    

        string html = ConvertRSSToHtml(feed)
        myWebView.NavigateToString(html, true);            
    }

LoadMyPage 需要一段时间才能完成,因为它从 Web 服务获取数据并将其加载到屏幕上。然而,看起来 UI 正在等待它:我的猜测是直到上述事件完成。

所以我的问题是:我该怎么办?有没有更好的事件我可以 Hook ,或者有另一种方法来处理这个问题?我考虑过启动后台任务,但这对我来说似乎有点过分了。

编辑:

为了阐明这个问题的严重性,我说的是最多 3 - 4 秒的无响应时间。这绝不是一项长期运行的工作。

编辑:

我已经尝试了下面的一些建议,但是,SelectionChanged 函数的整个调用堆栈都在使用 async/await。我已经追踪到这个声明:

myFeed = await client.RetrieveFeedAsync(uri);

在完成之前似乎不会继续处理。

编辑:

我意识到这正在变成 war 与和平,但下面是使用空白 Metro 应用程序和按钮复制的问题:

XAML:

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <StackPanel>
        <Button Click="Button_Click_1" Width="200" Height="200">test</Button>
        <TextBlock x:Name="test"/>
    </StackPanel>
</Grid>

代码隐藏:

    private async void Button_Click_1(object sender, RoutedEventArgs e)
    {
        SyndicationFeed feed = null;

        SyndicationClient client = new SyndicationClient();
        Uri feedUri = new Uri(myUri);

        try
        {
            feed = await client.RetrieveFeedAsync(feedUri);

            foreach (var item in feed.Items)
            {       
                test.Text += item.Summary.Text + Environment.NewLine;                    
            }
        }
        catch
        {
            test.Text += "Connection failed\n";
        }
    }

最佳答案

试一试...

SyndicationFeed feed = null;

SyndicationClient client = new SyndicationClient();

var feedUri = new Uri(myUri);

try {
    var task = client.RetrieveFeedAsync(feedUri).AsTask();

    task.ContinueWith((x) => {
        var result = x.Result;

        Parallel.ForEach(result.Items, item => {
            Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal,
            () =>
            {
                test.Text += item.Title.Text;
            });
       });     
   });
}
catch (Exception ex) { }

我通过使用 Grid 应用程序模板向应用程序添加按钮在我的机器上进行了尝试。我可以在更新页面标题时来回滚动项目网格而不会出现问题。虽然我没有很多项目,但它进行得非常快,所以很难做到 100% 积极。

关于c# - WinRT - 在保持 UI 响应的同时加载数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11878654/

相关文章:

c# - 为什么 DrawString 看起来很糟糕?

c# - 设置一个 C# 方法,在第一次失败后尝试重新连接到不同端口上的 SQL Server

.net - 在 .Net CLI 应用程序中显示完成百分比的最佳方式是什么?

c# - Windows 8 中的 Windows Azure 和 Metro 风格的应用程序

Windows 8 : Doing heap analysis on 32 bit application running through WOW64

c# - 什么时候应该使用 ConcurrentDictionary 和 Dictionary?

c# - 如何在可执行文件中记录序列号?

c# - 使用 asp.net(C#) 更改 div 标签中的 css 类

c# - 关于 PascalCase 的新手问题

c# - 我应该使用哪个版本的 csc.exe?