我想将StringDictionary
保存到应用程序设置中,以便在应用程序启动时使用保存的值填充我的列表框lbc_lastCustomersVisited
。
这是我的应用程序设置(XML格式):
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="Wibe_EFI.Properties" GeneratedClassName="Settings">
<Profiles />
<Settings>
<Setting Name="ApplicationSkinName" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
<Setting Name="LastTimeWibeDataObtained" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
<Setting Name="LastVisitedCustomer" Type="System.Collections.Specialized.StringDictionary" Scope="User">
<Value Profile="(Default)" />
</Setting>
</Settings>
在我的表单中,我得到了一个StringDictionary
局部变量:
public partial class MainForm : XtraForm
{
private StringDictionary lastVisitedCustomers = new StringDictionary();
[...]
}
以下是我填充StringDictionary
局部变量的方法:
private void btn_selectCustomer_Click(object sender, EventArgs e)
{
DataRowView selectedRow = GetCustomersGridSelectedRow();
lastVisitedCustomers.Add(GetCustomerID(selectedRow), String.Format("{0} - {1}", GetCustomerName(selectedRow), GetCustomerCity(selectedRow)));
}
( StringDictionary
已成功填写)
在FormClosing
,我保存了我的设置:
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
Settings.Default["ApplicationSkinName"] = UserLookAndFeel.Default.SkinName;
Settings.Default.LastVisitedCustomer = lastVisitedCustomers;
Settings.Default.Save();
}
设置ApplicationSkinName
已成功保存,但lastVisitedCustomer StringDictionary
未成功保存。
因为当我在应用程序启动时加载设置时,Settings.Default.LastVisitedCustomer
为null
。
这是我如何加载我的应用程序皮肤设置(它的工作原理):
public MainForm()
{
InitializeComponent();
InitSkinGallery();
UserLookAndFeel.Default.SkinName = Settings.Default["ApplicationSkinName"].ToString();
}
但由于StringDictionnary
,我无法在此处加载NullReferenceException
。
所以我在这里加载它:
private void MainForm_Load(object sender, EventArgs e)
{
_mySqlCeEngine = new MySqlCeEngine(this);
ShowHomePanel();
LoadLastVisitedCustomers();
}
private void LoadLastVisitedCustomers()
{
if (Settings.Default.LastVisitedCustomer.Count > 0)
{
lastVisitedCustomers = Settings.Default.LastVisitedCustomer;
}
lbc_lastCustomersVisited.DataSource = new BindingSource(lastVisitedCustomers, null);
lbc_lastCustomersVisited.DisplayMember = "Value";
lbc_lastCustomersVisited.ValueMember = "Key";
}
但此时Settings.Default.LastVisitedCustomer
为空,我不明白为什么。我尝试了一些不使用局部变量和直接从Settings.Default.LastVisitedCustomer
读/写的东西,但我遇到了同样的问题。
谢谢,
地狱猫。
编辑:添加了完整的Settings.settings
文件(XML视图)
答案 0 :(得分:0)
如果您想尝试,我尝试通过创建新的formapplication来产生错误:
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
test.Text = (string)Settings.Default["lastCustomers"];
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Settings.Default["lastCustomers"] = test.Text;
Settings.Default.Save();
}
}
}
在此示例中,代码可以正常工作。只需创建一个新的表单 - 应用程序,并在其中添加名为test
的文本框。每次关闭程序并重新启动时,您写入文本框的字符串都将被保存并重新加载到test.Text
。
特别是在您的情况下,您的构造函数应该如下所示: (可能的解决方案)
public MainForm()
{
InitializeComponent();
InitSkinGallery();
UserLookAndFeel.Default.SkinName = Settings.Default["ApplicationSkinName"].ToString();
lastVisitedCustomers = (StringDictionary)Settings.Default["LastVisitedCustomer"];
}
然后以与加载设置相同的方式保存设置:
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
Settings.Default["ApplicationSkinName"] = UserLookAndFeel.Default.SkinName;
Settings.Default["LastVisitedCustomer"] = lastVisitedCustomers;
Settings.Default.Save();
}
这可以解决你的问题!
修改强> 哦,当然,你的加载功能看起来不同了:
private void MainForm_Load(object sender, EventArgs e)
{
_mySqlCeEngine = new MySqlCeEngine(this);
ShowHomePanel();
LoadLastVisitedCustomers();
}
private void LoadLastVisitedCustomers()
{
lbc_lastCustomersVisited.DataSource = new BindingSource(lastVisitedCustomers, null);
lbc_lastCustomersVisited.DisplayMember = "Value";
lbc_lastCustomersVisited.ValueMember = "Key";
}
答案 1 :(得分:0)
我找不到在应用设置中存储Dictionary
的任何解决方案,因此我决定处理string
并将其拆分:
public partial class MainForm : XtraForm
{
private Dictionary<string, string> lastVisitedCustomers;
public MainForm()
{
InitializeComponent();
InitSkinGallery();
UserLookAndFeel.Default.SkinName = Settings.Default["ApplicationSkinName"].ToString();
lastVisitedCustomers = StringToDictionary(Settings.Default["LastVisitedCustomer"].ToString());
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
Settings.Default["ApplicationSkinName"] = UserLookAndFeel.Default.SkinName;
Settings.Default["LastVisitedCustomer"] = DictionaryToString(lastVisitedCustomers);
Settings.Default.Save();
}
#region Dictionary management
private string DictionaryToString(Dictionary<string, string> dic)
{
if (dic.Count == 0)
{
return string.Empty;
}
else
{
string dicString = string.Empty;
int i = 0;
foreach (KeyValuePair<string, string> entry in dic)
{
if (i == 0)
{
dicString = String.Format("{0} ## {1}", entry.Key, entry.Value);
i++;
}
else
{
dicString += String.Format(" -- {0} ## {1}", entry.Key, entry.Value);
}
}
return dicString;
}
}
private Dictionary<string, string> StringToDictionary(string str)
{
Dictionary<string, string> dic = new Dictionary<string,string>();
if (String.IsNullOrEmpty(str))
{
return dic;
}
else
{
string[] entries = Regex.Split(str, " -- ");
foreach (string entry in entries)
{
string[] kvp = Regex.Split(entry, " ## ");
dic.Add(kvp[0], kvp[1]);
}
return dic;
}
}
}
#endregion
现在,它有效。
我希望在我的情况下它会帮助任何人。
注意:您可能必须修改我的字典方法才能处理异常的每个案例。但我认为现在这没关系。
对不起任何英语错误