为什么 ApplicationSettingsBase.Reset() 会清空 PropertyValues?

发布于 2024-08-09 07:28:49 字数 951 浏览 6 评论 0原文

我认为 Reset() 方法会再次使用默认值重新填充设置,但似乎并非如此。如何使用默认值重新加载它们?

  private void buttonLoadDefaultSettings_Click(object sender, EventArgs e)
  {
   FooSettings.Default.Reset();

   // Data grid will show an empty grid after call to reset.
   DataGridFoo.Rows.Clear();
   foreach (SettingsPropertyValue spv in FooSettings.Default.PropertyValues)
   {
    DataGridFoo.Rows.Add(spv.Name, spv.PropertyValue);
   }
  }

更新

private void buttonLoadDefaultSettings_Click(object sender, EventArgs e)
  {
   foreach (SettingsProperty sp in FooSettings.Default.Properties)
   {
    FooSettings.Default[sp.Name.ToString()] = sp.DefaultValue;
   }

   DataGridFoo.Rows.Clear();
   foreach (SettingsPropertyValue spv in FooSettings.Default.PropertyValues)
   {
    DataGridFoo.Rows.Add(spv.Name, spv.PropertyValue);
   }
  }

删除了对 Reset() 的调用,并将属性值手动设置为默认存储的值。我仍然很想知道这是否是应该使用的方式,还是我错过了什么?

I thought the Reset() method repopulates the Settings with default values again, but it seems not to. How can I reload them with the default values?

  private void buttonLoadDefaultSettings_Click(object sender, EventArgs e)
  {
   FooSettings.Default.Reset();

   // Data grid will show an empty grid after call to reset.
   DataGridFoo.Rows.Clear();
   foreach (SettingsPropertyValue spv in FooSettings.Default.PropertyValues)
   {
    DataGridFoo.Rows.Add(spv.Name, spv.PropertyValue);
   }
  }

Update

private void buttonLoadDefaultSettings_Click(object sender, EventArgs e)
  {
   foreach (SettingsProperty sp in FooSettings.Default.Properties)
   {
    FooSettings.Default[sp.Name.ToString()] = sp.DefaultValue;
   }

   DataGridFoo.Rows.Clear();
   foreach (SettingsPropertyValue spv in FooSettings.Default.PropertyValues)
   {
    DataGridFoo.Rows.Add(spv.Name, spv.PropertyValue);
   }
  }

Removed the call to Reset() and set the property values manually to the default stored ones. I'm still eager to hear if this is the way it's supposed to be used or am I missing something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离鸿 2024-08-16 07:28:49

我遇到这个线程是因为我遇到了同样的问题。我想我应该向未来可能来这里的旅行者报告我的发现。我不能保证这是 100% 准确或完整的,因为我已经摆弄了它一个小时,这已经足够摆弄一天了,尽管我觉得还有更多东西需要了解。但至少他们会在这里提供一些提示。 :)

虽然 Reset() 的文档似乎表明 user.config 文件中保存的设置被 app.config 文件中的默认值覆盖,但事实似乎并非如此。它只是从 user.config 文件中删除设置,使用上面的示例,会导致 FooSettings.Default.PropertyValues 的计数为 0,因为使用 Reset()< 后不存在任何设置。 /代码>。但是有一些方法可以处理这个结果,而无需像OP那样重新填充设置。一种方法是显式检索单个设置值,如下所示:

// This always returns the value for TestSetting, first checking if an
// appropriate value exists in a user.config file, and if not, it uses 
// the default value in the app.config file.
FormsApp.Properties.Settings.Default.TestSetting;

其他方法涉及使用 SettingsPropertyValueCollection 和/或 SettingsPropertyCollection

// Each SettingsProperty in props has a corresponding DefaultValue property
// which returns (surprise!) the default value from the app.config file.
SettingsPropertyCollection props = FormsApp.Properties.Settings.Default.Properties;

// Each SettingsPropertyValue in propVals has a corresponding PropertyValue
// property which returns the value in the user.config file, if one exists.
SettingsPropertyValueCollection propVals = FormsApp.Properties.Settings.Default.PropertyValues;

因此,回到最初的问题,您可以做的是这:

private void buttonLoadDefaultSettings_Click(object sender, EventArgs e)
{
    FooSettings.Default.Reset();
    DataGridFoo.Rows.Clear();

    // Use the default values since we know that the user settings 
    // were just reset.
    foreach (SettingsProperty sp in FooSettings.Default.Properties)
    {
        DataGridFoo.Rows.Add(sp.Name, sp.DefaultValue);
    }
}

I came across this thread because I encountered the same issue. I figured I'd report my findings for any future travelers who might come this way. I can't promise this is 100% accurate or complete 'cause I've been fiddling with it for an hour that's enough fiddling for one day even though I feel like there's still more to know. But at least they'll be some tips here. :)

Although the documentation for Reset() seems to indicate that saved settings are overwritten in the user.config file with the default values from the app.config file, this does not appear to be the case. It simply erases the settings from the user.config file, which, using the example above, results in FooSettings.Default.PropertyValues having a count of 0 because none exists after using Reset(). But there are ways to work with this result that don't necessitate repopulating the settings as the OP did. One way is to explicitly retrieve individual settings values like this:

// This always returns the value for TestSetting, first checking if an
// appropriate value exists in a user.config file, and if not, it uses 
// the default value in the app.config file.
FormsApp.Properties.Settings.Default.TestSetting;

Other ways involve using SettingsPropertyValueCollection and/or SettingsPropertyCollection:

// Each SettingsProperty in props has a corresponding DefaultValue property
// which returns (surprise!) the default value from the app.config file.
SettingsPropertyCollection props = FormsApp.Properties.Settings.Default.Properties;

// Each SettingsPropertyValue in propVals has a corresponding PropertyValue
// property which returns the value in the user.config file, if one exists.
SettingsPropertyValueCollection propVals = FormsApp.Properties.Settings.Default.PropertyValues;

So, returning to the original question, what you could have done is this:

private void buttonLoadDefaultSettings_Click(object sender, EventArgs e)
{
    FooSettings.Default.Reset();
    DataGridFoo.Rows.Clear();

    // Use the default values since we know that the user settings 
    // were just reset.
    foreach (SettingsProperty sp in FooSettings.Default.Properties)
    {
        DataGridFoo.Rows.Add(sp.Name, sp.DefaultValue);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文