设置两个 ObservableCollection 相等,而不丢失 WPF-Binding
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这只是一个疯狂的猜测,但是在第二种情况下发出手动属性更改通知会有帮助吗?
至于第一种方法,可能是因为您在 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?我同意戴恩的观点。如果您在 MediaLibrary setter 中引发 OnPropertyChanged 事件,则不必移动任何绑定。
I agree with dain. If you raise a OnPropertyChanged event in MediaLibrary setter, you should not have to move any binding.