Friday, 16 November 2012

Introduction to XAML in WPF/Silverlight


Introduction to XAML in WPF/Silverlight

XAML stands for Extensible Application Markup Language. Its a simple language based on XML to create and initialize .NET objects with hierarchical relations. Altough it was originally invented for WPF it can by used to create any kind of object trees.

Today XAML is used to create user interfaces in WPF, Silverlight, declare workflows in WF and for electronic paper in the XPS standard.

All classes in WPF have parameter less constructors and make excessive usage of properties. That is done to make it perfectly fit for XML languages like XAML.


XAML vs. Code

As an example we build a simple StackPanel with a textblock and a button in XAML and compare it to the same code in C#.


 <StackPanel>

    <TextBlock Margin=”20″>Welcome to the World of XAML</TextBlock>
    <Button Margin=”10″ HorizontalAlignment=”Right”>OK</Button>
</StackPanel>



The same expressed in C# will look like this:


// Create the StackPanel

StackPanel stackPanel = new StackPanel();
this.Content = stackPanel;
// Create the TextBlock
TextBlock textBlock = new TextBlock();
textBlock.Margin = new Thickness(10);
textBlock.Text = “Welcome to the World of XAML”;
stackPanel.Children.Add(textBlock);
// Create the Button
Button button = new Button();
button.Margin= new Thickness(20);
button.Content = “OK”;
stackPanel.Children.Add(button);
  


 As you can see is the XAML version much shorter and clearer to read. And that’s the power of XAMLs expressiveness

Advantages of XAML

All you can do in XAML can also be done in code. XAML ist just another way to create and initialize objects. You can use WPF without using XAML. It’s up to you if you want to declare it in XAML or write it in code. Declare your UI in XAML has some advantages:
  • XAML code is short and clear to read
  • Separation of designer code and logic
  • Graphical design tools like Expression Blend require XAML as source.
  • The separation of XAML and UI logic allows it to clearly separate the roles of designer and developer.

Happy Programming ! !

If you have any query mail me to Sujeet.bhujbal@gmail.com     


Regards

Sujeet Bhujbal

------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------




No comments:

Post a Comment