我有一個自定義類及其集合,我想用它在我的Settings.default中創建條目。我設法使用名稱空間上的瀏覽(...)按鈕設置設計器中的類型,並在給定的會話中使用。我可以存儲並從我的類(或者這些類的對象)中獲取值。Visual C#自定義類的類型設置不會保存
但設置不保存。 我已經使用this method在我的設置中實現我的自定義課程,並且正在嘗試諸如this one之類的提示來保存它們 - 但這裏沒有運氣。
我將如何保存自定義對象類? 最終的類中的類型是微不足道的,只不過是字符串,整數和布爾值。
更新: 它不是一個集合序列化的問題,包含一個字符串一個簡單的類也將不存在。我將我的代碼更改爲更簡單的示例。
using System;
using System.Windows.Forms;
using System.Configuration;
namespace tester
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
if(Properties.Settings.Default.MySetting== null)
{
MessageBox.Show("MySetting is null");
Properties.Settings.Default.MySetting = new saveme(); // this saveme will be accessible just fine for the duration of the session, but it won't persist.
}
textBox1.Text = Properties.Settings.Default.MySetting.somestring; // will always be "initial value" as per initialization of saveme()
}
private void button1_Click(object sender, EventArgs e)
{
Properties.Settings.Default.MySetting.somestring = textBox1.Text;
Properties.Settings.Default.Save();
}
}
[Serializable()]
public class saveme : ApplicationSettingsBase // class containing data
{
private string Somestring = "initial value";
[UserScopedSetting()]
[DefaultSettingValue("default value")]
[SettingsSerializeAs(SettingsSerializeAs.Binary)]
public string somestring
{
get { return Somestring; }
set { Somestring = value; }
}
}
}
它將總是最後MySetting是空
SettingsSerializeAs屬性必須指定Binary作爲我們的代碼。 - 來自你基於的教程。繼續嘗試吧。 –
@Ephraim:你的意思是'[SettingsSerializeAs(SettingsSerializeAs.Binary)]'?我剛開始時有一個,但沒有成功:/ –
@Ephraim我回到了Binary,結果相同 - 你有什麼想法嗎? –