添加元素之间到ListView控件不阻塞UI [英] Add Element By Element to ListView Without blocking UI

查看:153
本文介绍了添加元素之间到ListView控件不阻塞UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am developing a Wpf application which retrieves the data from a database using EF.

我有一些的ListView控件这是为了填充数据库的一些表,以便于prevent从阻塞UI,同时检索数据我做如下:

I have some ListView controls which are filled with some tables of the database so in order to prevent from blocking the UI while retrieving the data I do as follows:

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

在的ItemsSource变量是被绑定到ListView的ItemsSource属性一个ObservableCollection。

The ItemsSource variable is an ObservableCollection which is bound to the ItemsSource property of a ListView.

的事情是,正如所料,在加载数据的用户界面保持响应。我的问题是,ListView控件是空的,直到所有的数据被加载。所以,我想看到​​的元素之间出现在ListView 。有没有办法做到这一点?我已经试过这foreach循环,没有运气。

The thing is that, as expected, while loading the data the UI keeps responsive. My problem is that the ListView is empty until all the data is loaded. So I would like to see element by element appearing in the ListView. Is there a way to do that?? I have tried which a foreach loop with no luck.

在此先感谢。

推荐答案

这可以通过调度增加了新的元素,你的观察的集合使用Disptacher的BeginInvoke方法,从你的任务称为完成。 是这样的:

This can be accomplished by dispatching the addition of new elements to your observable collection using the Disptacher's BeginInvoke method, called from your task. Something like:

// MainWindow.xaml

//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

//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为每个记录。

If you have a large amount of records you may want to chunk it, otherwise just call the Disptacher for each record.

这篇关于添加元素之间到ListView控件不阻塞UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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