简体   繁体   English

在几种形式之间处理应用程序设置的最佳实践是什么

[英]What is the best practice to handle application's settings between several forms

What is the best practice to handle application's settings contains in the external INI file between several forms? 处理几种形式之间的外部INI文件中包含的应用程序设置的最佳实践是什么?

Suppose that I have two forms -- MainForm and SettingsForm . 假设我有两种形式MainFormSettingsForm User can open the SettingsForm from the MainForm . 用户可以从MainForm打开SettingsForm Now I have smth like this: 现在我有这样的东西:

  • On Load event of the SettingsForm I read application's settings from the file and change the UI appropriately SettingsForm Load事件中,我从文件中读取了应用程序的设置,并适当地更改了UI
  • On "Save" button press I write new options' values to the file 在“保存”按钮上,按“我将新选项的值写入文件”
  • On the "SettingsFormClosed" event of the MainForm I read the settings from the file again MainForm的“ SettingsFormClosed”事件上,我再次从文件中读取设置

I think that the last thing is the most terrible in this case. 我认为在这种情况下,最后一件事最可怕。 Maybe I should use smth like SettingsSingleton or smth like this? 也许我应该使用像SettingsSingleton这样的smth或这样的smth? What is the best practice to do it in C# and Windows Forms? 在C#和Windows窗体中执行此操作的最佳实践是什么?

Thanks in advance. 提前致谢。

You should use Observer pattern . 您应该使用Observer模式 You will need a class which represents settings file, this file is Observable . 您将需要一个代表设置文件的类,该文件是Observable Your SettingsForm and MainForm are Observers . 您的SettingsFormMainFormObservers And SettingsForm can modify the Observable . 并且SettingsForm可以修改Observable

I like to do it like this, make a Setting class for storing the values, then create a service class that is responsible for saving and supplying the settings to other objects. 我喜欢这样,创建一个用于存储值的Setting类,然后创建一个负责保存设置并将其提供给其他对象的服务类。 Service is implemented as a singleton and implements INotifyPropertyChanged. 服务以单例形式实现,并实现INotifyPropertyChanged。 Then in your forms you just have to subscribe to ProperyChanging event from the service. 然后,您只需在表格中订阅服务中的ProperyChanging事件即可。

Example service code: 服务代码示例:

 public sealed class SettingsService : INotifyPropertyChanged
    {
        private static Lazy<SettingsService> instance = new Lazy<SettingsService>(() => new SettingsService());

        public static SettingsService Instance
        {
            get { return instance.Value; }
        }

        private Setting settings;
        public Setting Settings
        {
           get
           {
              var clone = settings.Clone();
              return clone;
           }
        }

        private SettingsService()
        {
           //load your settings
        }            

        public void SaveChanges(Setting settings)
        {
           //Do whatever you do to save the changes, and raise the event the   settings changed
           this.settings = settings;
           NotifyPropertyChanged();
        }


        private void NotifyPropertyChanged()
        {
            if (PropertyChanged != null)
               PropertyChanged(this, new PropertyChangedEventArgs("Settings"));
        }
        public event PropertyChangedEventHandler PropertyChanged;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM