xaml for the winforms developer - skimedic.com · xaml for the winforms developer philip japikse...

43
XAML for the Winforms Developer Philip Japikse (@ skimedic ) Principal Architect, InterKnowlogy Level: Intermediate

Upload: lehanh

Post on 10-Jul-2018

265 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

XAML for the Winforms Developer

Philip Japikse (@skimedic)

Principal Architect, InterKnowlogyLevel: Intermediate

Page 2: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

PHIL.ABOUT()

2

• Principal Architect, InterKnowlogy

• http://interknowlogy.com

• Microsoft MVP, MCSD, MCDBA, CSM, CSP

• Lead Director, Cincinnati .NET User’s Group

• Co-host Hallway Conversations Podcast

• www.hallwayconversations.com

• Founder, Agile Conferences, Inc.

• www.dayofagile.org

Page 3: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

WHAT IS WPF?

3

• Windows Presentation Foundation

• Next Generation Rich User Application Platform

• Declarative UI (XAML)

• Based on DirectX (not GDI/GDI+)

• Vector Based

• Supports true Hardware Acceleration

• Resolution Independence

• Leaps ahead with binding

• Data and other Elements

Page 4: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

WHAT ELSE IS IN WPF?

4

• Flexible flow layout model

• Styles and Templates

• OOTB document handling features

• True animation – not timer based

• A/V support (through WMP)

• Commands

• Routed Events

Page 5: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

WHAT’S NEW IN .NET 4?

5

• DataGrid, DatePicker, Calendar

• Visual State Manager

• Layout Rounding

• Multi-Touch support

• Custom Dictionaries

• Behaviors*

• Animation Easing

• Binding String Format

Page 6: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

WHAT’S NEW IN .NET 4.5

• Ribbon Control

• Improved Performance with large data

• Binding to Static Properties

• Accessing Collections on non-UI Threads

• Asynchronous Validation

• Delay Updating Data Source

• Retrieving Data Binding Info from Expression

• And more…

• http://tinyurl.com/wpf4-5

Page 7: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

WHAT’S MISSING

7

• WPF – What’s missing

• Visual Inheritance

• MDI – complete lack of support

Page 8: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

AGENDA

8

• Elements, Controls, and More

• Layout/Layout Containers

• Commands

• Custom Dictionaries

• Behaviors

• Binding

Page 9: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

ELEMENTS, CONTROLS, AND MORE

9

Page 10: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

ELEMENTS & CONTROLS

10

• Everything is an Element

• Controls are elements that can:

•Receive Focus

•Accept User Input

• Content Controls

• Hold a single element

•Window (Special)

•Labels, Buttons, CheckBox, RadioButton, ToolTips

•ScrollViewer, Border, Expander

• Text Controls

• TextBox , PasswordBox – Strings Only

• RichTextBox – Sophisticated Content – FlowDocument

Page 11: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

LIST CONTROLS

11

• List Controls (ItemsControl)

• Selectors

•ListBox, ComboBox, TabControl

• Non-Selectors

•Menus, ListView, GridView, TreeView, ToolBar

• Range Based Controls

• ScrollBar, ProgressBar, Slider

Page 12: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

EVENTS

12

• Routed Events replace usual .NET events

• Direct

•Originate in one element and don’t pass on

• Bubbling

•Travel up the element tree

• Tunneling

•Travel down the element tree

• Can add event handlers for child elements

• <Grid Button.Click=“ClickEvent” …

Page 13: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

MENUS

13

• Use “_” instead of “&” for Hot-Key

• Beware for XAML order vs Grid Order

• Similar to WinForms except

• MenuItem is a container control

• Use <Separator> instead of “-”

Page 14: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

LAYOUTS AND LAYOUT CONTAINERS

14

Page 15: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

LAYOUT

15

• “Web”-like layout model with Flexible Flow

• Gone are explicit sizing, absolute positioning, etc

• Containers try to allocate element’s requested space

• Layout Process

• Measure

•Loop through all elements asking for preferred size

• Arrange

•Container places elements in appropriate position

Page 16: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

LAYOUT CONTAINERS

16

• Standard

• Grid

•Most commonly used (default Window Panel)

• StackPanel, WrapPanel, DockPanel

•Used for UI Tweaks

• Canvas

•Absolute (think WinForms) positioning

• InkCanvas - Supports Digital Ink

• Specialized

• UniformGrid, TabPanel, ToolBarPanel, ToolBarOverflowPanel

Page 17: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

ATTACHED PROPERTIES

17

• Properties defined in one element used in another

• Grid.Row/Grid.Column

• Translated into method calls

• Grid.SetRow(control,x)

Page 18: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

Layouts

18

Page 19: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

COMMANDS

19

Page 20: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

COMMANDS

20

• WPF Command Model adds:

• Delegates to the Appropriate Commands

• Keeps state of control tied into state of Command

•Can_Execute

• Consists of:

• Commands – represents an application task

• Command Bindings – links command to code

• Command Sources – trigger the command

• Command Target – element on which the command is being performed

Page 21: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

Commands

21

Page 22: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

CUSTOM DICTIONARIES

22

Page 23: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

CUSTOM DICTIONARIES

23

• Enable by setting SpellCheck.IsEnabled=“True”

• Text File (<name>.lex) for custom words

• Add as a resource, assign to the CustomDictionaries collection, copy to output directory

• Four languages supported

• English, Spanish, German, French

Page 24: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

Custom Dictionaries

24

Page 25: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

BEHAVIORS

25

Page 26: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

BEHAVIORS

• Introduced in Expression Blend v3

• Encapsulate functionality into reusable components

• Drag & Drop

• Pan and Zoom

• Input Validation

• Watermark Text

• InvokeCommand

• Additional Behaviors are available from the Expression Gallery

• http://msdn.microsoft.com/en-us/expression/jj873995

Page 27: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

Behaviors

27

Page 28: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

BINDING

28

Page 29: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

ELEMENT BINDING

29

• Allows for powerful UI constructs all defined in XAML

• Can bind elements value directly to another element

No more coding “onChanged” events!

Page 30: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

DATA BINDING

30

• Supports

• IDataErrorInfo

• INotifyPropertyChanged

• INotifyCollectionChanged

• Can bind to *any* Dependency Property

• Choice of

• Datasets

• Data Objects

Page 31: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

Data Binding

31

Page 32: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

OBSERVABLES

32

Page 33: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

OBSERVABLES

33

•Models

•Leverage INotifyPropertyChanged

•Beware of magic strings

•Collections

•Leverage ObservableCollections

•Implements

•INotifyCollectionChanged

•INotifyPropertyChanged

Page 34: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

WHERE TO IMPLEMENT?

3/18/2015 34

• Anemic Model

• Implemented in ViewModel

• “Cleaner” model

• Lots of code duplication

• Anemic ViewModel

• Implemented in Model

• Less code duplication

• Mixing of concerns?

Page 35: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

VALIDATION

Page 36: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

VALIDATION METHODS

• Raise an error on your data object

• Normally errors are ignored on bindings

• Set NotifyOnValidationError

• Use INotifyDataErrorInfo or IDataErrorInfo

• INotifyDataErrorInfo is new in WPF 4.5

• Define validation at the binding level

• Note: Validation only occurs with TwoWay or OneWayToSource binding modes

Page 37: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

VALIDATESONEXCEPTION

• Raises exceptions from bound property

• Sets Validation.HasError = True

• Creates ValidationError object

• If NotifyOnValidationError = true

• WPF raises Validation.Error

Page 38: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

IDATAERRORINFO

• Add an string indexer for the Item property

• Error property is not used by WPF

• Relies on INotifyPropertyChanged

• Requires ValidatesOnDataErrorrs in binding

Page 39: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

INOTIFYDATAERRORINFO

• New in WPF 4.5

• Contains

• ErrorsChanged event

• HasError Boolean

• GetErrors() method to return the errors

• Relies on INotifyPropertyChanged

Page 40: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

CUSTOM VALIDATION RULES

• Derive from ValidationRule

• Add additional properties for custom configuration

• Override Validate()

• Return new ValidationResult(true || false, [Error Message])

• Add to Binding.ValidationRules collection

Page 41: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

Validation

Page 42: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

QUESTIONS?

42

Page 43: XAML for the Winforms Developer - skimedic.com · XAML for the Winforms Developer Philip Japikse (@skimedic) Principal Architect, InterKnowlogy Level: Intermediate

[email protected]

• www.skimedic.com/blog

• www.twitter.com/skimedic

• www.hallwayconversations.com

• www.about.me/skimedic

CONTACT ME

54