如何在MVVM ListView中保存和加载ObservableCollection。错误低于..
未处理的类型' System.InvalidOperationException' 发生在System.Xml.dll
中其他信息:Database_MVVM.Model.UserData不能 序列化,因为它没有无参数构造函数。
在我的MainViewModel
中public ObservableCollection<UserData> _userDataCollection = new ObservableCollection<UserData>();
public ObservableCollection<UserData> UserDataCollection
{
get { return _userDataCollection; }
set { _userDataCollection = value; }
}
public ICommand SaveCommand
{
get
{
if (saveCommand == null)
{
saveCommand = new DelegateCommand(Save);
}
return saveCommand;
}
}
private void Save()
{
XmlSerializer serializer = new XmlSerializer(typeof(ObservableCollection<UserData>));
using (var sw = new StreamWriter("output.txt"))
{
serializer.Serialize(sw, UserDataCollection);
sw.Close();
}
}
我的模特
public class UserData
{
#region Declarations
private string _theEnteredData;
private string _theRandomData;
public UserData(string theEnteredData, string theRandomData)
{
this._theEnteredData = theEnteredData;
this._theRandomData = theRandomData;
}
#endregion
#region Properties
public string theEnteredData
{
get { return _theEnteredData; }
set { _theEnteredData = value; }
}
public string theRandomData
{
get { return _theRandomData; }
set { _theRandomData = value; }
}
#endregion
}
在My Command中,我创建了DelegateCommand.cs
public class DelegateCommand : ICommand
{
private readonly Action _action;
public DelegateCommand(Action action)
{
this._action = action;
}
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
this._action();
}
public event EventHandler CanExecuteChanged
{
add
{
}
remove
{
}
}
我在listview中保存项目后会自动清除。加载保存文件(txt文件)并加载它,而不使用openfiledialogbox和openfiledialog框。
我尝试使用streamwriter和foreach循环将其保存为文本文件,但我失败了我只是刚接触MVVM并仍在尝试学习它。
答案 0 :(得分:1)
正如异常所述:UserData没有默认构造函数。
序列化程序使用默认构造函数来创建实例,因此如果缺少序列化程序,则无法创建对象。
添加默认构造函数:
public class UserData
{
#region Declarations
private string _theEnteredData;
private string _theRandomData;
public UserData()
{
}
public UserData(string theEnteredData, string theRandomData)
{
this._theEnteredData = theEnteredData;
this._theRandomData = theRandomData;
}
#endregion
#region Properties
public string theEnteredData
{
get { return _theEnteredData; }
set { _theEnteredData = value; }
}
public string theRandomData
{
get { return _theRandomData; }
set { _theRandomData = value; }
}
#endregion
}
答案 1 :(得分:-1)
XmlSerializer获取一个对象并将其转换为xml,您可以将其保存到文本文件中。然后可以在需要时对其进行反序列化。
public void SaveToFile()
{
XmlSerializer serializer = new XmlSerializer(typeof(ObservableCollection<UserData>));
using (var sw = new StreamWriter("output.txt"))
{
serializer.Serialize(sw, UserDataCollection);
sw.Close();
}
}
public void LoadFromFile()
{
XmlSerializer serializer = new XmlSerializer(typeof(ObservableCollection<UserData>));
using (Stream reader = new FileStream("output.txt", FileMode.Open))
{
// Call the Deserialize method to restore the object's state.
UserDataCollection = (ObservableCollection<UserData>)serializer.Deserialize(reader);
}
}
答案 2 :(得分:-1)
您可以在mainViewModel中尝试此设置吗?
public ObservableCollection<UserData> _userDataCollection = new ObservableCollection<UserData>();
public ObservableCollection<UserData> UserDataCollection
{
get { return _userDataCollection; }
set
{
_userDataCollection = value;
RaisePropertyChanged(() => UserDataCollection);
}
}
public ICommand SaveCommand
{
get
{
//Bind UserDataCollection to your List View
LoadDataToListView();
}
}
private void LoadDataToListView()
{
using (var sw = new StreamWriter("output.txt"))
{
//load in UserDataCollection your data from textfile in here
sw.Close();
}
return UserDataCollection ;
}
只需从文本文件中加载UserDataCollection中的数据,然后将其绑定到列表视图即可。您将使用相同的模型。这对我很好。我希望我帮助你。祝你好运。
您的模型更新
public class UserData
{
#region Declarations
private string _theEnteredData;
private string _theRandomData;
public UserData(string theEnteredData, string theRandomData)
{
theEnteredData = theEnteredData;
theRandomData = theRandomData;
}
public UserData(){}
#endregion
#region Properties
public string theEnteredData
{
get { return _theEnteredData; }
set { _theEnteredData = value; }
}
public string theRandomData
{
get { return _theRandomData; }
set { _theRandomData = value; }
}
#endregion
}