如何以编程方式保存用户设置?
How to save user settings programmatically?
我有一个按钮可以打开 windows 颜色托盘,然后将 select 颜色分配给某个虚拟工作室中的 selected 元素。元素首先由用户点击鼠标 select 编辑,并根据元素 ID 分配颜色。因此,每次单击按钮时,相同或不同元素的颜色都会改变。元素 ID 是从一个委托中获取的,如果鼠标单击某个元素,该委托就会触发。颜色设置按钮的代码是这样的:
private void Btn_Choose_Color_Click(object sender, RoutedEventArgs e)
{
uint id_selected = (uint)selected_element; //get id of selected element from clickintocallback
//open windows color dialog
System.Windows.Forms.ColorDialog my_dialog = new System.Windows.Forms.ColorDialog();
my_dialog.ShowDialog();
//get the color from windows dialog
int red = my_dialog.Color.R;
int green = my_dialog.Color.G;
int blue = my_dialog.Color.B;
//create cinector color object and pass rgb values from windows dialog
ByteRGBColor desired_color = new ByteRGBColor((byte)red, (byte)green, (byte)blue); //assign color statically
for (int i = 0; i < all_color_elements_in_loaded_studio.Count; i++)
{
uint id_current = all_color_elements_in_loaded_studio.ElementAt(0).colorElementID; //get id of current element in a loop
if(id_current == id_selected) //compare selected and current element
{
//all_color_elements_in_loaded_studio.ElementAt(i).colorElementColor = test_color; //set the test color
instance.SetStudioColorElement(id_current, desired_color); //assign the color to the element
break;
}
}
//after we choose a color
Btn_Pick_Element_Clicked = false;
Btn_Choose_Color.IsEnabled = false;
}
现在,我的问题是如何在分配给用户设置后保存元素 ID 及其颜色?我知道我可以转到 Properties->Settings 并在那里手动定义用户设置,但在这里必须以某种方式以编程方式完成。还有,如何加载这些设置?
设置
Properties.Settings.Default.myColor = Color.AliceBlue;
获取
this.BackColor = Properties.Settings.Default.myColor;
保存
如果您想在应用程序会话之间保留对用户设置的更改,请调用 Save 方法,如以下代码所示:
Properties.Settings.Default.Save();
查看 this article,尤其是 在 运行 时间保存用户设置 部分。
要得到这个答案不只是 link。以下是转载的相关部分:
在 运行 时保存用户设置
应用程序范围设置是只读的,只能在设计时或通过在应用程序会话之间更改 .exe.config 文件来更改。但是,用户范围设置可以在 运行 时写入,就像您更改任何 属性 值一样。新值在应用程序会话期间持续存在。您可以通过调用 Settings.Save
方法在应用程序会话之间保留对用户设置的更改。这些设置保存在 User.config 文件中。
在 运行 时间写入和保存用户设置
访问用户设置并为其分配一个新值,如下例所示:
Properties.Settings.Default.myColor = Color.AliceBlue;
如果您想在应用程序会话之间保留对用户设置的更改,请调用 Save 方法,如以下代码所示:
Properties.Settings.Default.Save();
我有一个按钮可以打开 windows 颜色托盘,然后将 select 颜色分配给某个虚拟工作室中的 selected 元素。元素首先由用户点击鼠标 select 编辑,并根据元素 ID 分配颜色。因此,每次单击按钮时,相同或不同元素的颜色都会改变。元素 ID 是从一个委托中获取的,如果鼠标单击某个元素,该委托就会触发。颜色设置按钮的代码是这样的:
private void Btn_Choose_Color_Click(object sender, RoutedEventArgs e)
{
uint id_selected = (uint)selected_element; //get id of selected element from clickintocallback
//open windows color dialog
System.Windows.Forms.ColorDialog my_dialog = new System.Windows.Forms.ColorDialog();
my_dialog.ShowDialog();
//get the color from windows dialog
int red = my_dialog.Color.R;
int green = my_dialog.Color.G;
int blue = my_dialog.Color.B;
//create cinector color object and pass rgb values from windows dialog
ByteRGBColor desired_color = new ByteRGBColor((byte)red, (byte)green, (byte)blue); //assign color statically
for (int i = 0; i < all_color_elements_in_loaded_studio.Count; i++)
{
uint id_current = all_color_elements_in_loaded_studio.ElementAt(0).colorElementID; //get id of current element in a loop
if(id_current == id_selected) //compare selected and current element
{
//all_color_elements_in_loaded_studio.ElementAt(i).colorElementColor = test_color; //set the test color
instance.SetStudioColorElement(id_current, desired_color); //assign the color to the element
break;
}
}
//after we choose a color
Btn_Pick_Element_Clicked = false;
Btn_Choose_Color.IsEnabled = false;
}
现在,我的问题是如何在分配给用户设置后保存元素 ID 及其颜色?我知道我可以转到 Properties->Settings 并在那里手动定义用户设置,但在这里必须以某种方式以编程方式完成。还有,如何加载这些设置?
设置
Properties.Settings.Default.myColor = Color.AliceBlue;
获取
this.BackColor = Properties.Settings.Default.myColor;
保存
如果您想在应用程序会话之间保留对用户设置的更改,请调用 Save 方法,如以下代码所示:
Properties.Settings.Default.Save();
查看 this article,尤其是 在 运行 时间保存用户设置 部分。
要得到这个答案不只是 link。以下是转载的相关部分:
在 运行 时保存用户设置
应用程序范围设置是只读的,只能在设计时或通过在应用程序会话之间更改 .exe.config 文件来更改。但是,用户范围设置可以在 运行 时写入,就像您更改任何 属性 值一样。新值在应用程序会话期间持续存在。您可以通过调用 Settings.Save
方法在应用程序会话之间保留对用户设置的更改。这些设置保存在 User.config 文件中。
在 运行 时间写入和保存用户设置
访问用户设置并为其分配一个新值,如下例所示:
Properties.Settings.Default.myColor = Color.AliceBlue;
如果您想在应用程序会话之间保留对用户设置的更改,请调用 Save 方法,如以下代码所示:
Properties.Settings.Default.Save();