Sunday, August 28, 2011

XAML Magic - Few of the Basic Features..

The basic idea of XAML is to make UI design more generic and provide environment to work both developers and UI designers separately. Finally it’s ended up in XML structure, where everyone can understand the stuff very easily.

Just let me list down few of the main features that are coming with XAML,

1. Whatever you do in XAML, you can do the same with code as well

It’s like this, Here is the XAML Code,

  

Same thing you can do with the code too,

   
In XAML, Elements/Tags are representing the classes and the XML tag attributes are representing the class properties. This you alreay have seen in above XAML code and as an example,
"Margin="20">Welcome to the World of XAML"
Margin is a property of TextBlock class and that you can see as attribute of  tag. 
2.       Represent Properties as Elements
This is one of the interesting feature that you can find in XAML and it is looks like this,
Normally what you do is that you specify the content of the button directly as a text, but here you can take button content as anything that you can imagine as a UI control. In example it is a Image, but you can something different.
3.       Implicit Type Conversion 
This is about converting the types at runtime and for an example,


Border is the class that has property, which accepts “System.Windows.Media.Brushes.Blue” object as its value. In addition to that BorderThicknss also a property and which is accepting the Thickness Object as its value. 
4.       Markup Extensions 
This is about .NET attributes and where you can place them in XAML structure.  There are number of built in Markup extensions and in addition to that you can develop custom markup extensions by your own. 
These are the built in markup extensions you can find in .NET,
  • Binding
    To bind the values of two properties together.
  • StaticResource
    One time lookup of a resource entry
  • DynamicResource
    Auto updating lookup of a resource entry
  • TemplateBinding
    To bind a property of a control template to a dependency property of the control
  • x:Static
    Resolve the value of a static property.
  • x:Null
    Return
    null

Example,


5. Finally, Namespaces

Normally namespaces we used to find in .cs or .vb files in .NET (in most if the times), but this time you will have to invoke them in XAML, because, in XAML you are dealing with the objects, which are already defined for you some where by Microsoft or you or any other.

To invoke a relevant class tag in a XAML, you have to import them first into XAML document. In default you will get imported basic namespaces, into you XAML document by Visual Studio, when you working with it. Basic namespaces you will find in WPF applciations,

http://schemas.microsoft.com/winfx/2006/xaml/presentation - It is mapped to all wpf controls inSystem.Windows.Controls.

http://schemas.microsoft.com/winfx/2006/xaml - it is mapped to System.Windows.Markup that defines the XAML keywords.

Here is how it’s look like in XAML,

If you any custom controls developed in an Assembly called “Project.CustomControls”, here is the how we can import it into XAML document,

xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”
        xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”
        xmlns:myCustomControls ="clr-namespace:Project.CustomControls">
Cheers
Lakmal Kankanamge 

1 comment: