Module 14
Application Settings, State, and Life Cycle
Module Overview
• Managing Application State by Using Application Settings
• Managing the Application Life Cycle
Lesson 1: Managing Application State by Using Application Settings
• Understanding Application Settings
• Adding and Removing Application Settings
• Understanding the Settings Classes
• Reading and Writing Settings
User.config fileUser.config fileApp.config fileApp.config file
Web service URLWeb service URLConnection stringConnection string
Understanding Application Settings
Application SettingsApplication Settings
Settings classApplication-scoped
settings
User-scoped
settings
Adding and Removing Application Settings
To add application settings by using the Project Designer:To add application settings by using the Project Designer:
In the Settings grid, click a blank row
In the Name column, type the name for the setting
In the Type list, click the data type for the setting
In the Scope list, click Application or User
In the Value column, type a default value for the setting
In the Settings grid, click a blank row
In the Name column, type the name for the setting
In the Type list, click the data type for the setting
In the Scope list, click Application or User
In the Value column, type a default value for the setting
11
33
22
To remove application settings:To remove application settings:
•In the Settings grid, click the row for the setting to remove,
and then press DELETE
55
44
Settings provider classesSettings provider classesSettings classesSettings classes
public class MyUserSettings : ApplicationSettingsBase{ [UserScopedSetting()] public Color BackgroundColor { get { return ((Color)this["BackgroundColor"]); } set { this["BackgroundColor"] = (Color)value; } }}
public class MyUserSettings : ApplicationSettingsBase{ [UserScopedSetting()] public Color BackgroundColor { get { return ((Color)this["BackgroundColor"]); } set { this["BackgroundColor"] = (Color)value; } }}
Understanding the Settings Classes
SettingsBase
ApplicationSettingsBase
SettingsProvider
SettingsProviderAttributeSettingsProviderAttribute
XMLXML
LocalFileSettingsProvider
MyUserSettings
Reading and Writing Settings
<Window xmlns:p="clr-namespace:MyApplication.Properties" ...> <Grid> ... <Label Content="_Font Selection:" Target="{Binding Fonts}" /> <ComboBox x:Name="Fonts" Grid.Column="1" ItemsSource="{x:Static Fonts.SystemFontFamilies}" SelectedItem="{Binding Source={x:Static p:Settings.Default}, Path=UserFont, Mode=TwoWay}"> ... </ComboBox> ...
<Window xmlns:p="clr-namespace:MyApplication.Properties" ...> <Grid> ... <Label Content="_Font Selection:" Target="{Binding Fonts}" /> <ComboBox x:Name="Fonts" Grid.Column="1" ItemsSource="{x:Static Fonts.SystemFontFamilies}" SelectedItem="{Binding Source={x:Static p:Settings.Default}, Path=UserFont, Mode=TwoWay}"> ... </ComboBox> ...
private void Ok_Click(object sender, RoutedEventArgs e){ Properties.Settings.Default.Save();} private void Cancel_Click(object sender, RoutedEventArgs e){ Properties.Settings.Default.Reload();}
private void Ok_Click(object sender, RoutedEventArgs e){ Properties.Settings.Default.Save();} private void Cancel_Click(object sender, RoutedEventArgs e){ Properties.Settings.Default.Reload();}
Lesson 2: Managing the Application Life Cycle
• Understanding the Application Class
• Handling Application Events
Application definition:Application definition:
Understanding the Application Class
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="MyApplication.App" />
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="MyApplication.App" />
Application services:Application services:
• Create and manage common application infrastructure
• Track and interact with the application life cycle
• Retrieve and process command-line parameters
• Share application-level properties and resources
• Detect and respond to unhandled exceptions
• Return exit codes
• Manage windows in stand-alone applications
• Track and manage navigation
Handling Application Events
Application ObjectApplication Object Application CodeApplication Code
Run method
Startup
Deactivated
SessionEnding
Exit
Shutdown method
Activated
Not canceledCanceled
Explicit shutdown
Operating SystemOperating System
Lab: Creating a Settings Dialog Box
• Exercise 1: Creating Application and User Settings by Using Visual Studio
• Exercise 2: Creating a Dialog Box
• Exercise 3: Reading and Writing Settings
• Exercise 4: Consuming Settings Properties
Logon information
Estimated time: 60 minutes
Lab Scenario
You have been asked to update the Work Orders WPF application to remember the position and size of the main application window between sessions. You have also been asked to update the application to provide a settings dialog box to enable the user to configure the application. The settings dialog box should provide a way to view the database connection string details.
Lab Review
Review Questions
• What is the difference between application and user settings?
• Which method do you use to show a Window class as a dialog box?
Module Review and Takeaways
• Review Questions