WPF Demystified

45
It’s always better live. MSDN Events Chris Koenig Chris Koenig Developer Evangelist Developer Evangelist [email protected] [email protected] http://blogs.msdn.com/chkoenig http://blogs.msdn.com/chkoenig WPF Demystified WPF Demystified

description

From my September 25, 2008 MSDN Unleashed presentation in Austin, TX

Transcript of WPF Demystified

Page 1: WPF Demystified

It’s always better live. MSDN Events

Chris KoenigChris KoenigDeveloper EvangelistDeveloper [email protected]@microsoft.comhttp://blogs.msdn.com/chkoenighttp://blogs.msdn.com/chkoenig

WPF DemystifiedWPF Demystified

Page 2: WPF Demystified

Upcoming EventsUpcoming Events

October9-10 – Tulsa TechFest20-22 – in.Telligent Conference

November4 – Election Day (Rock the Vote!)14 – Dallas Agile.NET Conference18-20 – MSDN TechDays (Dallas)

December8-10 – VSLive! Dallas

January16-18 – We Are Microsoft

Page 3: WPF Demystified

We Are Microsoft / GiveCampWe Are Microsoft / GiveCamp

Volunteer event

Building web applications for local-area charities & non-profits

January 16-18 at BravoTECH

Very rewarding experience!

http://www.wearemicrosoft.com

http://www.givecamp.org

Page 4: WPF Demystified

What Will We Cover?What Will We Cover?

Essential concepts of Windows Presentation Foundation

How to create a data-bound, service based WPF application

Page 5: WPF Demystified

Level 100

Helpful ExperienceHelpful Experience

User Interface development

.NET language

VB

C#

Familiar with XML

Visual Studio 2008

Page 6: WPF Demystified

AgendaAgenda

Discover the top WPF benefits

Creating a WPF application

XAML

Layout

Styles

Using services and databases

Data Templates

User Controls

Control Templates

Page 7: WPF Demystified

WPF AdvantagesWPF Advantages

Composable Elements

Flexible layout engine

Powerful data-binding architecture

Impressive print, font and document capabilities

Page 8: WPF Demystified

WPF AdvantagesWPF Advantages

Lookless controls

Styles and Templates

Full integration of all UI disciplines

Page 9: WPF Demystified

AgendaAgenda

Discover the top WPF benefits

Creating a WPF application

XAML

Layout

Styles

Using services and databases

Data Templates

User Controls

Control Templates

Page 10: WPF Demystified

WPF ApplicationsWPF Applications

Create WPF application with

Visual Studio 2008

Expression Blend

A WPF application is managed code

It runs on the .NET 2.0 runtime

Some classes similar to the WinForms model

Application class

Has Window class instead of Form class

Use .Show() or .ShowDialog() to display

Page class for forward/back navigation

Host WinForms controls on WPF windows

Host WPF controls on Winform forms

Page 11: WPF Demystified

DemoDemo

Creating WPF application in Creating WPF application in Visual Studio 2008Visual Studio 2008

Page 12: WPF Demystified

AgendaAgenda

Discover the top WPF benefits

Creating a WPF application

XAML

Layout

Styles

Using services and databases

Data Templates

User Controls

Control Templates

Page 13: WPF Demystified

XAMLXAML

XAML is a declarative programming language

Tool friendly: Easy to persist UI structure in XAML

Expression Blend, ZAM 3D, Visual Studio

XAML is parsed by dedicated parser

Parser converts the tree of XML into a instances of objects

Most .NET types can be stored in XAML tree

Most likely that these will be Visual items

WPF, WF, Silverlight & XPS use XAML

Silverlight XAML is a sub-set of WPF XAML

Page 14: WPF Demystified

XAMLXAML

Page 15: WPF Demystified

DemoDemo

XAMLXAML

Page 16: WPF Demystified

AgendaAgenda

Discover the top WPF benefits

Creating a WPF application

XAML

Layout

Styles

Using services and databases

Data Templates

User Controls

Control Templates

Page 17: WPF Demystified

LayoutLayout

WPF includes a flexible, panel based layout engine

What is layout?

Measuring and arranging a region of WPF UI

All layout is handled by panels

Panel has ultimate control over every child

Child size, location, ZOrder

Child location relative to other children

Frees you from hard coding sizes/locations

Mix and combine panels

Page 18: WPF Demystified

LayoutLayout

Child communicates with host panel via Attached properties

Inform their containing panel of desired layout

Example: Grid.Column = "3"

Each panel handles one type of layout

Canvas: Absolute positioning

StackPanel: Children stacked horizontal/vertical

WrapPanel: Stacked, with multi children per line

DockPanel: Attach child to side of panel

Grid: Create rows/columns for children

Panel model is extensible with your own panels

Page 19: WPF Demystified

DemoDemo

LayoutLayout

Page 20: WPF Demystified

AgendaAgenda

Discover the top WPF benefits

Creating a WPF application

XAML

Layout

Styles

Using services and databases

Data Templates

User Controls

Control Templates

Page 21: WPF Demystified

StylesStyles

Styles provide a convenient way to itemize a set of property values for an element type

Example: Set font attributes for all textboxes

To define a Style

Styles are defined in a Resource element

Any element in XAML can have a resource section

Usual locations are Page, Window and Application level

Named styles have a x:Key value

Use TargetType to limit style to one element type

Page 22: WPF Demystified

StylesStyles

To apply a Style to an Element

Reference the Style by setting the Style attribute

Use the Markup Extension syntax '{}'

Page 23: WPF Demystified

DemoDemo

StylesStyles

Page 24: WPF Demystified

AgendaAgenda

Discover the top WPF benefits

Creating a WPF application

XAML

Layout

Styles

Using services and databases

Data Templates

User Controls

Control Templates

Page 25: WPF Demystified

Using Services and DBsUsing Services and DBs

Accessing data from WPF

XML

LINQ

LINQ to Objects

LINQ to SQL

LINQ to XML

LINQ to [your provider goes here]

ADO.NET

Entity Framework

Invoking Services from WPF

WCF

SOAP, REST, WS-*

Page 26: WPF Demystified

Using Services and DBsUsing Services and DBs

ADO.NET Data Service

Code name “Astoria”

Exposes SQL data over HTTP via REST endpoints

Examples: http://localhost/CohoDS.svc/Wines

…Northwind.svc/Customers?$filter=contains(City,'Kent')

Authoring the Data Service

Website

ADO.NET Entity Data Model

ADO.NET Data Service

Consuming the DataService

Generate proxy with DataSvcUtil.exe

Use “Add Service Reference”

Page 27: WPF Demystified

DemoDemo

Astoria Data ServicesAstoria Data Services

Page 28: WPF Demystified

AgendaAgenda

Discover the top WPF benefits

Creating a WPF application

XAML

Layout

Styles

Using services and databases

Data Templates

User Controls

Control Templates

Page 29: WPF Demystified

Data BindingData Binding

Binding overview

WPF synchronizes a data source and data target

Add binding to target element with {Binding ***}

Change notification

WPF dependency system updates target upon source changes provided:

Source must implement IPropertyNotifyChanged

Single binding

Bind a target to a single property on source

Example: TextBlock.Text to Customer.Age

List binding

Bind a target to a array, collection etc.

Example: Treeview.ItemsSource to Collection object

Page 30: WPF Demystified

Data TemplatesData Templates

Templates provide a convenient way to centralize the replacement UI for:

Tabular data

Hierarchical data

Controls

Data Template

Define the UI for one Type

Example: Customer class

Render UI with six TextBlock objects and one Image

Page 31: WPF Demystified

DemoDemo

Data TemplatesData Templates

Page 32: WPF Demystified

AgendaAgenda

Discover the top WPF benefits

Creating a WPF application

XAML

Layout

Styles

Using services and databases

Data Templates

User Controls

Control Templates

Page 33: WPF Demystified

Custom ControlsCustom Controls

Custom elements

Less need for custom elements in WPF

First consider getting custom look via

Composed UI

Styles

Templates

If you must create custom element choose base:

Existing control

Control

UserControl

Page 34: WPF Demystified

Custom Controls Custom Controls

User control benefits

Simple

Easy to compose sub-elements into new class

Easily add custom element to UI tree

Control library author

Create custom User Control

Compiled into assembly

Control consumer

Add reference to assembly

Bring namespace into scope with xmlns attribute

Page 35: WPF Demystified

DemoDemo

User ControlsUser Controls

Page 36: WPF Demystified

AgendaAgenda

Discover the top WPF benefits

Creating a WPF application

XAML

Layout

Styles

Using services and databases

Data Templates

User Controls

Control Templates

Page 37: WPF Demystified

Control TemplatesControl Templates

All controls in WPF are lookless

Each has a default UI template

We can replace default template with our own control template

Any valid UI element can be part of template

Templates also support binding

Templates do not change behavior of element

Page 38: WPF Demystified

DemoDemo

Control TemplatesControl Templates

Page 39: WPF Demystified

Session SummarySession Summary

WPF is a rich UI programming framework

Since WPF is managed code, working with .NET APIs is easy and familiar

Core concept is the composable UI model

Create UI out of any valid WPF element

Mix and merge elements

Formalize these replacement parts with Styles and Templates

Page 40: WPF Demystified

For More InformationFor More Information

Online Resources

msdn.microsoft.com

windowsclient.net

wpfwiki.com

Local Community

adnug.org

agileaustin.org

southcentraldevelopers.com

Page 41: WPF Demystified

For the latest titles, visitwww.microsoft.com/learning/books/devtools

Microsoft Press PublicationsMicrosoft Press Publications

Page 42: WPF Demystified

These books can be found and purchased at all major book stores and online retailers

Non-Microsoft PublicationsNon-Microsoft Publications

Page 43: WPF Demystified

Course ID Title

6460A Windows Presentation Foundation

For training information and availability www.microsoft.com/learning

Training ResourcesTraining Resources

Page 44: WPF Demystified

•Are you ready to take your career as a developer to the next level?

•Looking for a learning experience that is designed for you?

Join MSDN Ramp Up and Summit Your Career!

MSDN Ramp Up is your online source that provides free training and technical resources to help take your development skills to the next level.• Step-by-Step training plans to build your development skills.

• Premium technical content created by expert developers for developers.

• Access to valuable online e-learning, e-references, and virtual labs.

• 50% discount on select certification exams and 30% discount on Microsoft Press training kits.

Join Ramp Up for free today! Go to: http://msdn.microsoft.com/rampup

RAMP-UPRAMP-UP

Page 45: WPF Demystified