Designing Settings

The preferred settings mechanism for WPF applications is the one provided by .NET 2.0 and Visual Studio 2005: the ApplicationSettingsBase class from the System.Configuration namespace with the built-in designer. To access the settings for your application, click on the Settings tab in your project settings. This will bring up the Settings Designer shown in Figure 2-13.

Here we've defined two settings: a user setting of type System.String, called LastExcuse; and an application setting of type System.Boolean, called ExcludeAnimalExcuses with a default value of True. These two settings will be loaded automatically when I run my application, pulled from the application's configuration file (named MyApplication.exe. config) and the user settings file saved from the application's last session.

The Settings Designer manages a settings file and generates a class that allows you to program against the settings. For instance, our settings example will result in the class in Example 2-6 being generated (roughly).

Figure 2-13. The Settings Designer

Example 2-6. The Settings Designer-generated class using namespace System.Configuration;

namespace excusegen.Properties { sealed partial class Settings : ApplicationSettingsBase { static Settings defaultInstance =

((Settings)(ApplicationSettingsBase.Synchronized(new Settings())));

public static Settings Default { get { return defaultInstance; }

[UserScopedSettingAttribute()] [DefaultSettingValueAttribute("")] public string LastExcuse {

get { return ((string)(this["LastExcuse"])); } set { this["LastExcuse"] = value; }

[ApplicationScopedSettingAttribute()] [DefaultSettingValueAttribute("True")] public bool ExcludeAnimalExcuses {

Example 2-6. The Settings Designer-generated class (continued)

get { return ((bool)(this["ExcludeAnimalExcuses"])); }

There are several interesting things to notice about Example 2-6. The first is the defaultlnstance member, which is initialized with an instance of the generated Settings class that's been synchronized to allow for safe multithreaded access. Second, notice that this defaultlnstance member is static and exposed from the Default static property, which makes it very easy to get to our settings, as we'll soon see. Finally, notice the two properties exposed from the Settings class, one property for each of our settings in the Settings Designer. You can see that the mode of each property, user versus application, the default value, and the type all match. Further, although a user setting is read-write (it has a getter and a setter), because it can change during an application session, the application setting is read-only (it has only a getter). The implementations of the properties are just type-safe wrappers around calls to the ApplicationSettingsBase base class, which does the work of reading and writing your settings to the associated settings storage.

Was this article helpful?

0 0
Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook


Post a comment