Windows Presentation Foundation Unit 01

29
Visual Studio ® 2012: Windows ® Presentation Foundation

Transcript of Windows Presentation Foundation Unit 01

Page 1: Windows Presentation Foundation Unit 01

Visual Studio® 2012: Windows® Presentation

Foundation

Page 2: Windows Presentation Foundation Unit 01

Module 1: Creating an Application by Using Windows Presentation Foundation

• Overview of WPF

• Creating a Simple WPF Application

• Handling Events and Commands

• Navigating Between Pages

Page 3: Windows Presentation Foundation Unit 01

Lesson: Overview of WPF

• What Is WPF?

• WPF Architecture

• Defining User Interfaces in WPF

• WPF Capabilities and Features

• WPF Application Types

Page 4: Windows Presentation Foundation Unit 01

What Is WPF?

It is a new foundation for building Windows-based applications by using:

• Media

• Documents

• Application UI

.NET Framework 4.5.NET Framework 4.5

Windows PresentationFoundation(WPF)

Windows PresentationFoundation(WPF)

Windows Communication Foundation(WCF)

Windows Communication Foundation(WCF)

Windows WorkflowFoundation(WF)

Windows WorkflowFoundation(WF)

Windows CardSpace(WCS)

Windows CardSpace(WCS)

Page 5: Windows Presentation Foundation Unit 01

WPF Architecture

WPF Core Components

PresentationFrameworkPresentationFramework

Common Language Runtime

Common Language Runtime

PresentationCorePresentationCore

milcoremilcore

DirectXDirectXUser32User32

KernelKernel

Managed Code

Unmanaged Code

Page 6: Windows Presentation Foundation Unit 01

Defining User Interfaces in WPF

<Window ... >...

<Label>Label</Label><TextBox>TextBox</TextBox><RichTextBox ... />

<RadioButton>RadioButton</RadioButton><CheckBox>CheckBox</CheckBox><Button>Button</Button>

</Window>

<Window ... >...

<Label>Label</Label><TextBox>TextBox</TextBox><RichTextBox ... />

<RadioButton>RadioButton</RadioButton><CheckBox>CheckBox</CheckBox><Button>Button</Button>

</Window>

Page 7: Windows Presentation Foundation Unit 01

WPF Capabilities and Features

WPF provides the following capabilities and features:

• XAML-based user interfaces

• Page layout management

• Data binding

• 2-D and 3-D graphics

• Multimedia

• Animation

• Documents and printing

• Security

• Accessibility

• Localization

• Interoperability with Windows Forms controls

Page 8: Windows Presentation Foundation Unit 01

WPF Application Types

Stand-Alone Applications XAML Browser Applications (XBAPs)

Page 9: Windows Presentation Foundation Unit 01

Lesson: Creating a Simple WPF Application

• Demonstration: Creating WPF Applications by Using Visual Studio 2012

• Defining the Application

• Defining Windows or Pages

• Adding Controls

• Building and Running a WPF Application

Page 10: Windows Presentation Foundation Unit 01

Demonstration: Creating WPF Applications by Using Visual Studio 2012

In this demonstration, you will see how to:

• Create a stand-alone WPF application

• Create a browser application

• Add controls to your application

Page 11: Windows Presentation Foundation Unit 01

Defining the Application

<Application xmlns:x=… xmlns=… x:Class="MyApp.App" StartupUri="Window1.xaml">

<Application.Resources> … </Application.Resources>

</Application>

<Application xmlns:x=… xmlns=… x:Class="MyApp.App" StartupUri="Window1.xaml">

<Application.Resources> … </Application.Resources>

</Application>

Visual Studio generates a XAML application file that specifies:

• The code-behind class for the application

• The startup window or page

• Application-wide resources

Page 12: Windows Presentation Foundation Unit 01

Defining Windows or Pages

A stand-alone application contains windows or pages

• They are represented by <Window> or <Page> elements in the XAML file

• The code-behind file contains event-handler code

<Window xmlns:x=… xmlns=… x:Class="MyApp.Window1" Title="My Window">

<Grid> … </Grid>

</Window>

<Window xmlns:x=… xmlns=… x:Class="MyApp.Window1" Title="My Window">

<Grid> … </Grid>

</Window>

<Page xmlns:x=… xmlns=… x:Class="MyApp.Page1" WindowTitle="My Page">

<Grid> … </Grid>

</Page>

<Page xmlns:x=… xmlns=… x:Class="MyApp.Page1" WindowTitle="My Page">

<Grid> … </Grid>

</Page>

Page 13: Windows Presentation Foundation Unit 01

Adding Controls

Windows and pages contain controls

• The controls are represented by XAML elements

•<Button> and <TextBox> are examples of these

...<Grid> <TextBox Name="TextBox1" /> <Button Name="Button1">Click here</Button></Grid>...

...<Grid> <TextBox Name="TextBox1" /> <Button Name="Button1">Click here</Button></Grid>...

Page 14: Windows Presentation Foundation Unit 01

Building and Running a WPF Application

You can build and run a WPF application in Visual Studio

Stand-alone or browser application

Stand-Alone Application Browser Application

Page 15: Windows Presentation Foundation Unit 01

Lesson: Handling Events and Commands

• The WPF Event Model

• Handling WPF Control Events

• What Are Routed Events?

• Defining Routed Events

• What Are Commands?

• Demonstration: Defining Commands

Page 16: Windows Presentation Foundation Unit 01

The WPF Event Model

WPF controls generate events such as:

• Clicking buttons

• Entering text

• Selecting lists

• Gaining focus

Page 17: Windows Presentation Foundation Unit 01

Implement event handler method in the code-behind file

Specify an event handler in the XAML file

Handling WPF Control Events

<Button Name="Button1" Click="Button1_Click"> Click here</Button>

<Button Name="Button1" Click="Button1_Click"> Click here</Button>

public void Button1_Click( object sender, RoutedEventArgs e){ MessageBox.Show("Hello WPF");}

public void Button1_Click( object sender, RoutedEventArgs e){ MessageBox.Show("Hello WPF");}

Page 18: Windows Presentation Foundation Unit 01

What Are Routed Events?

Root elementRoot element

Child element #1

Child element #1

Child element#2

Child element#2

Leaf element #1

Leaf element #1

Leaf element #2

Leaf element #2

WPF can route events up or down the element tree

Event bubbling:Event routed up element treeEvent bubbling:Event routed up element tree

Event tunneling:Event routed down element treeEvent tunneling:Event routed down element tree

TunnelTunnel

TunnelTunnel

BubbleBubble

BubbleBubble

Item clickedItem clicked

Page 19: Windows Presentation Foundation Unit 01

Defining Routed Events

Example of event bubbling

• Define leaf elements inside a container element

• Handle leaf events at the container level

<StackPanel Button.Click="CommonClickHandler"> <Button Name="YesButton">Yes</Button> <Button Name="NoButton">No</Button></StackPanel>

<StackPanel Button.Click="CommonClickHandler"> <Button Name="YesButton">Yes</Button> <Button Name="NoButton">No</Button></StackPanel>

private void CommonClickHandler(object sender, RoutedEventArgs e) { Button b = e.Source as Button; ...}

private void CommonClickHandler(object sender, RoutedEventArgs e) { Button b = e.Source as Button; ...}

Page 20: Windows Presentation Foundation Unit 01

What Are Commands?

Commands separate the semantics of an action from its logic

• Multiple sources can trigger the same command

• You can customize the command logic for different targets

Key concepts in WPF commanding:

• Commands

• Command sources

• Command bindings

• Command manager

Examples of predefined commands:

• Copy, Cut, and Paste

Page 21: Windows Presentation Foundation Unit 01

Demonstration: Defining Commands

In this demonstration, you will see how to:

• Define menu items that perform Copy and Paste commands

• Use the native ability of the TextBox to process the Copy and Paste commands

Page 22: Windows Presentation Foundation Unit 01

Lesson: Navigating Between Pages

• The WPF Navigation Model

• Demonstration: Navigating Pages by Using Hyperlinks

• Handling Page Navigation Events

• Maintaining State by Using Navigation Services

Page 23: Windows Presentation Foundation Unit 01

The WPF Navigation Model

Navigate from one page to another pageNavigate from one page to another page

Navigate to a fragment in a pageNavigate to a fragment in a page

Navigate subcontent frames in a pageNavigate subcontent frames in a page

Page 24: Windows Presentation Foundation Unit 01

Demonstration: Navigating Pages by Using Hyperlinks

In this demonstration, you will see how to:

• Create hyperlinks to navigate to other pages

• Create hyperlinks to navigate between pages and page fragments

• Create a Frame to contain pages in a Window

Page 25: Windows Presentation Foundation Unit 01

Handling Page Navigation Events

Page Navigation Request

Page Navigation Request

NavigatingNavigating

NavigationProgressNavigationProgress

NavigatedNavigated

LoadCompletedLoadCompleted

FragmentNavigationFragmentNavigation

NavigationStoppedNavigationStopped

NavigationFailedNavigationFailed

Page 26: Windows Presentation Foundation Unit 01

Maintaining State by Using Navigation Services

Page1.xaml Page2.xaml

Page1.xaml

Next

Back

• KeepAlive property

• FrameworkPropertyMetadata.Journal

• IProvideCustomContentState

Page 27: Windows Presentation Foundation Unit 01

Lab: Creating a WPF Application

• Exercise 1: Creating a Stand-Alone WPF Application

• Exercise 2: Handling Events and Commands

• Exercise 3: Navigating Between Pages

• Exercise 4: Creating an XBAP Application

Logon information

Virtual machine 6460A-LON-DEV-01

User name Student

Password Pa$$w0rd

Estimated time: 60 minutes

Page 28: Windows Presentation Foundation Unit 01

Lab Review

• Why would you want to inherit your window from the NavigationWindow class?

• How do you add an event handler to the Click event of a <Button> element in XAML?

• What is the name of the property that you use to configure a button to use the NextPage command?

• What is the name of the event to which you connect a handler if you want to manually determine if a command is allowed to be executed?

• When your application is running in a browser (XBAP), why are you not able to access the FileName property of the OpenFileDialog class?

Page 29: Windows Presentation Foundation Unit 01

Module Review and Takeaways

• Review Questions

• Common Issues and Troubleshooting Tips

• Best Practices

• Tools