Best Industrial Training in C,C++,PHP,Dot Net,Java in Jalandhar

Sunday 22 December 2013

Introduction to XAML


Introduction to WPF

Windows Presentation Foundation (WPF) is Microsofts next generation UI framework for building Windows client applications with visually stunning user experiences. By using WPF, you can create a wide range of both standalone and browser-hosted applications. WPF combines application UIs, 2D graphics, 3D graphics, documents and multimedia into one single framework. The core of WPF is a resolution-independent and vector-based rendering engine that is built to take advantage of modern graphics hardware. This makes the UI faster, scalable and resolution independent. WPF is builds on top of the DirectX (Direct3D), instead of relying on the older GDI/GDI+ subsystem.

Separation of Appearance and Behavior

WPF provides a consistent programming model for building applications and a clean separation between the user interfaces and the business logic. The appearance is generally specified in the Extensible Application Markup Language (XAML), the behavior is implemented in a managed programming language like C# or Visual Basic. This separation of appearance and behavior has the following benefits:

(i) Designers and developers can work on separate models.
(ii) Appearance and behaviour are loosely coupled

Resolution independence

All measures in WPF are logical units - not pixels. A logical unit is a 1/96 of an inch. If you increase the resolution of your screen, the user interface stays the same size - it just gets crispier. Since WPF builds on a vector based rendering engine it's incredibly easy to build scalable user interfaces.
XAML stands for Extensible Application Markup Language introduced with WPF. XAML is a declarative markup language. Today XAML is used to create user interfaces in WPF, Silverlight, declare workflows in WF and for electronic paper in the XPS standard.

Advantages to XAML

All you can do in XAML can also be done in code. 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:

1) XAML code is short and clear to read.
2) The separation of XAML and UI logic allows it to clearly separate the roles of designer and developer.


XAML vs. Code

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

<Grid Name="grid1">
 <Button Name="button1" Content="Click Me" Width="150" Height="30"></Button>
</Grid>

The same code expressed in C# will look like this:

Button b = new Button();
b.Width = 150;
b.Height = 30;
b.Content = "Click Me";
grid1.Children.Add(b);

As you can see is the XAML, code is much shorter and clearer to read and that's the power of XAML. 


Hello World Application in WPF

  1. Open Visual C# 2010 Express
  2. Then click on File-->New Project
  3. A dialog box will open, then choose "WPF Application" as project type.
  4. Then click ok.


Visual Studio creates the project and automatically adds some files to the solution. A Window1.xaml and an App.xaml.



5. Drag a Button from the toolbox to the Window.



6. Double click on the button and visual c# 2010 express automatically creates a method in the code-behind file that gets called when the button is clicked.

Then write the code below:

 private void button1_Click(object sender, RoutedEventArgs e)
  {
            MessageBox.Show("Hello WPF!");
   }






No comments:

Post a Comment