45
.NET 4.0: Declarative Programming Using XAML Rob Relyea Architect Microsoft Corporation robrelyea.com/bl og Daniel Roth Program Manager Microsoft Corporation TL36

Rob Relyea Architect Microsoft Corporation robrelyea.com/blog Daniel Roth Program Manager Microsoft Corporation TL36

Embed Size (px)

Citation preview

.NET 4.0:Declarative Programming Using XAML

Rob RelyeaArchitectMicrosoft Corporationrobrelyea.com/blog

Daniel RothProgram ManagerMicrosoft Corporation

TL36

State Of XAML In 2008Great start in .NET 3.0

XAML Vocabularies UI: Collaboration of Designer and Developer Workflow: Declarative coordination of work Electronic Paper: XML Paper Specification (XPS)

Runtimes: .NET 3.0, Silverlight 1 and 2 Tools: VS 2008 (WPF + WF Designer +

XAML editor), Expression Blend XAML Specifications published

[MS-XAML], [MS-WPFXV], [MS-SLXV], and XPS File Converters: Blender, Fireworks, LightWave,

Flash, SVG, Visio, Maya, 3ds, Illustrator, WMF, EMF, PSD, DWG, DXF, OBJ, and more

Many scenarios are too hard Make it faster Fundamentals of XAML/BAML need

continued investment Compilation, debugging, obfuscation,

localization, FxCop, VS integration, and more Need language evolution Should everybody write their own parser?

Need a consistent XAML to CLR Binding for Type information

Model more domains in XAML

State Of XAML In 2008Still much to do!

XAML In .NET 4.0

Language Enhancements Faster parsing! XAML Documents are easier to parse,

process, and transform Support for more domains, from

Microsoft and others

XAML 2009

announcing

XAML 2006<Label Target="{Binding ElementName=firstName}">

First Name</Label>

<TextBox Name="firstNameBox" />

XAML 2009<Label Target="{x:Reference firstNameBox}">…</>

<TextBox Name="firstNameBox" />

Or even better:<Label Target="firstNameBox">First Name</Label>

<TextBox Name="firstNameBox" />

{x:Reference name} Better support for name references

Built-in Types Simplify authoring

XAML 2006 <sys:String xmlns:s="clr-

namespace:System;assembly=mscorlib“>Foo </s:String>

XAML 2009

x:Stringx:Charx:Singlex:Double

x:Booleanx:Bytex:Int16x:Int32

x:Int64x:Decimalx:Objectx:Uri

x:Timespanx:Arrayx:Listx:Dictionary

XAML 2006class PersonCollection :

ObservableCollection<Person> {}

<l:PersonCollection>

<Person Name="Dan" />

</l:PersonCollection>

XAML 2009<ObservableCollection x:TypeArguments=“Person”>

<Person Name="Dan" />

<Person Name="Rob" />

</ObservableCollection>

x:TypeArgument Generics support everywhere

XAML 2006String or MarkupExtension keys only

XAML 2009<Switch x:TypeArguments="x:Int64"

Expression="[rating]">

<Sequence x:Key="10">…</Sequence>

<Sequence>

<x:Key>9</x:Key>

</Sequence>

</Switch>

<x:Key>…</x:Key> Support arbitrary dictionary key types

XAML 2006<Button Click="button_Click" /> + markup compilation

XAML 2009No markup compilation required!

Also:

<Button Click="{DelegateCreatingME Foo}" />

Or even:

<Button>

<Button.Click>

<DelegateCreatingME Name="Foo" />

<Button.Click>

</Button>

Better Declarative Events Beyond method names

XAML 2006<Window x:Class="WpfApplication1.Window1" … />

XAML 2009<Activity x:Class="ActivityLibrary1.Prompt" …>

<x:SchemaType.Members>

<x:SchemaProperty Name="Text"

Type="InArgument(x:String)" />

<x:SchemaProperty Name=“Result" Type="OutArgument(x:String)" />

</x:SchemaType.Members>

<Sequence>…</Sequence>

</Activity>

Better Declarative Type Authoring Define new properties

XAML 2006Write a TypeConverter

XAML 2009 <Random>

<!-- Random seed -->

<x:Arguments>

<x:Int32>123</x:Int32>

</x:Arguments>

</Random>

<x:Arguments>…</x:Arguments> Instantiate using non-default constructors

XAML 2006Write a TypeConverter

XAML 2009<Guid x:FactoryMethod="Guid.NewGuid" />Same as:

<Guid x:FactoryMethod="NewGuid" />Composes with x:Arguments

x:FactoryMethod Use static factory methods

XAML 2009 Enhancements Summary

Improved named references (x:Reference) Full generics support (x:TypeArguments) Built-in types Support arbitrary dictionary key types (x:Key) Specify event handlers as markup Define new properties (x:SchemaType.Members,

x:SchemaProperty) Use non-default constructors (x:Arguments) Use factory methods (x:FactoryMethod) Define XML namespace prefixes (xmlns) on

member elements

Textual - .Xaml<Canvas> <Button Background="Green"> Ok </Button> <Button Content="Cancel"> <Button.Background> Red </Button.Background> </Button></Canvas>

Canvas

ButtonButton

What Is A XAML Document?

Object Graph

Compact Binary – .Baml

StartObject StartMember

StartMember TeST

TeSTStartObject StartMember

ST=Canvas

ST=Button

SM=Children

SM=Background

SM=Content

“Green

“Ok”

TeST

TeST “Red”

“Cancel”

StartMember

StartObject StartMember

ST=Button SM=Background

SM=Content

DOM Tree

TNTN“Gree

n”

“Ok”

TNTN “Red”

“Cancel”

ON

PN

PN PN

ST=Canvas

SM=Children

PN PN

SM=Background SM=Content SM=Background SM=Conte

nt

ONST=Butto

n

ONST=Butto

n

XAML Data Model

XAML Documen

ts

1) StartObject

3) StartMember 4) End Member

5) Value

The 6 XamlNodeTypes And Their “Payloads”

Type

Member

instance

2) End Object

6) Namespace Prefix, NamespaceUri

XAML Data ModelO-M-V + Types

Rob RelyeaArchitectXAML & WPF

demo

XAML Data Model: O - M - V

Root Object Objects have Members Members contain Objects and/or Values Some Objects are Collections Collections have a property

to hold their Items Objects are instances of XamlTypes Members are instances of XamlMembers

System.Xaml.dll October 2008 CTP

announcing

XAML To Objects In 3.0 And 4.0

XML Nodes

Object Graph

object XamlReader.Load(XmlReader)

XML Nodes

Object Graph

object XamlReader.Load(XmlReader)

XML Node to XAML Node

XAML Node to Objects

Type Resolution

XML Nodes to XAML Nodes

Resolve Type Information

Create Object Graph

Reading XAML, Writing Objects

Rob RelyeaArchitectXAML & WPF

demo

Demo Recap

XML Nodes

Object Graph

object XamlReader.Load(XmlReader)

XML Nodes

Object Graph

object XamlReader.Load(XmlReader)

XML Node to XAML Node

XAML Node to Objects

Type Resolution

XML Nodes to XAML Nodes

Resolve Type Information

Create Object Graph

BAML To Objects In 3.0 and 4.0

BAML File

Object Graph

object application.LoadComponent(Uri)

BAML Node to XAML Node

XAML Node to Objects

Type Resolution

BAML File

Object Graph

object application.LoadComponent(Uri)BAML Nodes to

XAML Nodes

Resolve Type Information

Create Object Graph

XamlReader API Signature

public abstract class XamlReader: Idisposable { public abstract bool Read(); public abstract XamlNodeType NodeType { get; }

public abstract string Namespace { get; } public abstract string Prefix { get; } public abstract XamlType Type { get; } public abstract bool IsObjectFromMember { get; } public abstract XamlProperty Member { get; } public abstract object Value { get; }

public virtual void Skip() public virtual XamlReader ReadSubtree() public abstract XamlSchemaContext SchemaContext { get; } …}

XamlWriter API Signature

public abstract class XamlWriter : Idisposable{ public void WriteNode(XamlReader reader)

public abstract void WriteNamespace( string xamlNamespace, string prefix); public abstract void WriteStartObject(XamlType type, bool isObjectFromMember); public abstract void WriteEndObject(); public abstract void WriteStartMember( XamlProperty property); public abstract void WriteEndMember(); public abstract void WriteValue(object value);

public abstract XamlSchemaContext SchemaContext { get; } …}

Objects To XAML In 3.0 And 4.0

Object Graph

XMLNodes

void XamlWriter.Save(Object, XmlWriter)

Object Graph

XML Nodes

void XamlWriter.Save(Object, XmlWriter)

Objects to XAML Node

XAML Nodes to XML Nodes

Type Resolution

Objects to XAML Nodes

Resolve Type Information

Write XML Nodes

Reading Objects, Writing XAML

Rob RelyeaArchitectXAML & WPF

demo

Demo Recap…

Object Graph

XMLNodes

void XamlWriter.Save(Object, XmlWriter)

Object Graph

XML Nodes

void XamlWriter.Save(Object, XmlWriter)

Objects to XAML Node

XAML Nodes to XML Nodes

Type Resolution

Objects to XAML Nodes

Resolve Type Information

Write XML Nodes

XamlXmlWriter

XamlXmlReader ObjectWriter

ObjectReader

BamlW

riter

BamlReader

X

D

B

O

Dom

Reader

Dom

Writer

Xaml Loading @ Runtime (X to O)

Optimizing Xaml to Binary Form (X to B)

Binary Form Loading @ Runtime (B to 0)

Designers – loading into a DOM & Objects

Xaml Saving (O to X)

Primary Scenarios

File Converters Today

PDF File

XAML File

void PdfFile.SaveAsXaml(pdfFileName, xamlFileName)

Draw correct arrow here

PDF data to XAML Nodes

Resolve Type Information

Write XML Nodes

File Converters In .NET 4

PDF File

Object Graph

object PdfFile.LoadAsObjects(pdfFileName)

Draw correct arrow here

PDF data to XAML Nodes

Resolve Type Information

Create Object Graph

System.Xaml.dll October 2008 CTP

announcing

.NET Is Getting More Model DrivenSay what you want without saying how to do it

XAML allows you to create rich application models

ApplicationModel Framework

WPF

WFWCF

.NET Is Getting More Model DrivenBenefits

Easier to tool and manipulate Easier to deploy and version Easier to retarget to

different execution environments Easier to statically verify and analyze Easier to query and compose Unified XAML format and CLR binding

.NET Is Getting More Model Driven More XAML support

WCF

WF

WPF

.NET 3.0 .NET 3.5 .NET 4.0

Rich XAML support

Rich XAML support

Rich XAML support

Rich XAML support

Rich XAML support

XAML with code behind

XAML with code behind

No XAML support

LimitedXAML support

WPF, WF, WCF in XAML

Daniel RothProgram ManagerXAML & Connected Framework

demo

WCF, WF, WCF In XAMLStock advice application

Get advice from two analysts if you should buy a certain stock

WPF App

WF Workflow

WCF ClientOperation

WCF ClientOperation

Update UI

WCF Advice Service1

WCF Advice Service2

Button.Click

XAML 2009 Language Enhancements

System.Xaml.dll Faster! Making XAML Documents easier to parse,

process, and transform XAML Ready Frameworks

More domains that can be modeled with a XAML Vocabulary, from Microsoft and others

Try out the bits and give feedback!

Takeaways: XAML In .NET 4.0

Related Sessions Will Go Here

Point to some other sessions here WPF, WF, WCF, Silverlight…

Discussion!

Evals & Recordings

Please fill

out your

evaluation for

this session at:

This session will be available as a recording at:

www.microsoftpdc.com

Please use the microphones provided

Q&A

© 2008 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market

conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.