好的,怎么样:
创建一个类以将重新创建名片(10 个标签和 3 个组合框)所需的信息存储为公共的、可获取/可设置的属性,并使用空的默认公共构造函数。然后,将每张名片作为 xml(或二进制)文件序列化XmlSerializer
到一个文件夹中。然后,您可以使用类似的东西string[] businessCards = Directory.GetFiles(Path.GetFullPath("mysettings\\businesscards"));
来遍历您长长的“缓存”名片列表。FlowLayoutPanel
通过调用如下函数来管理添加/删除项目的类: SerializableBusinessCardClass GetNextCard() {}
. 这将是相当简单的实现。你也可以序列化一个List<SerializableBusinessCardClass>
将大约 5 个长度(或者您想一次加载的多个)添加到单个 XML 文件中以获得最大效率,或者如果您的名片数量非常荒谬(这样浏览器在浏览文件夹时会滞后)。虽然二进制序列化会更快,但 XML 方法还有一个额外的好处,即您的客户可以通过自己创建 XML 文件来指定他们希望您显示的新名片。
在这里,我会给你一个具体的例子,说明如何构建和序列化这样的数据结构:
public class SerializableBusinessCard
{
public SerializableBusinessCard() { }
public string Name { get; set; }
public string Company { get; set; }
public List<string> Labels { get; set; }
public List<ComboItem> ComboBoxes { get; set; }
}
public class ComboItem
{
public ComboItem() { }
public string Name { get; set; }
public string Text { get; set; }
public int SelectedIndex { get; set; }
public Point Location { get; set; }
public Size size { get; set; }
public List<string> Collection{ get; set; }
}
用法:
public void stackoverflow_BusinessCard_FlowLayoutPanel()
{
List<string> labels = new List<string>();
labels.Add("Title");
labels.Add("Description");
labels.Add("Phone");
labels.Add("Email");
labels.Add("Address");
labels.Add("label6");
labels.Add("labelN");
ComboItem cItem = new ComboItem();
cItem.Collection = new List<string>();
cItem.Collection.Add("Option1");
cItem.Collection.Add("Option2");
cItem.Name = "comboName";
cItem.SelectedIndex = 0;
cItem.Text = cItem.Collection[cItem.SelectedIndex];
cItem.Location = new Point(50, 265);
cItem.size = new Size(100,21);
List<ComboItem> comboItems = new List<ComboItem>();
comboItems.Add(cItem);
SerializableBusinessCard bCard1 = new SerializableBusinessCard();
bCard1.Name = "CompanyXYZ_BlueTheme";
bCard1.Company = "CompanyXYZ";
bCard1.Labels = labels;
bCard1.ComboBoxes = comboItems;
SerializeObjectXML("BusinessCard_392.xml",bCard1);
SerializableBusinessCard loaded = DeserializeBusinessCardXML("BusinessCard_392.xml");
}
下面是序列化函数:
public void SerializeObjectXML(string filename,object obj)
{
using(StreamWriter streamWriter = new StreamWriter(filename))
{
XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType());
xmlSerializer.Serialize(streamWriter,obj);
}
}
结果:
<?xml version="1.0" encoding="utf-8"?>
<SerializableBusinessCard xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name>CompanyXYZ_BlueTheme</Name>
<Company>CompanyXYZ</Company>
<Labels>
<string>Title</string>
<string>Description</string>
<string>Phone</string>
<string>Email</string>
<string>Address</string>
<string>label6</string>
<string>labelN</string>
</Labels>
<ComboBoxes>
<ComboItem>
<Name>comboName</Name>
<Text>Option1</Text>
<SelectedIndex>0</SelectedIndex>
<Location>
<X>50</X>
<Y>265</Y>
</Location>
<size>
<Width>100</Width>
<Height>21</Height>
</size>
<Collection>
<string>Option1</string>
<string>Option2</string>
</Collection>
</ComboItem>
</ComboBoxes>
</SerializableBusinessCard>
和解串器:
public static SerializableBusinessCard DeserializeBusinessCardXML(string filename)
{
SerializableBusinessCard result = new SerializableBusinessCard();
using(StreamReader streamReader = new StreamReader(filename))
{
XmlSerializer xmlReader = new XmlSerializer(typeof(SerializableBusinessCard));
result = (SerializableBusinessCard) xmlReader.Deserialize(streamReader);
}
return result;
}
希望这可以帮助。