November 12, 2014

Getting started with WPF

Windows Presentation Foundation (WPF) is a new GUI framework of Microsoft as compare with Win Forms. 

Graphical User Interface (GUI) allows you to create an application with the wide variety of GUI controls.

For .NET developers the more interesting frameworks are WinForms and WPF. WPF is the newest GUI framework by Microsoft and developer have a choice to use either WinForms or WPF because Microsoft still support the earlier version GUI framework called "WinForms".

Both frameworks serves the same purpose but still there is LOT of differences between them like major difference is that The WinForms is a layer on top of the standard windows control while WPF is build from scratch and doesn't relies on windows control.

This is the just one difference but when you will start working on WPF, you'll see many more differences. Let me give you some advantages of both frameworks

WPF Advantages:

1. It's more flexible, so you can do more things without having to write or buy new controls.
2. Uses hardware acceleration for drawing the GUI, for better performance.
3. It allows you to make user interfaces for both Windows applications and web applications.

WinForms Advantages:

1. It's older and thereby more tried and tested.
2. There are already a lot of 3rd party controls that you can buy or get for free.
3. The designer in Visual Studio is still, as of writing, better for WinForms than for WPF, where you will have to do more of the work yourself with WPF.

Now back to WPF, it is a combination of XAML (markup) and C#/VB.NET/any other .NET language. XAML, which stands for eXtensible Application Markup Language, is Microsoft's variant of XML for describing a GUI.

So let get started with very basic and most popular example "Hello, World!".

In Visual Studio, click on New project from the File menu. On the left, focus on C# and select  Windows from tree. Now from right panel you can see "WPF Application" template. Select a WPF Application and named the project as "HelloWorld", then press the Ok button.

Now once project is created successfully, open the "MainWindow.xaml". MainWindow.xaml is the primary window of this project whenever you run the application it will show there unless you change this to another window.

Default XAML code of MainWindow.xaml file will look like this:


<Window x:Class="HelloWorld.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>

    </Grid>
</Window>

Here we want to display the "Hello, World!" text whenever application load, so place the text there in window as:

<Window x:Class="HelloWorld.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Main Window" Height="350" Width="525">
    <Grid>
        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="72">
            Hello, World!
        </TextBlock>
    </Grid>
</Window>
Try running the application now (select Debug -> Start debugging or press F5) and see the beautiful result of your hard work - your first WPF application. 

In future, I'll post the more tutorials related to WPF and those will help you to learn the WPF quickly. Let me know if any query remains. Cheers!

No comments:

Post a Comment