我正在尝试将自定义类保存到用户设置但没有太大成功。
我在 Visual Studio 2010 中使用 Target framework =“.NET Framework 4”创建了一个测试项目。主窗体有两个文本框,用于显示和编辑纯测试字符串以及自定义类中的“标题”字段;与两个按钮一起加载和保存设置。
最后,我添加了两个设置“TestString”(纯字符串值)和 TestData(MyDataClass 的类型 - 我在项目的 Properties.Settings 选项卡中通过从 Type 下拉列表中选择“Browse...”然后手动输入“MyUserSettingsTest .MyDataClass”在“选定类型”框中,因为它最初没有出现在可用类型列表中,但随后出现了。)
在启动应用程序并单击 LoadSettings 时,事件处理程序会加载纯字符串和 MyData,或者,如果失败,则创建 MyDataClass 的新实例。然后我可以在表单上编辑纯字符串和测试数据标题,然后单击 SaveSettings。如果我随后重新编辑这些值,则再次单击 LoadSettings,最后保存的值将按预期恢复为两个值。到目前为止,一切都很好。
问题是在退出应用程序并重新启动它时,纯测试字符串恢复正常,但 MyData 对象不是!显然,代码将 MyData 持久化到内存中,但不是永久存储?
主要表单代码为:
using System;
using System.Windows.Forms;
namespace MyUserSettingsTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public MyDataClass MyData {get;set;}
private void loadSettingsButton_Click(object sender, EventArgs e)
{
this.myTextBox.Text = Properties.Settings.Default.TestString;
this.MyData = Properties.Settings.Default.TestData;
if (this.MyData == null)
{
this.MyData = new MyDataClass() { ID = 1, Title = "Default New Title" };
}
this.myDataTitleTextBox.Text = this.MyData.Title;
}
private void saveSettingsButton_Click(object sender, EventArgs e)
{
this.MyData.Title = this.myDataTitleTextBox.Text;
Properties.Settings.Default.TestString = this.myTextBox.Text;
Properties.Settings.Default.TestData = this.MyData;
Properties.Settings.Default.Save();
}
}
}
我的测试数据类代码是:
using System.Configuration;
namespace MyUserSettingsTest
{
public class MyDataClass : ApplicationSettingsBase
{
public MyDataClass()
{
}
private int _id;
private string _title;
[UserScopedSetting()]
[SettingsSerializeAs(SettingsSerializeAs.Xml)]
public int ID
{
get { return _id; }
set { _id = value; }
}
[UserScopedSetting()]
[SettingsSerializeAs(SettingsSerializeAs.Xml)]
public string Title
{
get { return _title; }
set { _title = value; }
}
}
}
我不知道它是否有帮助,但自动生成的 app.config 文件(其中包含 TestString 但不是 TestData 的设置!!?)是:
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="MyUserSettingsTest.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false"/>
</sectionGroup>
</configSections>
<userSettings>
<MyUserSettingsTest.Properties.Settings>
<setting name="TestString" serializeAs="String">
<value/>
</setting>
</MyUserSettingsTest.Properties.Settings>
</userSettings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
自动生成的 Settings.Designer.cs 文件是:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.269
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace MyUserSettingsTest.Properties {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "10.0.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
public static Settings Default {
get {
return defaultInstance;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("")]
public string TestString {
get {
return ((string)(this["TestString"]));
}
set {
this["TestString"] = value;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public global::MyUserSettingsTest.MyDataClass TestData {
get {
return ((global::MyUserSettingsTest.MyDataClass)(this["TestData"]));
}
set {
this["TestData"] = value;
}
}
}
}
请谁能告诉我我做错了什么或者我需要做些什么才能让我的自定义类正确地坚持用户设置。