首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >BackgroundWorker和CollectionViewSource

BackgroundWorker和CollectionViewSource
EN

Stack Overflow用户
提问于 2012-01-25 15:01:15
回答 1查看 976关注 0票数 1

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

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

代码语言:javascript
运行
复制
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();
    }
}

谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-01-25 16:26:55

您必须使用WPF的Dispatcher对象(Dispatcher.Invoke)来执行UI元素上的代码(当使用BackgroundWorker完成时)。也许this article可以给你更多的信息。

或者working with the WPF Dispatcher

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8998936

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档