我有一些属性,我想以 json 格式保存一些特定的属性值。这是我的代码,我想保存两个属性值,如 SelectedScalesModel 和 SelectedScales port 谁能帮我解决这个问题。public class SetUpViewModel : ViewModelBase{ public List<string> ScalesModel { get; set; } = new List<string> { "None", "METTLER-TOLEDO", "DINI ARGEO DFW-DFWK", "ESSAE SI-810" }; private string _selectedScalesModel; public string SelectedScalesModel { get { return _selectedScalesModel; } set { _selectedScalesModel = value; RaisePropertyChanged("SelectedScalesModel"); } } public List<string> ScalesPort { get; set; } = new List<string> { "None", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", "COM10", "COM11", "COM12", "COM13", "COM14", "COM15" }; private string _selectedScalesPort; public string SelectedScalesPort { get { return _selectedScalesPort; } set { _selectedScalesPort = value; RaisePropertyChanged("SelectedScalesPort"); } } string _text1; public string BlackLineText { get { return _text1; } set { _text1 = value; RaisePropertyChanged(nameof(BlackLineText)); } } public RelayCommand SaveButtonCommand { get; private set; } public SetUpViewModel() { SaveButtonCommand = new RelayCommand(SaveCommand); } private void SaveCommand() { SetUpViewModel setUpobj = new SetUpViewModel(); string strJsonResult = JsonConvert.SerializeObject(setUpobj); File.WriteAllText("setup.json", strJsonResult); MessageBox.Show("File save in Json Format"); }}
1 回答
炎炎设计
TA贡献1808条经验 获得超4个赞
您可以尝试SerializeObject通过匿名类然后携带您的期望属性而不是SetUpViewModel对象。
private void SaveCommand()
{
string strJsonResult = JsonConvert.SerializeObject(
new {
SelectedScalesModel = this.SelectedScalesModel,
SelectedScalesPort = this.SelectedScalesPort
}
);
File.WriteAllText("setup.json", strJsonResult);
MessageBox.Show("File save in Json Format");
}
笔记
使用this因为您的对象中的属性信息。
- 1 回答
- 0 关注
- 175 浏览
添加回答
举报
0/150
提交
取消