WPF 不支持从调度程序线程以外的线程对其 SourceCollection 进行的更改

2022-10-12,,,,

该问题出现在wpf中的vm类中,observablecollection类型,该类型的 collectionview 不支持调度程序线程以外的线程对其 sourcecollection 进行的更改,解决办法:

threadpool.queueuserworkitem(delegate
            {
                synchronizationcontext.setsynchronizationcontext(new
                    dispatchersynchronizationcontext(system.windows.application.current.dispatcher));
                synchronizationcontext.current.post(pl =>
                {
                    //里面写真正的业务内容
                    _framecontents.add(frame);
                    _datagridmain.scrollintoview(_framecontents[_framecontents.count - 1], _datagridmain.columns[0]);
                }, null);
            });

其中 _framecontents 就是public observablecollection<framecontent> _framecontents 类型。

by:

《WPF 不支持从调度程序线程以外的线程对其 SourceCollection 进行的更改.doc》

下载本文的Word格式文档,以方便收藏与打印。