C# 如何序列化和反序列化TABC控件#
我正在使用一个tabcontrol,我想序列化并保存它。我正在使用这段代码,但它给出了tabcontrol类未标记为可序列化的消息。如何将其标记为可序列化,因为我无法重写该类。怎么做C# 如何序列化和反序列化TABC控件#,c#,winforms,serialization,tabcontrol,C#,Winforms,Serialization,Tabcontrol,我正在使用一个tabcontrol,我想序列化并保存它。我正在使用这段代码,但它给出了tabcontrol类未标记为可序列化的消息。如何将其标记为可序列化,因为我无法重写该类。怎么做 using (Stream stream = File.Open("data.dat", FileMode.Create)) { BinaryFormatter bin = new BinaryFormatter(); bin.Serialize(stream,tabControl1); } 它给
using (Stream stream = File.Open("data.dat", FileMode.Create))
{
BinaryFormatter bin = new BinaryFormatter();
bin.Serialize(stream,tabControl1);
}
它给出了这个错误
System.Windows.Forms.TabControl未标记为可序列化
为什么不序列化控件,还有其他选择吗?
如果序列化控件,则会出现以下问题:
System.Windows.Forms.TabControl
没有像您看到的那样标记为可序列化[Serializable] // don't forget this! It will mark your class so you can serialize it.
public class BindingClass // p.s.: give this a better name!
{
public string Text { get; set; } // Bind whit a control of your tab control.
public float Number { get; set; }
public string ImageLocation { get; set; } // used for the image
public IEnumerable<object> ListOfString { get; set; } // used for a list
}
图像
对于图像来说有点复杂。您可以序列化图像,但这样做也是一件坏事。更好的方法是将映像保存在项目的bin/debug
文件夹中,并序列化该映像的路径。例如:
BindingClass bc = new BindingClass();
bc.Text = textBox1.Text;
bc.Number = numbericUpDown.Value;
using (Stream stream = File.Open("data.dat", FileMode.Create))
{
BinaryFormatter bin = new BinaryFormatter();
bin.Serialize(stream, bc);
}
string imageLocation = Application.StartupPath + @"\myImage.jpg"
pictureBox1.Image.Save(imageLocation, ImageFormat.Jpeg);
// declare bc like code above.
bc.ImageLocation = imageLocation;
// serialize bc.
如果文件中已存在该图像,则可以覆盖它。但是如果你愿意研究历史,这不是一件好事。。。您可以使用当前日期时间作为文件名来解决此问题!使用以下命令更改代码:
string imageLocation = Application.StartupPath +
DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg"
注意:您还可以使用and(非免费)等服务,或将图像上载到或9gag(免费)。请注意,客户端和服务器之间必须有internet连接。你可以通过搜索如何上传
字符串列表
对于字符串列表,您可以使用以下命令:
bc.ListOfString = comboBox1.Items;
注
我还没有测试代码。因此,如果你有一个例子的问题,评论它,我会看看它,但也试着看看谷歌为您的问题的解决方案。你自己试试,最好的学习方法
序列化的备选方案(2016年6月16日更新)
序列化是一种使人们无法阅读代码的节省方法。但是,如果扩展应用程序,可能会出现问题。Microsoft Word也会出现此问题。旧的.doc
文件也是序列化的代码,新的.docx
文件是压缩的xml文件,现在更容易生成.docx
文件
好的替代方案是或。为什么不序列化控件,还有替代方案吗?
如果序列化控件,则会出现以下问题:
System.Windows.Forms.TabControl
没有像您看到的那样标记为可序列化[Serializable] // don't forget this! It will mark your class so you can serialize it.
public class BindingClass // p.s.: give this a better name!
{
public string Text { get; set; } // Bind whit a control of your tab control.
public float Number { get; set; }
public string ImageLocation { get; set; } // used for the image
public IEnumerable<object> ListOfString { get; set; } // used for a list
}
图像
对于图像来说有点复杂。您可以序列化图像,但这样做也是一件坏事。更好的方法是将映像保存在项目的bin/debug
文件夹中,并序列化该映像的路径。例如:
BindingClass bc = new BindingClass();
bc.Text = textBox1.Text;
bc.Number = numbericUpDown.Value;
using (Stream stream = File.Open("data.dat", FileMode.Create))
{
BinaryFormatter bin = new BinaryFormatter();
bin.Serialize(stream, bc);
}
string imageLocation = Application.StartupPath + @"\myImage.jpg"
pictureBox1.Image.Save(imageLocation, ImageFormat.Jpeg);
// declare bc like code above.
bc.ImageLocation = imageLocation;
// serialize bc.
如果文件中已存在该图像,则可以覆盖它。但是如果你愿意研究历史,这不是一件好事。。。您可以使用当前日期时间作为文件名来解决此问题!使用以下命令更改代码:
string imageLocation = Application.StartupPath +
DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg"
注意:您还可以使用and(非免费)等服务,或将图像上载到或9gag(免费)。请注意,客户端和服务器之间必须有internet连接。你可以通过搜索如何上传
字符串列表
对于字符串列表,您可以使用以下命令:
bc.ListOfString = comboBox1.Items;
注
我还没有测试代码。因此,如果你有一个例子的问题,评论它,我会看看它,但也试着看看谷歌为您的问题的解决方案。你自己试试,最好的学习方法
序列化的备选方案(2016年6月16日更新)
序列化是一种使人们无法阅读代码的节省方法。但是,如果扩展应用程序,可能会出现问题。Microsoft Word也会出现此问题。旧的.doc
文件也是序列化的代码,新的.docx
文件是压缩的xml文件,现在更容易生成.docx
文件
好的替代方案是或。为什么不序列化控件,还有替代方案吗?
如果序列化控件,则会出现以下问题:
System.Windows.Forms.TabControl
没有像您看到的那样标记为可序列化[Serializable] // don't forget this! It will mark your class so you can serialize it.
public class BindingClass // p.s.: give this a better name!
{
public string Text { get; set; } // Bind whit a control of your tab control.
public float Number { get; set; }
public string ImageLocation { get; set; } // used for the image
public IEnumerable<object> ListOfString { get; set; } // used for a list
}
图像
对于图像来说有点复杂。您可以序列化图像,但这样做也是一件坏事。更好的方法是将映像保存在项目的bin/debug
文件夹中,并序列化该映像的路径。例如:
BindingClass bc = new BindingClass();
bc.Text = textBox1.Text;
bc.Number = numbericUpDown.Value;
using (Stream stream = File.Open("data.dat", FileMode.Create))
{
BinaryFormatter bin = new BinaryFormatter();
bin.Serialize(stream, bc);
}
string imageLocation = Application.StartupPath + @"\myImage.jpg"
pictureBox1.Image.Save(imageLocation, ImageFormat.Jpeg);
// declare bc like code above.
bc.ImageLocation = imageLocation;
// serialize bc.
如果文件中已存在该图像,则可以覆盖它。但是如果你愿意研究历史,这不是一件好事。。。您可以使用当前日期时间作为文件名来解决此问题!使用以下命令更改代码:
string imageLocation = Application.StartupPath +
DateTime.Now.ToString("yyyyMMddHHmmss") + ".jpg"
注意:您还可以使用and(非免费)等服务,或将图像上载到或9gag(免费)。请注意,客户端和服务器之间必须有internet连接。你可以通过搜索如何上传
字符串列表
对于字符串列表,您可以使用以下命令:
bc.ListOfString = comboBox1.Items;
注
我还没有测试代码。因此,如果你对其中一个例子有问题,请评论它,我会看一看,但也试着在谷歌上看一看