gpt4 book ai didi

.net - 如何实现保存/加载功能?

转载 作者:行者123 更新时间:2023-12-02 08:40:10 25 4
gpt4 key购买 nike

我正在尝试为 Windows 窗体应用程序实现加载/保存功能。

我有以下组件:

  • TreeView
  • 几个 ListView
  • 几个文本框
  • 几个对象(包含一个大字典列表)

我想实现一种将所有这些保存到文件中的方法,并稍后恢复/加载它。

最好的方法是什么?

我认为 XML 序列化是可行的方法,但我不太确定如何或从哪里开始。或者是否需要一个非常复杂的解决方案才能做到这一点?

最佳答案

这是一个绑定(bind)对象和一些祖先的示例到用户界面;这里使用 C# 3.0 纯粹是为了简洁 -一切都适用于 C# 2.0。

这里的大部分代码都是设置表单,和/或处理属性更改通知 -重要的是,没有任何代码专门用于更新来自对象模型的 UI,或来自用户界面。

另请注意,IDE 可以执行大量数据绑定(bind)代码对于您来说,只需将 BindingSource 拖放到表单并将数据源设置为一种类型属性网格中的对话框。

请注意,提供属性更改并不是必需的通知(PropertyChanged 的​​东西) - 但是,大多数 2 向 UI 绑定(bind)的效果会好得多如果你执行这个。并不是说 PostSharp 有一些用最少的代码实现这一点的有趣方法。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;
static class Program { // formatted for vertical space
[STAThread]
static void Main() {
Application.EnableVisualStyles();

Button load, save, newCust;
BindingSource source = new BindingSource { DataSource = typeof(Customer) };
XmlSerializer serializer = new XmlSerializer(typeof(Customer));
using (Form form = new Form {
DataBindings = {{"Text", source, "Name"}}, // show customer name as form title
Controls = {
new DataGridView { Dock = DockStyle.Fill, // grid of orders
DataSource = source, DataMember = "Orders"},
new TextBox { Dock = DockStyle.Top, ReadOnly = true, // readonly order ref
DataBindings = {{"Text", source, "Orders.OrderRef"}}},
new TextBox { Dock = DockStyle.Top, // editable customer name
DataBindings = {{"Text", source, "Name"}}},
(save = new Button { Dock = DockStyle.Bottom, Text = "save" }),
(load = new Button{ Dock = DockStyle.Bottom, Text = "load"}),
(newCust = new Button{ Dock = DockStyle.Bottom, Text = "new"}),
}
})
{
const string PATH = "customer.xml";
form.Load += delegate {
newCust.PerformClick(); // create new cust when loading form
load.Enabled = File.Exists(PATH);
};
save.Click += delegate {
using (var stream = File.Create(PATH)) {
serializer.Serialize(stream, source.DataSource);
}
load.Enabled = true;
};
load.Click += delegate {
using (var stream = File.OpenRead(PATH)) {
source.DataSource = serializer.Deserialize(stream);
}
};
newCust.Click += delegate {
source.DataSource = new Customer();
};
Application.Run(form);
}
}
}

[Serializable]
public sealed class Customer : NotifyBase {
private int customerId;
[DisplayName("Customer Number")]
public int CustomerId {
get { return customerId; }
set { SetField(ref customerId, value, "CustomerId"); }
}

private string name;
public string Name {
get { return name; }
set { SetField(ref name, value, "Name"); }
}

public List<Order> Orders { get; set; } // XmlSerializer demands setter

public Customer() {
Orders = new List<Order>();
}
}

[Serializable]
public sealed class Order : NotifyBase {
private int orderId;
[DisplayName("Order Number")]
public int OrderId {
get { return orderId; }
set { SetField(ref orderId, value, "OrderId"); }
}

private string orderRef;
[DisplayName("Reference")]
public string OrderRef {
get { return orderRef; }
set { SetField(ref orderRef, value, "OrderRef"); }
}

private decimal orderValue, carriageValue;

[DisplayName("Order Value")]
public decimal OrderValue {
get { return orderValue; }
set {
if (SetField(ref orderValue, value, "OrderValue")) {
OnPropertyChanged("TotalValue");
}
}
}

[DisplayName("Carriage Value")]
public decimal CarriageValue {
get { return carriageValue; }
set {
if (SetField(ref carriageValue, value, "CarriageValue")) {
OnPropertyChanged("TotalValue");
}
}
}

[DisplayName("Total Value")]
public decimal TotalValue { get { return OrderValue + CarriageValue; } }
}

[Serializable]
public class NotifyBase { // purely for convenience
[field: NonSerialized]
public event PropertyChangedEventHandler PropertyChanged;

protected bool SetField<T>(ref T field, T value, string propertyName) {
if (!EqualityComparer<T>.Default.Equals(field, value)) {
field = value;
OnPropertyChanged(propertyName);
return true;
}
return false;
}
protected virtual void OnPropertyChanged(string propertyName) {
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}

关于.net - 如何实现保存/加载功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/348022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com