I saw this post XamlWriter deepcopy to copy contents of one grid to the another. It asks to use a XamlWriter and the pass on the string to XamlReader. But the XamlWriter class is not available in uwp. So I came across this article Serializing in uwp asking to use a XmlSerializer instead. When I try the following I get an exception saying cannot serialize non public members. Please help. In simple terms how can I clone the contents of one Grid to another in UWP.
xaml: I want to clone elements in this Grid into a new Grid from code behind.
<Grid x:Name="gridBarImagePanel"
RenderTransformOrigin="0.5,0.5"
Width="800" VerticalAlignment="Stretch">
<Image x:Name="BarCodeImage" Stretch="None"></Image>
<Canvas Background="Pink" x:Name="cnvBarCodeImage" Opacity="0.5" AllowDrop="True">
</Canvas>
</Grid>
I tried the following but get an exception
var tempGrid = new Grid();
XmlSerializer serializer = new XmlSerializer(typeof(Grid));
StringWriter stringWriter = new StringWriter();
serializer.Serialize(stringWriter, gridBarImagePanel.Children); //Gives an exception saying it does not have a parameterless constructor.
So I tried the following to do a deep copy. But it complains saying gridBarImagePanel.Children is not marked as Serializable. When I try to create a custom class that inherits UIElementCollection that class is sealed.
string content;
using (var stream = new MemoryStream())
{
BinaryFormatter f = new BinaryFormatter();
f.Serialize(stream, gridBarImagePanel.Children);
stream.Position = 0;
content = new StreamReader(stream).ReadToEnd();
}