Wednesday 23 May 2012

FAQ's - WPF (Windows Presentation Foundation)

WPF is Windows Presentation Foundation Framework that shipped as part of the .NET Framework 3.0 . First it has shipped with Windows Vista OS in built, and is re-distributable for Windows XP or Windows Server 2008.
WPF is Object Oriented , XAML based.
  • It Uses DirectX Engine for rendering GUI
  • It do not use GDI 32 programming at all as opposed to Win32 applications
  • It do not require more time or cost for graphics, drawing or animation programming
  • It is Easy to change resolution unlike win32 application
  • It is XAML Friendly.
  • It has .NET API that has integrated to XAML (Xtensible Application Markup Language)
  • Easily select the Controls unlike Win32 Controls.
  • It has the ability to create 3D graphics in windows apps
  • It contains Separate API for graphics and animation
  • Most powerful Windows UI Framework
It is used for the
  • For the better User Interface & the  Design
  • Customization of Controls
  • Integrating Flash, Direct, Win32, Windows Forms
  • For Generic consistency professional look (Using Styles like CSS in Web)WPF Provides an unified Model for producing high end GUI easily using normal XML syntax  to render output on graphics devices than using GDI components.

  • XAML stands for 'Xtensible Application Markup Language'
  • It is Declarative Markup Language.
  • It simplifies creating a UI for a .NET Framework application.
  • It can create visible UI elements in XAML , and then separate the UI definition from the run-time logic by using code-behind files. This joined to the markup through partial class definitions. XAML represents the instantiation of objects in a special set of backing types defined in assemblies.
  • It enables a workflow where separate parties can work on the UI and the logic of an application, using different tools. ex: Expression Blend and Visual Studio
  • It is Visual designer to create User friendly UI
  • XAML is not dependent on WPF or WPF is not dependent on XAML. WPF Designed to be XAML Friendly


  • Every UIElement in WPF is derived from DispatcherObject which defines a property called Dispatcher that points to the UI thread.
  • A dispatcher is used to raise calls on another thread.
  • Dispatcher can also be called as a  class that handles thread affinity.
Thread Affinity
As mentioned  above Dispatcher thread holds all UI elements. This is called thread affinity. All Most all of the WPF elements have thread affinity. If we have a background thread working, and if at all we need to update the UI thread, then a dispatcher is definitely required for this. Thus from any other thread, if at all we want to access UI component, we need to do it using Dispatcher thread.
DispatcherObject class contains two methods .
1. CheckAccess() : This method provides access to current dispatcher that an object is tied to . This returns a Boolean value as true if the current thread has access to use the object and returns false if the current thread can not use the object.
2. VerifyAccess() : The purpose of this method is to Verify  if the thread has access to the object. If the thread does not have access to the object, an exception is thrown.
If we make a call to a DispatcherObject from a non-UI thread, it will throw an exception. So if we are working on a non-UI thread, we need to update DispatcherObjects by using dispatcher.
The below picture represents object hierarchy in WPF.  It gets inherited from Object class.
WPF-object-heirarchy
Dispatcher object  represents an object associated with a System.Threading.Dispatcher.
As conclusion, we can say that, it is actually an important message loop through which all elements are managed.


  • Most of the WPF classes derive from Dependency Object. All UI Elements are derived from Dependency Object.
  • The Dependency Object class gives all the functionality of the WPF dependency property system.
  • Dependency Object defines WPF Property System.
  • System.Windows.DependencyObject is used to create Dependency Property.
  • Dependency properties are the  special property in WPF that are used to support many features in WPF.
The following are steps to create DP
1. Creating Dependency Property
  Create a Property that gets inherited from dependency object
2. Register the Property, This should be public static readonly. Visual Studio 2010 gives code snippet for this (propdp). This is the property that us used to identify target property while databinding .
3. Use Property accesses' as GetValue SetValue , GetValue is used to Read the property and SetValue is used to write the property .
Below is the example for this
01.class Foo : DependencyObject
02.{
03.public int MyCustomProperty
04.{
05.get return (int)GetValue(MyCustomPropertyProperty);
06.}
07.set { SetValue(MyCustomPropertyProperty, value);
08.}
09.}
10. 
11.//Using a DependencyProperty as the backing store for MyCustomProperty.
1.//This enables //animation, styling, binding, etc...//Registering the property
2. 
3.public static readonly DependencyProperty MyCustomPropertyProperty =
4. 
5.DependencyProperty.Register("MyCustomProperty"typeof(int), typeof(Foo), new UIPropertyMetadata(0));
6.}
While registering we need to pass the parameters as Name, Type, Type that owns property and the metadata that contains property  .
Meta Data Contains Default Value of properties information.


WPF has Presentation Framework, Presentation Core & Composition Engine (MIL).
Each one sit on the top of the other in the following order
1. Composition Engine
2. Presentation Core &
3. Presentation Framework
WPF uses 3D pipeline to render everything.  It uses 3d hardware and graphics card while rendering, even to render 2D graphics.
clip_image002[4]
1. Composition Engine Is also called MIL, Media Integration Layer. It takes Bitmaps, Vectors, and media render them to the DirectX. MIL sits as unmanaged layer to minimize CPU usage
2. Presentation Core
It provides .NET API that uses rendering services for the MIL. During graphics programming we work with Core API.
3. Presentation Framework
Most of the important WPF elements are in Presentation Framework. Provides high-level services like layout, data binding, command handling.


Event Routing or A Routed Event is a type of event that is has the ability to invoke handlers not only on the object that raised the event but also on multiple listeners in an element tree. 
There are 3 types of events in WPF
1. Bubbling
2. Tunneling
3. Direct
Event Bubbling
It starts from Target and then bubbles up to the root  . Imagine that we have  Stack Panel inside Window , a rectangle control  inside the StackPanel.   If you we fire a bubbled event on a  rectangle ,  it will be first fired on the rectangle , then  StackPanel  and then on window .
  • Most routed events use the bubbling routing .
  • Bubbling routed events are  used to report input changes from  other UI elements.
  • All Main events Bubble.
ex: MouseDown
Event Tunneling
The order of events firing for the controls  in Event Tunneling is exactly opposite to the event Bubbling.  Do not get confused of the concept.  Let me make it very simple and clear. Imagine that we have  Stack Panel inside Window , a rectangle control  inside the StackPanel.   If we fire  a tunneled event on a Window ,  it will be first fired on the Window, then  StackPanel  and then on rectangle .
  •   All the Preview events tunnels down
ex: PreviewMouseDown
Direct Routing This is similar to the "routing" that Windows Forms uses for events .These are also called  CLR events which we all are used to in .NET environment. Only that event fires.
ex: Mouse Enter
Advantages in RealTime
Routed events support a class handling mechanism in which the class  specifies static methods that  handles routed events before  registered instance handlers can access them. This is  concept is  useful in control design as custom class can enforce event-driven class behaviors .
In the next post I will discuss practical implementation and how to use event routing in real time applications


7. What is difference between Event Bubbling Vs Event Tunneling? When to apply what?

WPF is a huge shift from WinForms . If you are planning to migrate from WinForms to WPF these are the benefits .
Benefits of WPF  over Winforms
  • Rich UI can be made very easily especially for the Windows Applications.
  • WPF Supports animations and Special Effects easily through Graphics API.
  • It has DPI settings for the application in built.
  • DataBinding makes WPF more easier to bind data from database dynamically
  • Templating (Control, Data and Item) makes extending controls or adjust controls  easily.
  • We can  add shapes, lines, and arbitrary drawings to the application using Vs IDE itself . Every component of these can be data-bound and animated, or controlled by code.
  • There are lot of things added in Graphics part colors, gradient brushes ,fancy fonts, rotations , tile brushes . Now graphically you can do anything that you want in Windows.
  • Customize the Controls, extends Custom Controls, User Controls .
  • Easy Maintainability .
Considerations – WPF over Winforms
  • Definitely costlier than Winforms but not too much costlier.
  • WPF stores its data more efficiently hence individual objects will be small, but there tend to be more objects in WPF than in WinForms .This would require more RAM than Windows Application.
  • CPU utilization will go up compared to WinForms   WPF objects onscreen takes more CPU as normal WinForms rendering (again depending on the requirement) .
When to Choose What ?
If application spends most of its time updating the screen, WPF is not the right case to choose .  ex: Applications that are written directly to DirectX.


9. What are different types of Panels in WPF ? Explain them?
10. What is difference between StackPanel, DockPanel, WrapPanel and Grid?
11. What are Primitive Controls and Look Less Controls?
12. What are different important components to know in WPF? Explain them?
13. Why do you think WPF has more power?
14. What is difference between WPF and Web Applications which do you prefer? When to choose what?
15. Is Silverlight part of WPF  or subset of WPF? What is difference between WPF and Silverlight?
16. What is the class name from which all WPF objects are derived from?
17. How did you create Dependency Object?
18. How did you implement Dependency Property?
Prism is a Framework for developing Composite or Complex applications specific to WPF or Silverlight or Windows Phone.
It uses modularity; It allows to break application into pieces can be called as Modules.
It uses design patterns like MVVM, Command Patterns, Dependency Injection (DI), and Inversion of Control (IC), Separation of Concerns to achieve loosely coupling.

Advantages

Reusability: It allows building component in one framework (WPF) and reusing it in other platforms (Silverlight).
Extensibility: Due to its design patterns nature new functionality that is to be added is easily extensible for future purpose.
Flexibility: PRISM is flexible to develop large complex applications
Team Collaboration: Simultaneous module development is possible and Multiple teams can work on different modules simultaneously.
Fault Tolerance : It allows components to be thoroughly and completely tested thus results in error free application development of better quality. 
Maintainability: The large scale complex applications developed using PRISM are maintenance friendly
Modularity: As everything is break down into modules, prism supports modularity due to this PRISM is flexible, extensible .

Components of PRISM

  1. Shell : Template that Defines structure of the UI. Shell contains several regions.
  2. Regions: Regions are used to specify specific portion of shell as elements to inject view at runtime
  3. Modules: These are major functional areas of the application. Each module need to be independent of other,
  4. Views: Modules contains number of views. Views in Prism are built using MVVM design pattern.
  5. Boot-Strapper: This component is Responsible for Creating Shell and initializing application. 
Well. Now I got some basic idea of the PRISM How to get it ?
You can download the prism framework from patterns and practices


10. How did you install and implement PRISM in real-time applications?

11.What are WPF Commands and Events

The following are important features of jQuery.
Extensibility
The jQuery Framework is easily extensible. 
DOM elements manipulations
It easily handles DOM element selections functions, manipulations, traversal and modification.
Event Handling
We can implement Event Handling at client side.
CSS manipulation
It provides the flexibility for CSS manipulation. We can apply or change CSS dynamically using jQuery.
Animations
Advanced effects and animations are possible using jQuery.
Cross Browser Compatibility
It is Consistent across all browsers.
Easy Plugins
Number of plug-in or widgets available to achieve certain functionality like paging, sorting, drag , accordion etc.




No comments:

Post a Comment