I am getting a System.Windows.Controls.UIElementCollection from a System.Windows.Controls.Canvas object in WPF, it contains a lot of System.Windows.Shape objects
What I am trying to achieve is to serialize these objects in the Canvas, then deserialize them later: like a save/load.
Serialization code:
var items = view.myCanvas.Children;
var mystrXaml = XamlWriter.Save(items);
var fs = File.Create(@"C:\data.xaml");
var sw = new StreamWriter(fs);
sw.Write(mystrXaml);
sw.Close();
fs.Close();
Deserialization code:
var sr = new StreamReader(@"C:\data.xaml");
var xmlReader = new XmlTextReader(sr);
var xamlReader = XamlReader.Load(xmlReader);
Serializing seems to be fine, while deserializing gives this error:
No matching constructor found on type 'System.Windows.Controls.UIElementCollection'. You can use the Arguments or FactoryMethod directives to construct this type.' Line number '1' and line position '22'.
I conceptually understand that there are no parameterless constructors for UIElementCollection as it takes two parameters:
UIElementCollection(UIElement visualParent, FrameworkElement logicalParent);
How can I inject these parameters while the UIElementCollection is getting deserialized? Or am I going about this the wrong way?
Thanks
Small edit: the reason why I thought this was possible is because this library: http://www.codeproject.com/Articles/620154/UniversalSerializer seems to be able to cater exactly for this case, but I can't figure out exactly how it does it, there seems to be some reflection magic.