Tuesday, March 29, 2011

WPF - Windows Presentation Foundation

WPF Overview:

The Windows Presentation Foundation is Microsoft's next generation UI framework to create applications with a rich user experience. It is part of the .NET framework 3.0 and higher.

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.

The followinig illustration gives you an overview of the main new features of WPF

http://www.wpftutorial.net/images/wpfMainFeatures.png

Separation of Appearance and Behavior

WPF separates the appearance of an user interface from its behavior. 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. The two parts are tied together by databinding, events and commands. The separation of appearance and behavior brings the following benefits:

  • Appearance and behaviour are loosely coupled
  • Designers and developers can work on separate models.
  • Graphical design tools can work on simple XML documents instead of parsing code.

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 - if just gets crispier. Since WPF builds on a vector based rendering engine it's incredibly easy to build scaleable user interfaces.

Built-In Support for Graphics and Animation :

WPF applications as being rendered within DirectX environment, it has major support of graphics and animation capabilities. A separate sets of classes are there which specifically deals with animation effects and graphics. The graphics that you draw over the screen is also Vector based and are object oriented. That means, when you draw a rectangle in WPF application, you can easily remove that from the screen as rectangle is actually an object which you always have hold on. On traditional Windows based application, once you draw a rectangle, you cant select that individually.  Thus programming approach in case of WPF is completely different and more sophisticated than traditional graphics approach. We will discuss graphics and animation in more detail in later section of the article.


Redefine Styles and Control Templates :

In addition to graphics and animation capabilities, WPF also comes with a huge flexibility to define the styles and ControlTemplates. Style based technique as you might come across with CSS are a set of definitions which defines how the controls will look like when it is rendered on the screen. In case of traditional windows applications, styles are tightly coupled with each controls, so that you need to define color, style etc for each individual control to make it look differently. In case of WPF, Styles are completely separated from the UIElement. Once you define a style, you can change the look and feel of any control by just putting the style on the element.

Most of the UIElements that we generally deal with is actually made using more than one individual elements. WPF introduces a new concept of Templates, which you might use to redefine the whole control itself. Say for instance, you have a CheckBox, which has a Rectangle in it and a ContentPresenter (one where the caption of the TextBox appears). Thus you can redefine your checkbox and put a ToggleButton inside it, so that the check will appear on the ToggleButton rather than on the Rectangle. This is very interesting. We will look into more detail on Styles and ControlTemplates in later section of the article.


Advantages

1    WPF has the ability to separate UI from logic effectively.

2    WPF has an inbuilt storyboarding feature and animation models.

3    Data binding is very much better than with the WinForms application.

4    WPF allows you to handle large data sets because of it has a built-in 'user interface virtualisation' feature.

5    WPF offers data and control templates that provide flexible modelling of UI on data models.

6    WPF supports 3D graphics to make UIs look really special.

7    WPF supports various types of media such as video, 3D content and animations.

8    Even if Visual Studio designer is not available, you can code in XAML (Extensible Application Mark-up Language).

9    WPF has the ability to use business objects in UI declaratively. In other words, a developer can build and package the business objects in an assembly and then one can instantiate the instances of them in XAML and bind them to controls directly. Not a single line of code is required for this at UI.

10    WPF enables programmers to develop much of the application in a completely declarative model, using XAML.

11    WPF has far more effective control creation and reusability features that provide greater flexibility to developers in building UIs.


WPF Architecture


For every new technology, it is very essential to have clear idea about its architecture. So before beginning your application you must grab a few concepts. If you dont like to know WPF in detail, please skip this section. As mentioned earlier, WPF is actually a set of assemblies that build up the entire framework. These assemblies can be categorized as

  • Managed Layer
  • UnManaged Layer
  • Core API

Managed Layer : Managed layer of WPF is built using a number of assemblies. These assemblies build up the WPF framework, communicates with lower level unmanaged API to render its content. The few assemblies that comprise the WPF framework are :

  1. PresentationFramework.dll : Creates the top level elements like layout panels, controls, windows, styles etc.
  2. PresentationCore.dll : It holds base types such as UIElement, Visual from which all shapes and controls are Derived in PresentationFramework.dll. 
  3. WindowsBase.dll : They hold even more basic elements which are capable to be used outside the WPF environment like Dispatcher object, Dependency Objects. I will discuss each of them later.


Unmanaged Layer (milcore.dll):  The unmanaged layer of WPF is called milcore or Media Integration Library Core. It basically translates the WPF higher level objects like layout panels, buttons, animation etc into textures that Direct3D expects.  It is the main rendering engine of WPF.

WindowsCodecs.dll : This is another low level API which is used for imaging support in WPF applications. WindowsCodecs.dll comprises of a number of codecs which encodes / decodes images into vector graphics that would be rendered into WPF screen.

Direct3D : It is the low level API in which the graphics of WPF is rendered.

User32 : It is the primary core api which every program uses. It actually manages memory and process separation.

GDI & Device Drivers : GDI and Device Drivers are specific to the operating system which is also used from the application to access low level APIs.

WPF architecture

 


-- Jagadeesan Kandasamy

Wednesday, March 16, 2011

How to download MSDN Magazine

Download MSDN Magazines using the below link:

http://msdn.microsoft.com/en-us/magazine/ee412244.aspx
 

LINQ : Language Integrated Query - A Simple Example

class Order

{

    private int _OrderID;

    private int _CustomerID;

    private double _Cost;

    public int OrderID

    {

        get { return _OrderID; }

        set { _OrderID = value; }

    }

    public int CustomerID

    {

        get { return _CustomerID; }

        set { _CustomerID = value; }

    }

    public double Cost

    {

        get { return _Cost; }

        set { _Cost = value; }

    }

}

 

class Program

{

    static void Main(string[] args)

    {

        // Set up some test orders.

        var Orders = new List<Order> {

                         new Order {

                             OrderID = 1,

                             CustomerID = 84,

                             Cost = 159.12

                         },

                         new Order {

                             OrderID = 2,

                             CustomerID = 7,

                             Cost = 18.50

                         },

                         new Order {

                             OrderID = 3,

                             CustomerID = 84,

                             Cost = 2.89

                         }

                     };

        // Linq query.

        var Found = from o in Orders

                    where o.CustomerID == 84

                    select o.Cost;

       

        // Display results.

        foreach (var Result in Found)

            Console.WriteLine("Cost: " + Result.ToString());

    }

}

 

 

 

The output of running this program is:

Cost: 159.12

Cost: 2.89