WPF (Windows Presentation Foundation) – Introduction and Sample Code in C# .NET
Introduction
Windows Presentation Foundation
(WPF) is Microsoft’s latest technology for creating graphical user interfaces,
whether they consist of plain forms, document-centric windows, animated
cartoons, videos, immersive 3D environments, or all of the above!
This
is a technology that makes it easier than ever to create a broad range of
applications.
It removes the limitation of GDI + and User yet provide the kind of productivity
that people enjoy with frameworks like window forms.
WPF is a major component of the
.NET Framework, starting with version 3.0.
FIGURE: The technologies in the .NET Framework
3.0.
Creating UI using WPF
Developing WPF UI is
much more like building web UI than native Windows development.
How to define UI in WPF
Defining WPF UI with XAML
XAML (pronounced
zammel) is an acronym for eXtensible
Application
Markup
Language and is an
XML-based specification for defining UI Although XAML was created specifically
for WPF.
This is technically called a
declarative
programming model. WPF
will take that definition and convert it into real elements on the screen.
For Example:
//XAML Code
<Page
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<StackPanel>
<Label>MindStick</Label>
</StackPanel>
</Page>
XAML documents are
strict
XML. Every tag (for
example, StackPanel
or Label) and attribute (for
example, Margin
or
FontSize) must correspond to a valid .NET type or property. If a tag has
the wrong case or an unknown attribute is used, the resulting XAML won’t work.
Defining WPF UI through code
You don’t have to use
XAML to define UI elements. You can write code to define your UI, much as you
did with Windows Forms. This is the classic
imperative
programming model.
The following
code does exactly the same thing as the XAML in above.
For Example:
//Using Code
Window window1;
Page page1;
StackPanel stackPanel1;
Label label1;
public Procedural()
{
window1 = new Window();
page1 = new Page();
stackPanel1 = new StackPanel();
label1 = new Label();
label1.Content = "Hello, World!";
stackPanel1.Children.Add(label1);
page1.Content = stackPanel1;
window1.Content = page1;
}
Simple application on WPF
Step1: we open the
visual studio and choose the new project as in figure and we select the WPF
application and right above corner we check the version 3.5.
Select the WPF Application
Step 2: Adding a button control from toolbox and double click on button and
write the code
private
void button1_Click(object
sender, RoutedEventArgs e)
{
MessageBox.Show("This is my first wpf application ");
}
Adding button from toolbox
Step 3: Running the application
This is the simple demonstration of WPF application
|