Я хочу сохранить 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>
In my form, I got a StringDictionary
local variable :
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
, но у меня возникла та же проблема.
Спасибо,
Адский кот.
EDIT: добавлен полный файл Settings.settings
(представление XML).
Settings
. - person Leon Bohmann   schedule 07.08.2015this.LastVisitedCustomers = (StringDictionary)Settings.Default["LastVisitedCustomer"]
в конструкторе MainForm(). - person Leon Bohmann   schedule 07.08.2015