//我在这里序列化了我的对象
namespace Vdrop
{
[Serializable]
public class DCSelections : INotifyPropertyChanged
{
private double _systemVoltage = 125;
public double SystemVoltage
{
get { return _systemVoltage; }
set
{
_systemVoltage = value;
OnPropertyChanged("SystemVoltage");
}
}
//我使用一个事件来进行序列化和反序列化
private void Open_Click(object sender, RoutedEventArgs e)
{
IFormatter serializer = new BinaryFormatter();
// Show the dialog and get result.
// Configure open file dialog box
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.FileName = "Vdrop1"; // Default file name
dlg.DefaultExt = ".vdp"; // Default file extension
dlg.Filter = "Vdrop documents (.vdp)|*.vdp"; // Filter files by extension
string path = Directory.GetCurrentDirectory();//use working directory
dlg.InitialDirectory = path;
// Show open file dialog box
Nullable<bool> result = dlg.ShowDialog();
//Process open file dialog box results
if (result == true)
{
// Open document
string filename = dlg.FileName;
FileStream loadFile = new FileStream(filename, FileMode.Open, FileAccess.Read);
dCSelections = serializer.Deserialize(loadFile) as DCSelections;
loadFile.Close();
}
}
private void Save_Click(object sender, RoutedEventArgs e)
{
IFormatter serializer = new BinaryFormatter();
// Configure save file dialog box
Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
dlg.FileName = "Vdrop1"; // Default file name
dlg.DefaultExt = ".vdp"; // Default file extension
dlg.Filter = "Vdrop documents (.vdp)|*.vdp"; // Filter files by extension
string path = Directory.GetCurrentDirectory();//use working directory
dlg.InitialDirectory = path;
// Show save file dialog box
Nullable<bool> result = dlg.ShowDialog();
// Process save file dialog box results
if (result == true)
{
// Save document
string filename = dlg.FileName;
FileStream saveFile = new FileStream(filename, FileMode.Create, FileAccess.Write);
serializer.Serialize(saveFile, dCSelections);
saveFile.Close();
}
}
//我知道它会反序列化。但是我的屏幕(WPF)没有显示SystemVoltage的反序列化值。这是我编写的第一个c#代码,我可能没有正确发布这些注释和代码。对不起,我很难过。