【问题标题】:using Properties.Settings for application settings使用 Properties.Settings 进行应用程序设置
【发布时间】:2012-01-26 17:07:24
【问题描述】:

我使用 Visual Studio 提供的内置设置来存储简单的应用程序设置。到目前为止,我已经通过使用约定在我的应用程序中访问了它:

Properties.Settings.Default.MySetting

然后调用Save之类的方法,使用:

Properties.Settings.Default.Save()

但是,最近有人告诉我,通过创建这样的成员变量来访问属性更正确:

private Properties.Settings settings = new Properties.Settings()

然后使用成员settings 访问属性和方法,例如:

settings.MySetting
settings.Save()

我隐约记得他们通过描述设置在用户区域中存储方式的差异来证明这一点。

任何人都可以确认或提供有关差异的更多详细信息吗?非常感谢。

【问题讨论】:

    标签: c# visual-studio-2010 settings


    【解决方案1】:

    Settings.Default初始化如下:

    private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
    

    所以和手动创建Settings的实例几乎一样,只不过@9​​87654324@提供的是同步实例。我看不出有什么好的理由手动创建Settings 的实例...

    【讨论】:

      【解决方案2】:

      这浪费了我很多时间。

      [MyAppNameSpace].Properties.Settings.Default.Save();
      

      不确定何时可以像上面那样删除命名空间,但在 app.xaml.cs 代码中的 wpf 中,我需要指定命名空间以使其编译。

      【讨论】:

      • 谢谢谢谢!这为我节省了很多时间!有趣的是,我是 2.5 年后第一个得到帮助的人!这适用于 app.xaml.cs
      • 也救了我!它在没有命名空间的情况下工作,直到我将它移到 App.xaml.cs,然后得到“字段初始化程序无法引用非静态字段”。看起来在 Application 类中有一个 Properties 成员,它是模棱两可的。
      猜你喜欢
      • 2016-03-20
      • 1970-01-01
      • 2010-12-20
      • 2012-11-26
      • 2019-11-01
      • 1970-01-01
      • 2013-03-22
      • 1970-01-01
      • 2011-06-28
      相关资源
      最近更新 更多