Windows Forms 2.0 Programming (Microsoft .NET Development Series)

Every settings file added to a VS05 project is used to generate an additional strongly typed settings class.[11] The class has the same name as the settings file, minus the extension. The class is stored in a .Designer.cs file, also with the same name as the settings file, which is then associated with the settings file, as shown in Figure 15.14.

[11] The strongly typed settings class is generated by the SettingsSingleFileGenerator custom tool, which is associated with settings files by default.

Figure 15.14. The Designer-Generated Settings Class File

Here are the relevant elements of the generated class implementation created for the VS05 default settings file, which is generated to Settings.Designer.cs:

// Settings.Designer.cs using System.Configuration; using System.Runtime.CompilerServices; ... namespace ApplicationSettingsSample.Properties { [global::CompilerGenerated()] internal sealed partial class Settings : global::ApplicationSettingsBase { static Settings defaultInstance = new Settings(); public static Settings Default { get { return defaultInstance; } } ... [global::UserScopedSetting()] [global::DefaultSettingValue("0")] public int HighScore { get { return ((int)(this["HighScore"])); } set { this["HighScore"] = value; } } ... [global::ApplicationScopedSetting()] [global::DefaultSettingValue("c:\\...\\microsoft.net")] public string AssemblyPaths { get { return ((string)(this["AssemblyPaths"])); } } } }

The Settings class derives from ApplicationSettingsBase, which is the workhorse of the settings system. ApplicationSettingsBase exposes the vast majority of the settings functionality you'll need through a relatively simple interface. ApplicationSettingsBase is the functional tip of the settings iceberg; it not only encompasses the basic read and write operations but also extends them with a variety of higher-level operations to handle a variety of settings scenarios. Additionally, ApplicationSettingsBase takes care of safely ensuring that the right settings are written to the right .config files.

Additionally, Settings derives from ApplicationSettingsBase and extends it by exposing settings as strongly typed properties. Our example shows both the HighScore and the AssemblyPaths settings as having been generated as strongly typed properties of the same name, each adorned with two attributes: XxxScopedSetting and DefaultSettingValue.

Because the HighScore setting has a user scope, its property implementation includes both get and set accessors and is adorned with the UserScopedSetting attribute. On the other hand, the AssemblyPaths setting has an application scope, and this causes its property implementation to include only a get accessor; AssemblyPaths is augmented with the ApplicationScopedSetting attribute. Ultimately, these attributes are used by ApplicationSettingsBase and the settings system to determine which settings can be written and where they can be written to.

The DefaultSettingValue attribute that's applied to both properties represents the value you entered into the Value column of the settings grid on the Settings Editor, and this attribute is used to support several settings rollback scenarios, as you'll soon see.

The generated Settings class also implements a helper method that provides access to a statically managed instance of itself. All generated settings classes reside in a namespace that matches the class's location in the project folder hierarchy. For example, the VS05 default Settings class resides in the following namespace: DefaultNameSpace.Properties. A generated class for a settings file located in the project root resides in the following namespace: DefaultNameSpace.

Although not configurable from the Settings Editor, the generated values for both the namespace and the class name are also stored in the settings file in the GeneratedClassNamespace and GeneratedClassName attributes:

<?xml version='1.0' encoding='utf-8'?> <SettingsFile ... GeneratedClassNamespace="ApplicationSettingsSample.Properties" GeneratedClassName="Settings"> ... </SettingsFile>

We have quite a flexible system for configuring application, user, and roaming-user settings, leaving you with a nicely generated strongly typed settings class. Now it's time to unleash the beast.

Категории