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

时间:2015-03-07 19:02:29

标签: c# wpf mvvm settings.settings

我正在重写我当前使用MVVM的程序,而且我非常喜欢MVVM - 我觉得它非常不知所措,所以如果这很明显就道歉......发生的事情是好事,不是坏事......但我想明白为什么会这样。 :)

我现在只有一些非常基本的东西。我有我的viewmodel,我已经开始组建一个模型来查看存储在properties.settings.default中的值......

以下是我的viewmodel中的方法:

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是真的)。我运行程序,看到消息框并关闭它。然后我再次运行它而不重建,在重建并获得新的版本号之前我没有看到任何消息框。

您能解释一下为什么我不想添加额外的代码行来将假值存储到用户设置中吗?为什么它被更新为假?我不能为我的生活找出原因!

同样,这表达了我想要的方式,而不是我期望的方式。

1 个答案:

答案 0 :(得分:1)

如果没有完整的代码示例,很难确切知道发生了什么,以及确保完全理解您甚至要问的内容。但是,根据您的描述:

  

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

强烈暗示正在执行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值存储到usersetting中?为什么它被更新为假?

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


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

那,没有a good, minimal, complete code example,任何人都无法回答。没有一个,就无法解释为什么你不需要任何额外的代码来做到这一点。

根据您的描述, 可以表示该属性显然已设置某处。此外,根据您的描述,似乎很可能两件事之一是真的:

  1. 您正在Properties.Settings.Default.NeedSettingsUpgrade方法中将false属性设置为Properties.Settings.Default.Upgrade()
  2. Properties.Settings.Default.NeedSettingsUpgrade属性绑定到UserSetting.NeedSettingsUpdate属性的某处并按此方式设置。
  3. 由于#2选项仅在您设置Properties.Settings.Default.Save()属性后继续并再次调用UserSetting.NeedSettingsUpdate (因为setter在其之前调用Save()之后)才有效提出通知事件),我的钱在#1上。这意味着您已在某处覆盖了该方法,并将NeedSettingsUpgrade设置属性设置为false

    但是没有看到那些代码,就不可能确切知道。