将ObservableCollection作为方法中的参数传递给另一个类

时间:2014-11-14 14:06:30

标签: c# wpf object observablecollection

我想将ObservableCollection作为方法中的参数传递给另一个类中的另一个方法。来自源类的方法调用看起来像saveLoadSerialization.Save(ClassDatas,Connectors, fileName);ClassDatasConnectors都是对象。这些对象在源类中定义如下:

public ObservableCollection<ClassData> ClassDatas { get; set; } public ObservableCollection<Connector> Connectors { get; set; }

存在Save-method的目标类如下所示:

public void Save(IList _classDatas, IList _connectors, String fileName)
{
    // ClassData Object
    try
    {
       XmlSerializer xsClassData = new XmlSerializer(typeof(ObservableCollection<ClassData>)); <--- fails here
       if (_classDatas.Count > 0)
       {
          using (StreamWriter swClassData = new StreamWriter(fileName))
          {
              xsClassData.Serialize(swClassData, _classDatas);
          }
       }
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
     }

     // Connector Object
     try
     {
        XmlSerializer xsConnector = new XmlSerializer(typeof(ObservableCollection<Connector>)); <-- fails here
        if (_connectors.Count > 0)
        {
            using (StreamWriter swConnector = new StreamWriter(fileName))
            {
                xsConnector.Serialize(swConnector, _connectors);
            }
        }
     }
     catch (Exception e)
     {
        Console.WriteLine(e.Message);
     }
}   

如果我将行XmlSerializer xsConnector = new XmlSerializer(typeof(ObservableCollection<Connector>));更改为XmlSerializer xsConnector = new XmlSerializer(typeof(ObservableCollection<_connectors>));,我会收到同样的错误:&#34;类型或命名空间名称&#39; _connectors&#39;无法找到&#34;。

我不明白为什么它不起作用?!

如何解决这个问题?

谢谢,

0 个答案:

没有答案