Reader Level:
ARTICLE

Silverlight 4.0 XAML Features

Posted by Pradeep Chandraker Articles | Silverlight with C# May 10, 2010
With Silverlight 4.0 there are some changes done in XAML parser. In this article I am going to cover some of the main changes done in XAML parser.
  • 0
  • 0
  • 4575

With Silverlight 4.0 there are some changes done in XAML parser. In this article I am going to cover some of the main changes done in XAML parser.

Direct content

In Silverlight 3.0 we had to use attribute syntax for caption or text.

<Button Content="Click Me"></Button>
<TextBlock Text="My TextBlock"></TextBlock>
<CheckBox Content="My CheckBox"></CheckBox>

This was the one of the inconsistency present with WPF.  Now the Silverlight control has better caption/text representation.

<Button>Click Me</Button>
<TextBlock>My TextBlock</TextBlock>
<CheckBox>My CheckBox</CheckBox>

Whitespace removed

In previous version of Silverlight, the XAML parser was showing whitespaces, it didn't remove whitespaces available in text.

<TextBox>

<TextBox.Text>

       This is

          the TextBlock

          whitespace test.

</TextBox.Text>

</TextBox>

When you run the application you will get this output.

1.gif

The Silverlight 4.0 XAML parser has handled whitespace behavior. Also it provides support for older version (when you are migrating from older version of Silverlight to Silverlight 4.0, this may be useful).

If you run the same above code you will get this output.

2.gif

To go back to previous version behavior you can use xml:space="Preserve"

<TextBox xml:space="preserve">

<TextBox.Text>

       This is

       the TextBlock

       whitespace test.

</TextBox.Text>

</TextBox> 

Dictionaries corrected

Now in XAML you can add items to dictionary just like we do in Resource Dictionary using "x:Key" attribute.

In Silverlight 4.0, any type or property implements IDictionary can be used in XAML. This is useful in complex XAML scenario when you want to declare dictionary rather than a list.

Note that, the above is for non-generic IDictionary interface.

Error message

Many bugs around error handling are fixed. In older version of Silverlight the biggest complain about XAML was poor error handling. Now errors are more descriptive like in WPF.

XmlnsDefinitionAttribue

When using different library or Silverlight toolkit you have to specifically define xmlns in every pages, for every assembly you want to use.

Now with XmlnsDefinitionAttribute you can do this with less code, all the assembly in the product can share same namespace which can then be defined only once.

COMMENT USING

Trending up