Powered by Blogger.

Saturday, March 1, 2014

Role of XAML in WPF

Here we will discusss how XAML is playing an important role in WPF.

Also you can check out:

Working with enterprise library for data access in asp.net Part-3

- Working with enterprise library for data access in asp.net Part-1

- the type initializer for threw an exception enterprise library 6

XAML, or Extensible Application Markup Language, is an XML based markup language created by Microsoft which is fundamental to WPF as HTML for web development.

XAML pronounced as zammel is the language used for creating user interfaces in WPF. It’s an XML-based markup language similar to HTML. XAML has applicability well beyond defining user interfaces. In fact, it’s even possible to represent data with XAML, such as array of strings or an instance of object.
Even though Visual Studio 2008 provides a WYSIWYG editor with drag and drop support for producing and manipulating XAML, you will often need to edit your markup directly. The default file extension is .xaml.

<Button Name = “button” Width = “100” Height = “40” Background = “Blue” Content = “Click Me” />

Property Element:
<Button Name = “button” Width = “100” Height = “40”>
<SolidColorBrush Color = “Blue”/>

Content Property:

<Button Content = “Click” />
<Button> Click </Button>

<Button.Content> Click </Button.Content>
<Ellipse Width = “24” Height = “24” Fill = “Yellow”/ >

<Button Background = “{StaticResource ResourceKey=mycolor}” Content = “Click” />
<Button Background = “{StaticResource mycolor}” Content = “Click” />

        <Color x:Key="mycolor">Red</Color>

Markup Extensions:

Sometimes we need to specify values in our markup that are either difficult to express in XAML or outside the scope of the XAML processor. XAML has a feature called markup extensions and it allows us to handle these awkward situations.

With WPF, we can store the color with a lookup key in an application’s resources and then we can set the background of the buttons to the color we stored in the resources. If we want to change the color, we need do so in only one place. To handle this in XAML we use the markup extensions.

The XAML with the markup extension might look like this:

<Button Background = “{StaticResource ResourceKey = <key>}” />

Markup extensions are identified by the presence of curly braces. The first word in the markup extensions tells WPF what kind of extension it is which is followed by a set of named parameters optionally. In this case, the extension is for retrieving a shared resource from a library or resources.
The name of the extension is StaticResource, and we provide a value for the ResourceKey parameter. Many extensions have a default parameter which doesn’t require to be referenced explicitly. In the above case ResourceKey is the default parameter so it can be written as:

<Button Background = “{StaticResource <key>}” />

Markup Extensions supported in XAML:

Binding: The extension used for binding data.
StaticResource: This is used for retrieving data from an application’s resources. Static resources are not expected to change while the application is running.
DynamicResource Similar to StaticResource, except that the data in the resource might change during runtime.
x:Null: Used for specifying a null value in XAML.

x:Type: This is used for supplying a System.Type object.

x: Array: This allows you to define an array of objects in XAML.


Post a Comment