gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:10:36 27 4
gpt4 key购买 nike

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

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

这是我的 View 模型中的一个方法:

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;

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

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

再次强调,这是我想要的,而不是我期望的。

最佳答案

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

I ran the program, saw the messagebox and closed it. Then I ran it again without rebuilding, and I see no messagebox until I rebuild and get a new version number.

这强烈表明正在执行 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()

Can you explain to me why I don't ned to add that extra line of code to store the false value into the usersetting? Why is it being updated to false?

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


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

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

根据您的描述,可以说的是,该属性显然是在某处设置的。此外,根据您的描述,以下两件事中的一件最有可能是正确的:

  1. 您在 Properties.Settings.Default.Upgrade() 方法中将 Properties.Settings.Default.NeedSettingsUpgrade 属性设置为 false .
  2. Properties.Settings.Default.NeedSettingsUpgrade 属性在某处绑定(bind)到 UserSetting.NeedSettingsUpdate 属性并以这种方式设置。

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

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

关于c# - 为什么在这种情况下会保存我的设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28918801/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com