Frage

I am having problems deserializing a xaml file and get the following error

enter image description here

My code is as follows:

    private void SerializeToXML()
    {
        FileStream filestream = File.Create(@"H:\test1.xaml");
        StreamWriter streamwriter = new StreamWriter(filestream);

        foreach (ListViewItem Item in slideListView.Items)
        {
            string mystrXAMLWrite = XamlWriter.Save(Item.Tag);
            streamwriter.Write(mystrXAMLWrite);
        }

        streamwriter.Close();
        filestream.Close();

    }

    private void DeSerialize()
    {
        FileStream filestream = File.Open(@"H:\test1.xaml", FileMode.Open);

        XamlReader reader = new XamlReader();

        slideListView = (ListView)reader.LoadAsync(filestream);

    }

If I go to the XAML file after saving and change the various names it has problems with, for example changing slideCanvas to slideCanvas1 and ContextMenu to ContextMenu1, then it will load. But obviously this is not a solution and it also means that whatever is loaded back in is not pointing to the correct bits of code as they have had numbers added to the values.

Does anyone know what I need to do here?

UPDATED

Here is the xaml produced when saving one slide object

<Slide imageZIndex="0" editText="False" ClipToBounds="True" xmlns="clr-namespace:StoryboardTool;assembly=StoryboardTool" xmlns:av="http://schemas.microsoft.com/winfx/2006/xaml/presentation"><av:Canvas Background="#FFFFFFFF" Name="slideCanvas" /></Slide>

If I try to place this in a Slide Object for example

Var obj = (Slide)reader.LoadAsync(filestream);

I get this XmalParseExceptionOccured problem.

War es hilfreich?

Lösung

The error says cannot register duplicate name 'slideCanvas' in this scope. I can only assume that you have another control called 'slideCanvas' that is defined in your application somewhere. Have you tried searching through the entire solution for 'slideCanvas'?

Other than that, there are a few other issues that cause this exception:

  1. Declaring a control in the Resources section using x:Name instead of x:Key.
  2. Trying to serialize a control that has code behind.

You could also try simply removing the names from the controls and binding to them to replicate whatever functionality you named them for.

If you still have a problem, please show the XAML that is causing the error when you try to deserialize it.

Andere Tipps

How I solved this: 1. Make user control which inherits from your control 2. Add this in the constructor

static int counter = 0;
public TestControl()
{
    InitializeComponent();
    this.Name += counter.ToString();
    counter ++;
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top