按元素添加元素到ListView而不阻止UI

时间:2013-10-25 01:42:04

标签: c# wpf multithreading .net-4.0 itemssource

我正在开发一个Wpf应用程序,它使用EF从数据库中检索数据。

我有一些ListView控件,这些控件填充了数据库的一些表,以便在检索数据时防止阻塞UI,如下所示:

        Task tsk = Task.Factory.StartNew(() =>
        {
            ItemsSource = Database.SomeTable();
        });

ItemsSource变量是一个ObservableCollection,它绑定到ListView的ItemsSource属性。

正如预期的那样,在加载数据时,UI保持响应。我的问题是ListView是空的,直到加载所有数据。所以我想在ListView中看到逐个元素。有没有办法做到这一点??我试过了一个没有运气的foreach循环。

提前致谢。

2 个答案:

答案 0 :(得分:3)

这可以通过使用从任务调用的Disptacher的BeginInvoke方法向您的可观察集合添加新元素来实现。 类似的东西:

// MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <ListView ItemsSource="{Binding MyList}" Grid.Row="0" />
        <Button Content="Load" Click="OnLoadClicked" Grid.Row="1" Height="30" />
    </Grid>
</Window>

// MainWindow.xaml.cs

using System;
using System.Collections.ObjectModel;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private VM _vm = new VM();
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = _vm;
        }

        private void OnLoadClicked(object sender, RoutedEventArgs e)
        {
            Load10Rows();            
        }

        private void Load10Rows()
        {
            Task.Factory.StartNew(() =>
                {
                    for (int i = 0; i < 10; i++)
                    {
                        Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                            {
                                _vm.MyList.Add(DateTime.Now.ToString());
                            }), DispatcherPriority.Background);
                        // Just to simulate some work on the background
                        Thread.Sleep(1000);
                    }
                });
        }
    }

    public class VM
    {
        private ObservableCollection<string> _myList;
        public VM()
        {
            _myList = new ObservableCollection<string>();
        }

        public ObservableCollection<string> MyList
        {
            get { return _myList; }
        }
    }
}

如果您有大量记录,可能需要将其分块,否则只需为每条记录调用Disptacher。

答案 1 :(得分:1)

也许使用Task.Delay允许用户界面在添加foreach中的下一个项目之前呈现更改

示例:

private async Task AddItems()
{
    foreach (var item in Database.SomeTable())
    {
        ItemsSource.Add(item);
        await Task.Delay(1);
    }
}
相关问题