module 14 application settings, state, and life cycle

14
Module 14 Application Settings, State, and Life Cycle

Upload: noreen-york

Post on 13-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Module 14 Application Settings, State, and Life Cycle

Module 14

Application Settings, State, and Life Cycle

Page 2: Module 14 Application Settings, State, and Life Cycle

Module Overview

• Managing Application State by Using Application Settings

• Managing the Application Life Cycle

Page 3: Module 14 Application Settings, State, and 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

Page 4: Module 14 Application Settings, State, and Life Cycle

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

Page 5: Module 14 Application Settings, State, and Life Cycle

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

Page 6: Module 14 Application Settings, State, and Life Cycle

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

Page 7: Module 14 Application Settings, State, and Life Cycle

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();}

Page 8: Module 14 Application Settings, State, and Life Cycle

Lesson 2: Managing the Application Life Cycle

• Understanding the Application Class

• Handling Application Events

Page 9: Module 14 Application Settings, State, and Life Cycle

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

Page 10: Module 14 Application Settings, State, and Life Cycle

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

Page 11: Module 14 Application Settings, State, and Life Cycle

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

Page 12: Module 14 Application Settings, State, and Life Cycle

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.

Page 13: Module 14 Application Settings, State, and Life Cycle

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?

Page 14: Module 14 Application Settings, State, and Life Cycle

Module Review and Takeaways

• Review Questions