为什么在这种情况下保存我的设置



我正在重写我当前的程序以使用 MVVM,而且我是 MVVM 的新手 - 我对它感到非常不知所措,所以如果这是显而易见的,我深表歉意......正在发生的事情是一件好事,而不是一件坏事......但我想了解为什么会发生这种情况。:)

我现在只有一些非常基本的东西。我有我的视图模型,我已经开始整理一个模型来查看存储在属性中的值。

这是我的观点模型中的方法:

private static void UpgradeApplicationSettingsIfNecessary()
{
    // Application settings are stored in a subfolder named after the full #.#.#.# version number of the program. This means that when a new version of the program is installed, the old settings will not be available.
    // Fortunately, there's a method called Upgrade() that you can call to upgrade the settings from the old to the new folder.
    // We control when to do this by having a boolean setting called 'NeedSettingsUpgrade' which is defaulted to true. Therefore, the first time a new version of this program is run, it will have its default value of true.
    // This will cause the code below to call "Upgrade()" which copies the old settings to the new.
    // It then sets "NeedSettingsUpgrade" to false so the upgrade won't be done the next time.
    UserSetting Setting = new UserSetting();
    if (Setting.NeedSettingsUpgrade)
    {
        Properties.Settings.Default.Upgrade();
        System.Windows.Forms.MessageBox.Show("Settings Upgraded");
        Setting.NeedSettingsUpgrade = false;
    }
}

(是的,它是从某个地方借来的代码)。本质上,我有一个存储在settings.settings中的设置,默认为true。如果是真的,我会升级用户设置,以便它们在版本之间保持不变。(我已经有我的版本随着程序构建时的递增而增加)。

这是我的模型:

class UserSetting : INotifyPropertyChanged
{
    private bool needSettingsUpgrade;
    //Initiates the instance.
    public UserSetting()
    {
        NeedSettingsUpgrade = Properties.Settings.Default.NeedSettingsUpgrade;
    }
    public bool NeedSettingsUpgrade
    {
        get
        {
            return needSettingsUpgrade;
        }
        set
        {
            needSettingsUpgrade = value;
            Properties.Settings.Default.Save();
            OnPropertyChanged("NeedSettingsUpgrade");
        }
    }

    #region INotifyPropertyChanged Members
    public event PropertyChangedEventHandler PropertyChanged;
    private void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    #endregion
}

所以。。。我以为我将不得不在某个时候写:

Properties.Settings.Default.NeedSettingsUpgrade = false;

然而,事实并非如此。我在消息框中添加到视图模型中,以便我可以查看设置是否正在保存,并且确实如此。我构建了程序(给它一个新的版本号,因此将settings.settings重置回默认值,所以NeedSettingsUpgrade是真的)。我运行了程序,看到了消息框并关闭了它。然后我在没有重建的情况下再次运行它,并且在我重建并获得新版本号之前看不到任何消息框。

你能向我解释为什么我不添加额外的代码行来将 false 值存储到用户设置中吗?为什么它被更新为 false?我一辈子都想不通为什么!

同样,这是按照我想要的方式行事,只是不是我期望的那样。

如果没有完整的代码示例,就很难确定发生了什么,也很难确保完全理解你在问什么。但是,根据您的描述:

我运行了程序,看到了消息框并关闭了它。然后我在没有重建的情况下再次运行它,并且在我重建并获得新版本号之前看不到任何消息框。

这强烈表明正在执行UpgradeApplicationSettingsIfNecessary()方法:

private static void UpgradeApplicationSettingsIfNecessary()
{
    // Application settings are stored in a subfolder named after the full #.#.#.# version number of the program. This means that when a new version of the program is installed, the old settings will not be available.
    // Fortunately, there's a method called Upgrade() that you can call to upgrade the settings from the old to the new folder.
    // We control when to do this by having a boolean setting called 'NeedSettingsUpgrade' which is defaulted to true. Therefore, the first time a new version of this program is run, it will have its default value of true.
    // This will cause the code below to call "Upgrade()" which copies the old settings to the new.
    // It then sets "NeedSettingsUpgrade" to false so the upgrade won't be done the next time.
    UserSetting Setting = new UserSetting();
    if (Setting.NeedSettingsUpgrade)
    {
        Properties.Settings.Default.Upgrade();
        System.Windows.Forms.MessageBox.Show("Settings Upgraded");
        Setting.NeedSettingsUpgrade = false;
    }
}

该方法不仅将NeedSettingsUpgrade属性设置为 false ,您甚至还有一个详细描述这一点的注释。

然后在该属性的 setter 中:

set
{
    needSettingsUpgrade = value;
    Properties.Settings.Default.Save();
    OnPropertyChanged("NeedSettingsUpgrade");
}

你打电话给Properties.Settings.Default.Save().

你能向我解释为什么我不添加额外的代码行来将 false 值存储到用户设置中吗?为什么它被更新为 false?

从上面可以看出,为什么你不需要任何额外的代码"将假值存储到用户设置中"应该很明显。UserSetting.NeedSettingsUpdate属性由您发布的代码显式设置。


现在,您可能实际上想知道为什么在调用 Save() 方法之前不需要实际设置 Properties.Settings.Default.NeedSettingsUpgrade 属性。

如果没有一个好的、最小的完整的代码示例,没有人能回答这个问题。如果没有一个,就无法解释为什么你不需要任何其他代码来做到这一点。

根据您的描述,可以说该属性显然是在某个地方设置的。此外,根据您的描述,似乎最有可能有两件事之一是正确的:

  1. Properties.Settings.Default.Upgrade() 方法中将 Properties.Settings.Default.NeedSettingsUpgrade 属性设置为false
  2. Properties.Settings.Default.NeedSettingsUpgrade属性绑定到UserSetting.NeedSettingsUpdate属性的某个位置,并以这种方式进行设置。

由于 #2 选项仅在您在设置 UserSetting.NeedSettingsUpdate 属性后继续并再次调用 Properties.Settings.Default.Save() 时才有效(因为 setter 在引发通知事件之前调用Save()),因此我的钱在 #1 上。这意味着您已在某处重写了该方法,并将 NeedSettingsUpgrade 设置属性设置为 false

但是,如果没有看到该代码,就无法确定。

最新更新