应用程序设置未保存

Application setting is not saved

我想将 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.LastVisitedCustomernull.


以下是我如何加载有关应用程序皮肤的设置(有效):

public MainForm()
{
        InitializeComponent();
        InitSkinGallery();
        UserLookAndFeel.Default.SkinName = Settings.Default["ApplicationSkinName"].ToString();
}

但是由于 NullReferenceException,我无法在此处加载我的 StringDictionnary
所以我在这里加载它:

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 read/write,但我遇到了同样的问题。

谢谢,

地狱猫。

编辑 : 添加了完整的 Settings.settings 文件(XML 视图)

如果你想尝试,我试图通过创建一个新的 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();
    }
  }
}

在此示例中,代码有效。只需创建一个新的 Form-Application 并在其中添加一个名为 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";
}

我找不到在应用程序设置中存储 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

目前,它有效。
我希望它能帮助我处境中的任何人。

注意:您可能必须修改我的字典方法才能处理所有异常情况。但我认为现在可以了。

对于任何英文错误,我们深表歉意