Join Walt Ritscher for an in-depth discussion in this video Exploring the relationship between XAML and .NET, part of Silverlight 5 Essential Training.
- View Offline
You cannot be a successful Silverlight developer or designer without understanding XAML. XAML, more properly known as Extensible Application Markup Language, is a new way to express your UI design and it permeates every corner of a Silverlight application. XAML is a declarative way to describe a related group of objects. In Silverlight this is usually a visual tree of objects. For example, a grid containing a text box and a button because XML is hierarchical, it's near perfect for describing the hierarchical relationships between instantiated objects.
It also makes an intuitive way to set up properties; here is an example of a UI you might want to create. On the left side of the screen is the output of the UI and on the right side of the screen is the XAML that describes it. As you can see by looking on the left side of the screen I have two labels, I have two text entry areas, and I have a Save button. That is represented over here in my XAML by having a grid at the root level, a couple of text blocks, a couple of text boxes, and then a button. And it's easy to see that the button is inside the grid and that I'm setting a value called Content =_Save.
XAML is not exclusive for Silverlight. Since it is just a way to describe a tree of .NET objects it is possible that XAML could have been used by any .NET type. In fact the XAML team is working with many other teams within Microsoft to enable this scenario. XAML is intended to be easy to generate from tools and it makes it much easier to separate out the design from the coding tools. Conceivably, each member of a UI team could use a different tool, Visual Studio, a 3D Modeling Tool. Your designers could be using Expression Blend and their tools could admit the correct XAML.
XAML is closely related to the underlying .NET types. I'd say that reading and understanding an XAML file in some cases is easier to understand than the equivalent procedural code. On this slide I have the procedural C# code that represents the same UI I showed you a few slides ago. Well it's easy to see that I'm creating a TextBlock here in line 47, and setting some properties. And it's a little less evident how I'm adding it to the grid. That's been done here on line 49 by saying Grid.SetRow TextBlock1, 0.
To me you can walk to this code and figure out what it is doing but in a hierarchical declarative language it's a lot easier to see the relationships between elements. XAML was originally created for WPF but other teams at Microsoft have adopted it for their products. Here's a few of the teams. One is Windows 8 Metro which is the new touch interface coming from Microsoft. The Windows Phone team uses XAML and Silverlight, of course does. And then last on this list is the XPS, which is Microsoft's XML Paper Specification, which is used for persisting documents in office.
XAML is just XML text. This provides some key benefits. It's readable, you can archive your XAML file and pull it out five years from now and be guaranteed that you will be able to read it with a text editor. It was deliberately designed to be toolable, it's validatable which means that you can verify that the elements you're writing in your XAML are available and that's your XAML syntax is correct. There are two areas of XAML to be concerned about however. One is that because the object tree is stored in text it takes a while to parse the contents and generate the visual tree of objects.
The second issue is that text files are always larger than their binary equivalent and that translates into a slightly longer download time. Here's what I want you to take away from this movie. XAML and .NET classes are two sides of the same coin. XAML is a simple, readable markup way of defining your visual tree. It's your choice how to construct your UI. The majority of your UI will likely be contained in a XAML file, but if you're stubborn you can do the entire tree in code. A button in Silverlight is still a .NET class. That means it has to be instantiated before you can use it.
Objects defined in XAML are instantiated too, that is the responsibility of the Silverlight Parser. That's enough about the XAML for now. There's an entire chapter devoted to it in this course. The rest of this chapter focuses on the code portion of Silverlight.
- Installing the toolkit and setting up the development environment
- Using Expression Blend vs. Visual Studio 2010
- Creating a Silverlight page
- Compiling an application
- Exploring the relationship between XAML and .NET
- Using C#
- Working with XAML
- Digging into the Dependency Property System
- Deploying and debugging code
- Creating sophisticated layouts with panel elements
- Managing Visual State with Visual State Manager
- Transforming the user interface (UI) with templates
- Understanding the event model
- Working with text
- Using business data in a binding
- Creating an out-of-browser (OOB) application
- Exploring trust levels
- Playing audio and video
- Handling errors
- Exploring animations
- Working with application windows
- Integration with XNA