Welcome to WindowsClient.net | Sign in | Join

Rob Relyea - XAMLified

WPF, Silverlight and Xaml

The 8 Benefits of XAML for UI and Beyond

Given another 4 years of experience since I wrote “Our 7 Goals for XAML”, here is my updated list of XAML Benefits:

XAML Describes Data in a Concise, but Human and Machine Comprehensible, Way

Declarative formats provide the best and most concise way to represent many types of data. Comprehension of a declarative format is better than a general purpose programming language.

<BookList>
  <Book Author="J. K. Rowling"
            Title="Harry Potter and the Philosopher’s Stone"
            Year="1997" />
  <Book Author="L. Frank Baum"
            Title="Wizard of Oz"
            Year="1900" />
</BookList>

BookList bl = new BookList();
Book b1 = new Book();
b1.Author = "J. K. Rowling";
b1.Title = “Harry Potter and the Philosopher’s Stone”;
b1.Year = 1997;
bl.Add(b1);
Book b2 = new Book();
b2.Author = "L. Frank Baum";
b2.Title = “Wizard of Oz”;
b2.Year = 1900;
bl.Add(b2);

XAML is Useful for Many Types of Data

Microsoft already supports XAML Vocabularies to describe UI ([MS-WPFXV], [MS-SLXV]), Workflow (WF) and Electronic Paper (XPS).  Many other vocabularies are possible. 

XAML 2009’s new Name Referencing enables object graph definition, which opens up the number of types of data that can be described. WF 4 leverages this for modeling Flow Charts. WCF 4 Services can be modeled in XAML more naturally as well.

XAML uses Typed Object Models for Better Programming and Validation

Systems that have a strongly-typed OM (object model) enable:

  • Manipulation of objects after initialization with strong-typing, which enables compile time checking of code.
  • Markup Validation using the OM as the Type’s schema, as opposed to having to build an additional schema.
class Book
{
    string Author { get; set; }
    string Title { get; set; }
    int Year { get; set; }
}
Works:
b1.Year = 2000;

Compile Time Error:
b1.Year = "6/1999";
Validation Error:
<Book Year="6/1999" />

XAML is Extensible and Version Tolerant

Systems that are extensible through class inheritance can be modeled easily in XAML.  Since BookList can hold Books, and since one can derive a PaperbackBook from Book, I can use PaperbackBook wherever a Book is allowed, in code or markup.

class PaperbackBook : Book
{
}
<BookList>
  <Book … />
  <my:PaperbackBook … />
</BookList>

XAML's default is that every tag and attribute must be known, while HTML defaults to ignoring any mistyped element name, attribute name or attribute value. While there is a place for this ignoring to allow for forward compatibility, XAML uses Markup Compatibility and Extensibility (ECMA-376 Part5) for backward/forward compatibility.

XAML is Toolable

Systems described in XAML will be creatable and consumable by a wide set of tools.  This goal drives our decisions throughout XAML, from our XML representation to schema and beyond.

XAML is XML

In .NET 3 and Silverlight, all valid XAML files must be valid XML files. The opposite isn't always true…all XML files are not XAML files. This bet on XML is important because it enables an ecosystem of tools, systems, APIs and developers familiar with XML to get a strong start on understanding XAML.

.NET 4 is broadening what it means to be a XAML document. We’re introducing a XamlReader and XamlWriter abstraction in System.Xaml.dll which enables anybody to provide or store XAML based data.  We’ll provide readers and writers for XML, BAML, and object graphs, but many other storage or in memory representations are possible.  We believe that our XML representation shipped with .NET 3.0 in 2006 will continue to be the most common representation of XAML documents, but time will tell.

XAML Enables Event Driven Programming Models

XAML enables description of object graphs which include the setting of properties and the wiring of events.  This enables systems which need an event driven programming model.

XAML is Compiled or Interpreted

XAML is sometimes treated as source code, sometimes as runtime instructions.  Different frameworks will make different choices around this: WPF chooses to compile XAML into a binary representation which gets embedded inside an assembly; Silverlight chooses to validate the XAML at compile time, but it embeds the original XAML file into a .XAP file.  Either WPF or Silverlight can also interpret at runtime a XAML file that is not downloaded with the “application”.

Published Thursday, November 06, 2008 6:49 AM by Rob_Relyea

Comments

# re: The 8 Benefits of XAML for UI and Beyond@ Thursday, November 06, 2008 11:47 AM

Rob,

XAML also enables great UI design patters like M-V-MV.

Nice post!

Karl

# re: The 8 Benefits of XAML for UI and Beyond@ Thursday, November 06, 2008 11:53 AM

When using XML (or any file format) to load and store data you have to maintain 3 systems.  1) The classes that will hold the data at runtime.  2) The file loading and storing module.  Even using XmlReader there is significant processing to get the XML data into your data structures.  3) The data files themselves.

With XAML: #2 is done for you, and is yet general enough to do nearly everything you want.

by Brian Chapman

# re: The 8 Benefits of XAML for UI and Beyond@ Thursday, November 06, 2008 12:39 PM

Agreed XAML is great and you  guys are doing great job on it. Especially the new XAML with support for non-def constructors and generics.

But

BookList bl = new BookList();

Book b1 = new Book();

b1.Author = "J. K. Rowling";

b1.Title = “Harry Potter and the Philosopher’s Stone”;

b1.Year = 1997;

bl.Add(b1);

Book b2 = new Book();

b2.Author = "L. Frank Baum";

b2.Title = “Wizard of Oz”;

b2.Year = 1900;

bl.Add(b2);

BookList bl = new BookList(){

new Book(){

Author = "J. K. Rowling",

Title = “Harry Potter and the Philosopher’s Stone”,

Year = 1997

},

new Book(){

     Author = "L. Frank Baum",

     Title = “Wizard of Oz”

     Year = 1900

}

};

C# is sweet too!

By the way, I am eager to get my hands on your new XAML Pad. Hopefully you would integrate the step-by-step loading(like f11, the one you mentioned in the talk).

# re: The 8 Benefits of XAML for UI and Beyond@ Thursday, November 06, 2008 12:41 PM

Shouldn't

Works:

b1.Year = 2000;

Compile Time Error:

b1.Year = "6/1999";

be

Compile Time Error:

b1.Year = 2000;

Works:

b1.Year = "6/1999";

?

# re: The 8 Benefits of XAML for UI and Beyond@ Thursday, November 06, 2008 12:48 PM

Tanveer-

I had mistakenly defined Book.Year as a string originally.  Somebody else caught that mistake and I've updated it to an int.  So the change you suggest is no longer necessary.

Thanks, Rob

# re: The 8 Benefits of XAML for UI and Beyond@ Thursday, November 06, 2008 1:09 PM

My favorite thing is ISupportInitialize support that DataContractSerializer lacks. Great that XamlReader/XamlWriter get more common namespace.

# The 8 Benefits of XAML for UI and Beyond @ Thursday, November 06, 2008 4:35 PM

WPF team's PM Architect Rob Relyea wrote up a good post today summarizing the benefits of XAML for UI

# re: The 8 Benefits of XAML for UI and Beyond@ Thursday, November 06, 2008 5:23 PM

Denis-

Just to clarify...XamlReader/XamlWriter that I mention are not the same X->O or O->X APIs from .NET 3.  They are now abstract classes which are more similar to XmlReader/XmlWriter.  Go watch the PDC talk on XAML to learn more.

Thanks, Rob

# Silverlight News for November 07, 2008@ Friday, November 07, 2008 3:19 AM

Pingback from  Silverlight News for November 07, 2008

# 2008 November 09 - Links for today &laquo; My (almost) Daily Links@ Sunday, November 09, 2008 2:28 AM

Pingback from  2008 November 09 - Links for today &laquo; My (almost) Daily Links

# re: The 8 Benefits of XAML for UI and Beyond@ Tuesday, November 18, 2008 3:34 AM

Rob,

As for XamlReader/XamlWriter PDC, it was a brilliant talk and awesome news. Waiting impatiently for November CTP.

Guess there will be a "DOM box" age on Codeplex starting from December :)

# re: The 8 Benefits of XAML for UI and Beyond@ Saturday, November 29, 2008 10:51 AM

What are your thoughts on xaml being used as a configuration file alternative? currently writing config sections etc is an ugly affair.

by Stephen

Leave a Comment

(required) 
(required) 
(optional)
(required) 
Page view counter