我有关于ViewModel的序列化和反序列化的问题-我创建了ViewModel的示例并将其分配给视图的DataContext。我有一个包含ViewModel中对象的列表,我需要对其进行序列化和反序列化。
在反序列化时,列表的大小一直在增长,不确定这里发生了什么。ViewModel不就是类的另一个示例吗?
例如,我从一个类的2个对象开始,我序列化并反序列化它,最后得到4条记录,记录的大小不断增加。
我为此做了一个测试场景,我使用的是JSON序列化器。
//---查看模型---//
using System;
using System.Windows;
using System.Windows.Input;
using Newtonsoft.Json;
namespace JsonTest
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
CarVM = new CarViewModel(); //Create instance if View Model and assign it to the datacontext
this.DataContext = CarVM;
}
private CarViewModel CarVM { get; set; }
/// <summary>
/// Serialize
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Serialize_Click(object sender, RoutedEventArgs e)
{
//Serialize and write to file
using (var fileStream = new FileStream(@"Config123.cfg", FileMode.Create))
using (var streamWriter = new StreamWriter(fileStream))
using (var jsonWriter = new JsonTextWriter(streamWriter))
{
JsonSerializer ser = new JsonSerializer();
ser.Formatting = Newtonsoft.Json.Formatting.Indented; //Format the output
ser.TypeNameHandling = TypeNameHandling.Auto;
ser.Serialize(jsonWriter, CarVM); //Serailizing View Model objects
jsonWriter.Flush();
}
printData();
}
/// <summary>
/// Deserialize
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Deserialize_Click(object sender, RoutedEventArgs e)
{
using (var fileStream = new FileStream(@"Config123.cfg", FileMode.Open))
using (var streamReader = new StreamReader(fileStream))
using (var jsonReader = new JsonTextReader(streamReader))
{
JsonSerializer ser = new JsonSerializer();
ser.Formatting = Newtonsoft.Json.Formatting.Indented;
ser.TypeNameHandling = TypeNameHandling.Auto;
CarVM = ser.Deserialize<CarViewModel>(jsonReader); //reload the ViewModel with new viewmodel obtained from serializing
}
Console.WriteLine("--Deserialization--");
printData();
}
/// <summary>
/// Print Data
/// </summary>
private void printData()
{
foreach(Car c in CarVM.Cars)
{
Console.WriteLine(c.make + " " + c.year);
}
}
}
}
//---简单类---//
namespace JsonTest
{
class Car
{
public string make { get; set; }
public int year { get; set; }
public Car() { }
public Car(string make, int year)
{
this.make = make;
this.year = year;
}
}
}
//---查看----//
<Window x:Class="JsonTest.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:JsonTest"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button Content="Serialize" Margin="50" BorderThickness="0" Click="Serialize_Click"/>
<Button Grid.Column="1" Content="Deserialize" Margin="50" BorderThickness="0" Click="Deserialize_Click"/>
</Grid>
</Window>
//---输出---//
--Serialization--
DeLorean 1975
Subaru 2018
--Deserialization--
DeLorean 1975
Subaru 2018
DeLorean 1975
Subaru 2018
--Serialization--
DeLorean 1975
Subaru 2018
DeLorean 1975
Subaru 2018
--Deserialization--
DeLorean 1975
Subaru 2018
DeLorean 1975
Subaru 2018
DeLorean 1975
Subaru 2018
//---汽车视图模型---//
class CarViewModel
{
public CarViewModel()
{
Cars = new List<Car>();
Cars.Add(new Car("DeLorean", 1975));
Cars.Add(new Car("Subaru", 2018));
}
private List<Car> cars;
public List<Car> Cars
{
get { return cars; }
set { cars = value; }
}
}
1条答案
按热度按时间jgwigjjp1#
在ViewModel构造函数中,添加两辆汽车。
序列化时,它将序列化这两辆汽车。
反序列化时,反序列化器使用默认构造函数创建类的示例。默认构造函数添加这两辆汽车。然后反序列化器添加在JSON中序列化的两辆车。
这不是你想的行为。