开发者

Set two ObservableCollection equal, without losing WPF-Binding

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);

                开发者_C百科    }

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?


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?


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜