Good Morning.
I have the following problem:
I got a SaveData class which should serialize an ObservableCollection<personmodel> to a XML document.
The ViewModel contains this collection while PersonModel is Model class.
I can not reference the Model in the SaveData class because this would cause an infinite dependency.
When I invoke the SerializeData() method in the ViewModel it says that it can not assign the ObservableCollection<personmodel> to the parameter type ObservableCollection<object>.
Seems logical...but how is it possible to cast the ObservableCollection?
Code for the SaveData class:
public class SaveData
{
private XmlSerializer serializer;
private string saveLogPath;
public SaveData()
{
serializer = new XmlSerializer(typeof(ObservableCollection<object>));
}
private string GetSaveLogPath()
{
const string saveLog = "SaveLog.xml";
var dir = AppDomain.CurrentDomain.BaseDirectory+saveLog;
return dir;
}
public void SerializeData(ObservableCollection<object> dataToSerialize)
{
saveLogPath = GetSaveLogPath();
if (File.Exists(saveLogPath))
{
var stream = new FileStream(saveLogPath, FileMode.Open);
serializer.Serialize(stream,dataToSerialize);
}
else
{
File.Create(saveLogPath);
var stream = new FileStream(saveLogPath, FileMode.Open);
serializer.Serialize(stream, dataToSerialize);
}
}
}
Code for the ViewModel:
class MainWindowViewModel : INotifyPropertyChanged
{
private ObservableCollection<PersonModel> personList;
public MainWindowViewModel()
{
personList = new ObservableCollection<PersonModel>();
}
public void SaveData(object parameter)
{
serializer = new SaveData();
serializer.SerializeData(PersonList);
}
}
Any help is appreciated.
Thanks in advance.