Introduction to .NET Framework 3.0/Windows Presentation Foundation

From Wikibooks, open books for an open world
< Introduction to .NET Framework 3.0
Jump to: navigation, search

Windows Presentation Foundation (codenamed:Avalon) is a new feature (a presentation platform) introduced with .NET Framework 3.0 which supports the creation of documents containing high-resolution pictures and graphics. WPF is also responsible for the integration of pictures and media in ordinary documents. At present, the component performing this function is Windows Forms.

What is Windows Presentation Foundation[edit]

A productive, unified approach to UI, media and documents to deliver unmatched user experience

WPF Infrastructure[edit]

The Presentation Foundation contains classes to create 'Print' and 'File Open' dialog boxes, script new windows, etc. It makes use of four specific classes most of the time:

  • UIElement
  • FrameworkElement
  • ContentElement
  • FrameworkContentElement

These classes help in building an effective and easy to use user interface. The WPF UI is usually in the form of a Element Tree. Element Trees used in WPF are of two types:

  • Logical Tree
  • Visual Tree

Element Trees are usually implemented in XAML by dragging and dropping and creating relationships between the concerned elements. In code, they depend on how we give property values for properties.

The logical tree involves creating a tree structure by adding a listbox tag and list items inside the tag. In the case of a DockPanel we use Children property.

The visual tree is mainly used by programmers who want to meddle with the underlying low-level components of the program.

Combination of Markup and Programming[edit]

While the usage of a programming language as C# is best recommended for programming logic, the usage of markup languages is recommended for texts and UIs.

In earlier versions of the .NET framework, the designer part was separate from the code. However, in .NET 3.0, both the designer part programmed in XAML and the logic programmed in C# are included in the same code.

XAML File
<Canvas ID="root“ xmlns="http://schemas.microsoft.com/2003/xaml“>
        <Button Click="Button_Click">Click Me!</Button>
</Canvas>

C# code-behind file 
using…
namespace Button
{
        public partial class Default : Panel
        {     // Event handler
                   void Button_Click (object sender, System.Windows.Controls.ClickEventArgs e)
               {    btn1.Background = System.Windows.Media.Brushes.Red;  }
        }
}

Graphics[edit]

Windows Presentation Foundation enables the user to create graphic applications of high-quality. You could add existing or create custom buttons using <button> tag, create docked panels, 2D and 3D graphical images, use color picker controls, create or modify images, videos, create custom medi players, etc.

Drawing Object Model[edit]

The WPF graphics system offers a completely new programming style. With Win32-based UI technologies, if you want to draw customized graphics, you must write code that responds to repaint requests, drawing the details on demand. This code effectively paints directly to the screen. If that part of the screen becomes obscured by another window and then uncovered, Windows sends a message requesting that this be redrawn. If you want to change the appearance of your graphics, you need to instruct Windows to invalidate the relevant areas of the screen in order to initiate a repaint.

Richly Formatted Text[edit]

WPF also helps us create formatted text easily and hence rich formatting could be used. The contents are enclosed within a <textblock> tag.

User Events could be executed easily by using triggers which are specified using the <trigger> tag. Apart from this, there are many other new innovations in WPF.

Templates and Styles[edit]

There are two types of templates: Data Templates and Control Templates. Control Templates define the control and the visual structure of the control.

Syntax:

ControlTemplate x:Key=“MyButton” TargetType=“{x:Type Button}”>
    <Grid>
        <Ellipse />
        <ContentPresenter />
    </Grid>
</ControlTemplate>

On the contrary, data templates contain specifications about the data to be stored.


Syntax:


<DataTemplate DataType=“{x:Type PhotoObject}”>
    <Image Source=“{Binding FileName}” />
</ControlTemplate>

Styles are used to specify the syles of buttons used.

<Style x:Key=“MyStyle” TargetType=“{x:Type Button}”>
</Style>

Styles can also be based on other styles.

Style x:Key=“MyStyle” BasedOn=“{StaticResource BaseStyle}” />
    <Setter Property=“Background” Value=“Green” />
</Style>

Documents[edit]

WPF supports three kinds of documents

  • Fixed Documents
  • Flow Documents
  • XPS Documents


WPF has introduced new concepts which make the integration of text with media files easier. WPF also supports a variety of fonts which were not handled by earlier versions.

Fixed Documents[edit]

Fixed Documents are representative of WYSIWYG ("What you see is what you get") principle. Here, what you see on the screen indicates the actual layout that you will get on paper when the page is printed. Hence, even slight changes to the layout shall be critical.

Flow Documents[edit]

Flow Documents are those document formats which adjust their size, readability, dimensions, etc. depending on the size of the screen. Documents of this type are also supported by WPF.

XPS Documents[edit]

XPS documents are those which are exact representations of how the document shall look on paper. They resemble scanned images of the document itself.

Benefits[edit]

  • Ease of Use -- Learn ability, Performance, Reliability, Security, Optimized form factors, Legibility / Readability, Relevance / Contextualization
  • Richness -- Graphics & Media, Data Visualization, Higher Fidelity Information, Globalization, Accessibility, Hardware & Printing Integration.

References[edit]

  1. http://msdn2.microsoft.com/en-us/windowsvista/aa905016.aspx#Understanding%20Windows%20Presentation%20Foundation:%20Essential%20Concepts
  2. http://msdn2.microsoft.com/en-us/library/aa970268.aspx#Windows_Presentation_Foundation_Applications
  3. http://www.dotnet-u.com/