Introduction to WPF applications with C#

What is WPF:

WPF (Windows Presentation Foundation) is a new framework introduced with .NET Framework 3.0 for developing graphical user interfaces. WPF combines application UIs, 2D graphics, 3D graphics, documents and multimedia into one single framework. Its vector based rendering engine uses hardware acceleration of modern graphic cards. This makes the UI faster, scalable and resolution independent.

WPF vs Windows Forms:

There is no meaning to say that WPF is better than windows forms or vice versa. So when do you choose the one or the other?
You choose Windows Forms because:
  • It's been around for a long time and you have a large control supply that you can use.
  • There are a lot of good resources on Windows Forms to learn from and to get new controls from.
You choose WPF because:
  • You can make richer UI, nowadays it's all about the user experience.
  • You want to have full control of your control's design.
  • You want rich, data-driven applications.
  • You want hardware accelerated UI.

What is XAML:

According to the definition, XAML is an XML based declarative markup language for specifying and setting the characteristics of classes. In other words, XAML is a language used by WPF, Silverlight or any other application which can declare classes by itself. So, you can declare a variable, define the properties of any class and directly use it in your application. The XAML parser will automatically parse and create the actual object while it renders the application.
XAML is generally used to define layout of UI, its elements and objects for static and visual aspect. We cannot define flow of a program using XAML. So even though there are large capabilities of XAML, it is actually not a programming language, rather it is used to design UI of the application. Thus XAML employs other programming languages like C#, VB.NET, etc. to define the logic in code behind.
ExpressionBuilder is the best tool to generate XAML.

Create your first WPF application:

Like Windows Forms applications, we can create WPF applications by drag and drop controls from ToolBox to Main Window. Also the Visual Studio has a window that contains XAML at the bottom. So let's create a simple WPF application that displays "Hello" + content of textBox in a label:
First, choose WPF application from New Project window.

Introduction to WPF applicaions with C#

Then, add a textBox, a button and a label to the Main Window and change their name property to textBox1, button1, label1 like below:

Introduction to WPF applicaions with C#

Finally, double click on the button to handle click event of it, and add this code:

private void Button1_Click(object sender, RoutedEventArgs e)
{
    label1.Content = "Hello   " + TextBox1.Text;
}

This is the result of running this application:

Introduction to WPF applicaions with C#

References:

How to: Create a C# WPF Application
WPF Tutorial : Beginning
Introduction to WPF applications with C# Introduction to WPF applications with C# Reviewed by Bloggeur DZ on 03:49 Rating: 5

Aucun commentaire:

Fourni par Blogger.