如何使这个运行异步? [英] How do I make this run asynchronously?

查看:178
本文介绍了如何使这个运行异步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写在C#中,并在其中一个WPF应用程序我需要查询历史TFS,而我显示的变更清单,我回去在ListView。

I am writing a WPF application in C# and in it I need to query history in TFS, and I'm displaying the list of changesets I get back in a listview.

ListView控件的ItemsSource绑定到称为变更一个IEnumerable属性,未加载,直到使用的属性:

The listview ItemsSource is bound to an IEnumerable property called Changesets that is not loaded until the property is used:

public IEnumerable<Changeset> Changesets
{
  get
  {
    if (p_nChangesets == null)
    {
      p_nChangesets = TfsHelper.VCS.QueryHistory(Path, VersionSpec.Latest, 0,
                                                 RecursionType.Full, null,
                                                 new ChangesetVersionSpec(1),
                                                 VersionSpec.Latest, int.MaxValue, 
                                                 false, true, false, false)
                                                 .OfType<Changeset>();
    }
    return p_nChangesets;
  }
}

现在当视图加载时,列表视图绑定到此属性,因此它立即调用属性来获取变更集的收集发生的事情是。有时,这种查询缓慢运行,因此还需要一段时间,甚至看到窗口打开。我希望发生是立即窗口显示,直到该变更集被发现,然后列表视图被填充ListView控件是空的。但我不知道如何做到这一点。我试图用一个任务,但有相同的结果:

Now what happens is when the view loads, the listview is bound to this property so it immediately calls the property to get the collection of changesets. Sometimes this query runs slow so it takes a while to even see the window open. What I want to happen is the window displays immediately and the listview is empty until the changesets are found and then the listview is filled. But I don't know how to do this. I tried using a Task, but that had the same result:

public IEnumerable<Changeset> Changesets
{
  get
  {
    if (p_nChangesets == null)
    {
      Task<IEnumerable> task = Task<IEnumerable>.Run(() => TfsHelper.VCS.QueryHistory(
                                                           Path, VersionSpec.Latest, 0, 
                                                           RecursionType.Full, null,
                                                           new ChangesetVersionSpec(1),
                                                           VersionSpec.Latest, int.MaxValue, 
                                                           false, true, false, false));
      p_nChangesets = task.Result.OfType<Changeset>();
    }
    return p_nChangesets;
  }
}

显然,我不知道我有任务在做。有谁知道如何使这个做我想要什么?

Clearly, I don't know what I'm doing with tasks. Does anyone know how to make this do what I want?

推荐答案

添加 IsAsync = TRUE 在XAML中绑定:

Add IsAsync=True in your binding in XAML:

<ListView ItemsSource="{Binding ChangeSets, IsAsync=True}"/>

这篇关于如何使这个运行异步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆