user1182622
user1182622

Reputation: 1

Load Element from XAML

I have a custom XAML User Control like this:

<UserControl x:Class="CheckPoint.Modules.Beach.Beach_Shape"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Polygon Name="Shape"></Polygon>
    </Grid>
</UserControl>

I want xaml serialize it with XamlWrite.Save and then reload it with XamlReader.Load.

XmlReader reader = XmlReader.Create(new StringReader(xml));
UserControl uc=(UserControl)XamlReader.Load(reader);
myGrid.Children.Add(uc);

"uc" is correctly visualized on myGrid, but "uc" Object is not logical correct, becouse the Shape element is not correctly loaded, for example it has not Background, Stroke or Points setted even though it are in xaml.

I try to reload it with

Shape=myGrid.Findname("Shape");

but it doesn't work too.

So, where is my mistake?

Upvotes: 0

Views: 655

Answers (1)

Anatolii Gabuza
Anatolii Gabuza

Reputation: 6260

This might be an answer for your question (from MSDN article Serialization Limitations of XamlWriter.Save):

The basic philosophy of what is serialized by a call to Save is that the result will be a representation of the object being serialized, at run-time. Many design-time properties of the original XAML file may already be optimized or lost by the time that the XAML is loaded as in-memory objects, and are not preserved when you call Save to serialize. The serialized result is an effective representation of the constructed logical tree of the application, but not necessarily of the original XAML that produced it. These issues make it extremely difficult to use the Save serialization as part of an extensive XAML design surface.

Upvotes: 1

Related Questions