0

我想将 a 保存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();
}

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 视图)

4

2 回答 2

0

如果您想尝试,我尝试通过创建一个新的表单应用程序来产生您得到的错误:

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";
}
于 2015-08-07T18:33:53.080 回答
0

我找不到将 a 存储Dictionary在应用程序设置中的任何解决方案,因此我决定处理 astring并将其拆分:

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

目前,它有效。
我希望它会帮助我遇到这种情况的任何人。

注意:您可能必须修改我的字典方法才能处理每个例外情况。但我认为现在这没问题。

抱歉有任何英文错误

于 2015-08-10T11:35:24.507 回答