It is very common in Windows applications to store application-wide data such as database connection strings, application title and folder path,etc that should be available for the duration of the application instance. The general strategy for storing such data is to have public classes with public properties/fields and access them from anywhere in the application. However, with Windows Presentation Foundation (WPF), the framework itself provides an application-wide “storage bag”, Application.Properties
, that could be used for the very same purpose. This bag is an app-domain specific thread-safe key-value based IDictionary
instance.
using System.Windows;
... ...
Application.Current.Properties["conStr"] = "my connection string";
... ...
string conStr = (string) Application.Current.Properties["conStr"]; // From anywhere in the application
Since the key value is of type object
, a casting is required when retrieving the data from Application.Properties
.