设置两个 ObservableCollection 相等,而不丢失 WPF-Binding

发布于 2024-10-25 01:16:14 字数 1817 浏览 7 评论 0原文

我有一个 MediaFile 的 ObservableCollection

MediaControlClass.GetInstanze().MediaLibrary

我有很多此 ObservableCollection 的 WPF 绑定。 在某些情况下,我必须从 XML 文件中重新填充此集合。 XML 文件中有 20.000 个媒体文件。我尝试了两种方法。

第一种方法:

serializer = new XmlSerializer(typeof(System.Collections.ObjectModel.ObservableCollection<MediaFile>));
                    XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
                    xmlReaderSettings.CheckCharacters = false;

                    using (XmlReader reader = XmlReader.Create(Environment.CurrentDirectory + "\\Notes.xml", xmlReaderSettings))
                    {
                        o = serializer.Deserialize(reader);
                        MediaControlClass.GetInstanze().MediaLibrary.Clear();

                        foreach (var i in (System.Collections.ObjectModel.ObservableCollection<MediaFile>)o)
                            MediaControlClass.GetInstanze().MediaLibrary.Add(i);

                    }

我将每个 MediaFile 添加到 MediaControlClass.GetInstanze().MediaLibrary 中,这确实很慢。

第二种方式:

serializer = new XmlSerializer(typeof(System.Collections.ObjectModel.ObservableCollection<MediaFile>));
                XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
                xmlReaderSettings.CheckCharacters = false;

                using (XmlReader reader = XmlReader.Create(Environment.CurrentDirectory + "\\Notes.xml", xmlReaderSettings))
                {
                    o = serializer.Deserialize(reader);
                    MediaControlClass.GetInstanze().MediaLibrary = (System.Collections.ObjectModel.ObservableCollection<MediaFile>)o;

                }

这很快,但我必须重新绑定很多 WPF 控件。

有什么方法可以快速做到这一点并且无需重新绑定吗?

I have a ObservableCollection of MediaFile

MediaControlClass.GetInstanze().MediaLibrary

I have a lot of WPF Bindings of this ObservableCollection.
In some case I have to refill this collection form a XML File. In the XML File are 20.000 MediaFiles. I tried two ways.

First way:

serializer = new XmlSerializer(typeof(System.Collections.ObjectModel.ObservableCollection<MediaFile>));
                    XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
                    xmlReaderSettings.CheckCharacters = false;

                    using (XmlReader reader = XmlReader.Create(Environment.CurrentDirectory + "\\Notes.xml", xmlReaderSettings))
                    {
                        o = serializer.Deserialize(reader);
                        MediaControlClass.GetInstanze().MediaLibrary.Clear();

                        foreach (var i in (System.Collections.ObjectModel.ObservableCollection<MediaFile>)o)
                            MediaControlClass.GetInstanze().MediaLibrary.Add(i);

                    }

I added every MediaFile to the MediaControlClass.GetInstanze().MediaLibrary which was really to slow.

Second way:

serializer = new XmlSerializer(typeof(System.Collections.ObjectModel.ObservableCollection<MediaFile>));
                XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
                xmlReaderSettings.CheckCharacters = false;

                using (XmlReader reader = XmlReader.Create(Environment.CurrentDirectory + "\\Notes.xml", xmlReaderSettings))
                {
                    o = serializer.Deserialize(reader);
                    MediaControlClass.GetInstanze().MediaLibrary = (System.Collections.ObjectModel.ObservableCollection<MediaFile>)o;

                }

This is fast, but I have to rebind a lot of WPF Controls.

Is there some way to do this fast and without rebinding?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦初启 2024-11-01 01:16:14

这只是一个疯狂的猜测,但是在第二种情况下发出手动属性更改通知会有帮助吗?

至于第一种方法,可能是因为您在 foreach 内进行类型转换并且它在每个循环上进行转换,所以它可能很慢?

This is just a wild guess, but would raising a manual property changed notification help in the second case?

As for the first method, might be that it's slow because you're doing a typecast inside foreach and it casts on every loop?

秋凉 2024-11-01 01:16:14

我同意戴恩的观点。如果您在 MediaLibrary setter 中引发 OnPropertyChanged 事件,则不必移动任何绑定。

I agree with dain. If you raise a OnPropertyChanged event in MediaLibrary setter, you should not have to move any binding.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文