Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# BackgroundWorker和CollectionViewSource_C#_Wpf_Multithreading_Backgroundworker_Collectionviewsource - Fatal编程技术网

C# BackgroundWorker和CollectionViewSource

C# BackgroundWorker和CollectionViewSource,c#,wpf,multithreading,backgroundworker,collectionviewsource,C#,Wpf,Multithreading,Backgroundworker,Collectionviewsource,使用CollectionViewSource时,如何在backgroundworker中加载数据?它抛出一个错误,因为我的CollectionViewSource位于UI线程中 这是我的全部代码(我在Xaml中设置的后台工作程序): 公共部分类部门查看:窗口 { 私人后台人员后台人员; 私人观察收集所有部门数据; 私有列表集合查看所有部门视图; 私有void backgroundWorker_DoWork(对象发送方,DoWorkEventArgs e) { IEnumerable数据=null

使用CollectionViewSource时,如何在backgroundworker中加载数据?它抛出一个错误,因为我的CollectionViewSource位于UI线程中

这是我的全部代码(我在Xaml中设置的后台工作程序):

公共部分类部门查看:窗口
{
私人后台人员后台人员;
私人观察收集所有部门数据;
私有列表集合查看所有部门视图;
私有void backgroundWorker_DoWork(对象发送方,DoWorkEventArgs e)
{
IEnumerable数据=null;
使用(ArchiveEntities db=new ArchiveEntities())
{
数据=db.部门;
this.AllDepartmentsData=新的可观察收集(数据);
}
CollectionViewSource部门资源=(CollectionViewSource)this.FindResource(“AllDepartmentsDataSource”);
departmentsSource.Source=this.AllDepartmentsData;
this.AllDepartmentsView=(ListCollectionView)departmentsSource.View;
}
公共部门lookup()
{
初始化组件();
backgroundWorker=((backgroundWorker)this.FindResource(“backgroundWorker”));
}
已加载私有无效窗口(对象发送器、路由目标)
{
backgroundWorker.RunWorkerAsync();
}
}

thanx.

您必须使用WPF的Dispatcher对象(Dispatcher.Invoke)在UI元素上执行代码(从BackgroundWorker完成时)。也许能给你更多的信息


或者。

我没有看到一个幕后工作者。但是,尽管如此,在设置collectionviewWhat exception以及在何处引发异常时,您必须使用dispatcher?
public partial class DepartmentsLookup : Window
{
    private BackgroundWorker backgroundWorker;
    private ObservableCollection<Department> AllDepartmentsData;
    private ListCollectionView AllDepartmentsView;

    private void backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        IEnumerable<Department> data = null;
        using (ArchiveEntities db = new ArchiveEntities())
        {
            data = db.Departments;
            this.AllDepartmentsData = new ObservableCollection<Department>(data);
        }
        CollectionViewSource departmentsSource = (CollectionViewSource)this.FindResource("AllDepartmentsDataSource");
        departmentsSource.Source = this.AllDepartmentsData;
        this.AllDepartmentsView = (ListCollectionView)departmentsSource.View;
    }

    public DepartmentsLookup()
    {
        InitializeComponent();
        backgroundWorker = ((BackgroundWorker)this.FindResource("backgroundWorker"));
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        backgroundWorker.RunWorkerAsync();
    }
}