为什么在这种情况下会保存我的设置?
Why are my settings saved in this situation?
我正在重写我当前的程序以使用 MVVM,我是 MVVM 的新手 - 我对它感到不知所措,所以如果这很明显,我深表歉意......发生的事情是一件好事事情,不是一件坏事......但我想了解为什么会这样。 :)
我现在只有一些非常基本的东西。我有我的视图模型,我已经开始构建一个模型来查看存储在 properties.settings.default...
中的值
这是我的视图模型中的一个方法:
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?我这辈子都想不通为什么!
再次强调,这是我想要的方式,而不是我期望的方式。
如果没有完整的代码示例,就很难确定发生了什么,也很难确保完全理解您的要求。但是,根据您的描述:
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?
从上面可以明显看出为什么您不需要任何额外的代码"to store the false value into the usersetting"。 UserSetting.NeedSettingsUpdate
属性 由您发布的代码明确设置。
现在,您可能 实际上 想知道为什么在调用 Save()
方法之前不需要实际设置 Properties.Settings.Default.NeedSettingsUpgrade
属性。
那个,没有a good, minimal, complete code example谁也回答不了。没有一个,就无法解释为什么您不需要任何额外的代码来做到这一点。
根据您的描述,可以 说的是,属性 显然在某处 设置了。此外,根据您的描述,以下两件事中的一件最有可能是正确的:
- 您正在
Properties.Settings.Default.Upgrade()
方法中将 Properties.Settings.Default.NeedSettingsUpgrade
属性 设置为 false
。
Properties.Settings.Default.NeedSettingsUpgrade
属性 绑定到 UserSetting.NeedSettingsUpdate
属性 的某处并以这种方式设置。
因为只有在设置 UserSetting.NeedSettingsUpdate
属性 之后继续并再次调用 Properties.Settings.Default.Save()
,#2 选项才有效(因为setter 在引发通知事件之前调用 Save()
),我的钱在 #1 上。这意味着您已经在某处覆盖了该方法并将 NeedSettingsUpgrade
设置 属性 设置为 false
.
但如果没有看到该代码,就无法确定。
我正在重写我当前的程序以使用 MVVM,我是 MVVM 的新手 - 我对它感到不知所措,所以如果这很明显,我深表歉意......发生的事情是一件好事事情,不是一件坏事......但我想了解为什么会这样。 :)
我现在只有一些非常基本的东西。我有我的视图模型,我已经开始构建一个模型来查看存储在 properties.settings.default...
中的值这是我的视图模型中的一个方法:
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?我这辈子都想不通为什么!
再次强调,这是我想要的方式,而不是我期望的方式。
如果没有完整的代码示例,就很难确定发生了什么,也很难确保完全理解您的要求。但是,根据您的描述:
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?
从上面可以明显看出为什么您不需要任何额外的代码"to store the false value into the usersetting"。 UserSetting.NeedSettingsUpdate
属性 由您发布的代码明确设置。
现在,您可能 实际上 想知道为什么在调用 Save()
方法之前不需要实际设置 Properties.Settings.Default.NeedSettingsUpgrade
属性。
那个,没有a good, minimal, complete code example谁也回答不了。没有一个,就无法解释为什么您不需要任何额外的代码来做到这一点。
根据您的描述,可以 说的是,属性 显然在某处 设置了。此外,根据您的描述,以下两件事中的一件最有可能是正确的:
- 您正在
Properties.Settings.Default.Upgrade()
方法中将Properties.Settings.Default.NeedSettingsUpgrade
属性 设置为false
。 Properties.Settings.Default.NeedSettingsUpgrade
属性 绑定到UserSetting.NeedSettingsUpdate
属性 的某处并以这种方式设置。
因为只有在设置 UserSetting.NeedSettingsUpdate
属性 之后继续并再次调用 Properties.Settings.Default.Save()
,#2 选项才有效(因为setter 在引发通知事件之前调用 Save()
),我的钱在 #1 上。这意味着您已经在某处覆盖了该方法并将 NeedSettingsUpgrade
设置 属性 设置为 false
.
但如果没有看到该代码,就无法确定。