0

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.

4

2 回答 2

1

错误说cannot register duplicate name 'slideCanvas' in this scope。我只能假设您在某个应用程序中定义了另一个名为“slideCanvas”的控件。您是否尝试过搜索“slideCanvas”的整个解决方案?

除此之外,还有一些其他问题会导致此异常:

  1. Resources在使用x:Name代替的部分中声明一个控件x:Key
  2. 尝试序列化包含代码的控件。

您还可以尝试简单地从控件中删除名称并绑定到它们以复制您命名它们的任何功能。

如果仍有问题,请在尝试反序列化时显示导致错误的 XAML。

于 2013-07-29T11:52:47.173 回答
-1

我是如何解决这个问题的: 1. 使用户控件继承自您的控件 2. 在构造函数中添加它

static int counter = 0;
public TestControl()
{
    InitializeComponent();
    this.Name += counter.ToString();
    counter ++;
}
于 2015-06-29T10:21:53.223 回答