It was really a pleasure to share my knowledge through blog on .NET Terminology, C#, Multithreading and WCF. Now, it’s time to learn WPF which is new, innovative, flexible and next generation UI for Desktop /Smart-Client application.We will cover Why, What and How WPF is useful and what are features involved including Xaml, DirectX, Hardware Acceleration etc.
Why WPF even we had User32, GDI, GDI+ and DirectX?
First let’s try to understand how display technology has evolved in Microsoft technology.
User32:- User32 decides which goes where on the screen and provide the windows look and feel for buttons, textboxes and other UI elements. User32 lacked drawing capabilities.
GDI (Graphics device interface):- GDI not only provided drawing capabilities but also provided a high level of abstraction on the hardware display. In other words, it encapsulates all complexities of hardware in the GDI API.
GDI+:- GDI+ is extension of GDI and provides extra functionalities like jpg and PNG support, gradient shading and anti-aliasing.
DirectX:- GDI and GDI+ does not support animation and hardware acceleration. This came as a biggest disadvantage for game developers. To answer and server game developers Microsoft developed DirectX. DirectX exploited hardware acceleration, had support for 3D, full color graphics, media streaming facility and lot more.
WPF:- Microsoft almost had 3 to 4 API's for display technologies , so why a need for one more display technology. DirectX had this excellent feature of using hardware acceleration. Microsoft wanted to develop UI elements like textboxes, button, grids etc using the DirectX technology by which they can exploit the hardware acceleration feature. As WPF stands on the top of DirectX you can not only build simple UI elements but also go one step further and develop special UI elements like Grid, FlowDocument, and Ellipse. Oh yes you can go one more step further and build animations. In simple words WPF uses directX internally. DirectX talks with drivers and renders the content.
Tricky question
Does that mean WPF has replaced DirectX?
No, WPF does not replace DirectX due to following reason
1. WPF used is for light animation while DirectX used for game development
2. The video performance of directX is still many times higher than WPF API because DirectX direct
interact with the video graphics, driver and with kernel
3. WPF is a wrapper which is built over DirectX.
What is WPF (Windows Presentation Foundation)?
It’s next generation UI framework to create applications with a rich user experience. It is part of the .NET framework 3.0 and higher.
It combines following things into one single framework
1. Has all equivalent common user controls like label, textbox, buttons etc.
2. Fixed and flow format documents
3. Have all of the capabilities of HTML and Flash
4. 2D and 3D vector graphics
5. Animation
6. Multimedia
7. Data binding
What are the features of WPF?
1. Device Independent Pixel (DPI)
2. Built in support for graphics and animation
3. Redefines style and control templates
4. Resource bases approach for every control
5. New property system and binding capabilities
What operating systems support WPF?
Following are the operating systems that support WPF
a) Windows7 b) Windows Vista c) Windows XP Service Pack 2 or later
How WPF is better than Windows Form
1. WPF depends on DirectX for its UI rendering while Windows Forms completely depends on GDI/GDI+ (sometimes User32)
2. WPF offer hardware acceleration while Windows Forms not.
3. Windows form flickers while WPF not Note: Its vector based rendering engine uses hardware acceleration of modern graphic cards. This makes the UI faster, scalable and resolution independent.
How does hardware acceleration work with WPF?
Hardware acceleration is a process in which we use hardware to perform some functions rather than performing those functions using the software which is running in the CPU.
WPF API first detects the level of hardware acceleration using parameters like RAM of video card , per pixel value etc. Depending on that it either uses Tier 0, Tier 1 or Tier 2 rendering mode.
Tier 0:- If the video card does not support hardware acceleration then WPF uses Tier 0 rendering mode. In other words it uses software acceleration. This corresponds to working of DirectX version less than 7.0.
Tier 1:- If the video card supports partial hardware acceleration then WPF uses Tier 1 rendering mode. This corresponds to working of DirectX version between 7.0 and 9.0.
Tier 2:- If the video card supports hardware acceleration then WPF uses Tier 2 rendering mode. This corresponds to working of DirectX version equal or greater than 9.0.
Can you explain the overall architecture of WPF?
WPF architecture has three major sections presentation core, presentation framework and milcore.
Presentation core :-
1. Provide low level API exposed by WPF providing features for 2D, 3D, geometry etc.
2. holds base types, such as UIElement and Visual from which all shapes and controls derive.
Presentation framework:
1. Provide high level features like windows, panels, application controls , layouts , content etc which helps you to build up your application.
2. It also implements higher-level programming abstractions such as styles. 3. Most of the classes used come from this assembly.
Mil core(media integration library):-
1. This section is an unmanaged code because it acts like a bridge between WPF managed and DirectX / User32 unmanaged API.
2. milcore.dll manages the display state.
3. It paints the appropriate portions of the window as it is dragged around, covered and uncovered, minimized and restored, and so on.
These *.dll is also called as the engine of managed graphics
What are the core WPF assembly?
The core WPF assemblies are,
WindowsBase.dll:- It defines the core types constituting the infrastructure of WPF API. PresentationCore.dll:- This is a low level API exposed by WPF providing features for 2D , 3D , geometry etc. , defines numerous types constituting foundation of WPF GUI layer.
PresentationFoundation.dll:- It defines WPF control types, animation & multimedia support, data binding suport and other WPF controls.
Note: Besides these three libraries WPF also uses an unmanaged binary called milcore.dll which acts as a bridge between WPF assemblies and DirectX runtime layer.
Can explain the different elements involved in WPF application practically?
These are the elements are included with while creating an WPF application
1. Windows1.xaml file - Windows UI
2. Windows1. xaml.cs - Writing C# code in backend
3. App. xaml and App. xaml.cs are the entry point files.
Other components you might expect
4. Models
5. ViewModels
6. Controls
7. ResourceDictionaries
Where is Entry point in WPF application?
When you create a WPF Windows Application in Visual Studio, the generated project has no Main method, yet it still runs as expected! In fact, even attempting to add a Main method gives a compilation error telling you that it is already defined. Application is special-cased when it is compiled from XAML, because Visual Studio assigns the XAML file a Build Action of ApplicationDefinition. This causes a Main method to be
autogenerated.
Can you explain how we can separate code and XAML?
XAML file and the code completely detached from the XAML presentation. In order to associate a class with XAML file you need to specify the x: Class attribute. Any event specified on the XAML object can be connected by defining a method with sender and event values. You can see from the above code snippet we have linked the MyClickEvent to an event in the behind code.
How can we access XAML objects in behind code?
To access XAML objects in behind code you just need to define them with the same name as given in the XAML document. For instance in the below code snippet we named the object as objtext and the object is defined with the same name in the behind code.
What is the WPF Unit?
The WPF Unit is measured in DPI. First we need to understand about DPI and PX (i.e. Device Independent Pixels & Physical Pixels)
1 DPI = 1/96 inch
1 PX = 1/DPI - Depends on screen type (Default system setting is 96 DPI)
Note: If the DPI increases, the number of physical pixels which compose one device independent pixel also increases. Hence, objects that have their sizes specified in device independent pixels get larger as the system DPI increases
What is XAML?
XAML (pronounced as Zammel) is a declarative XML-based language developed by Microsoft. XAML is the language behind the visual presentation of an application that you develop in Microsoft Expression Blend, just as HTML is the language behind the visual presentation of a Web page. If we are creating an application in Expression Blend means writing XAML code, either by hand or visually by working in the Design view of Expression Blend.
Following capability XAML has
1. Can define object and properties in XML
2. XAML document is loaded by a XAML parser. XAML parser instantiates objects and set their properties and there relation in between them.
3. Using XAML you can create any kind of objects that means graphical or non-graphical.
4. WPF parses the XAML document and instantiates the objects and creates the relation as defined by XAML
What are the advantages of XAML?
1. XAML code is short and very clear to read
2. Seperation of designer code(XAML) and UI logic allow it to very clearly have separation between developer and designer work
3. Graphical design tools like blend expression uses xaml source
Are XAML file compiled or built on runtime?
XAML files are usually compiled rather than parsing on runtime. But it also supports parsing during runtime. When we build a XAML based project, you will see it creates g.cs extension in obi\Debug folder. Therefore, for every XAMl file you will find a g.cs file. For instance, a Shiv.XAML will have Shiv.g.cs file in obj\Debug folder. In short, in runtime you actually do not see the XAML file. But if you want to do runtime, parsing of XAML file it also allows that to be done.
So is XAML meant only for WPF?
1. No, XAML is not meant only for WPF.
2. XAML is a XML-based language and it had various variants.
3. WPF XAML is used to describe WPF content, such as WPF objects, controls and documents.
4. In WPF XAML we also have XPS XAML which defines an XML representation of electronic documents.
5. Silverlight XAML is a subset of WPF XAML meant for Silverlight applications. Silverlight is a cross-platform browser plug-in which helps us to create rich web content with 2-dimensional graphics, animation, and audio and video.
6. WWF XAML helps us to describe Windows Workflow Foundation content. WWF engine then uses this XAML and invokes workflow accordingly.
What are the similarities and differences between WPF and Silverlight?
Similarities between WPF and Silverlight
1. WPF and Silverlight both use XAML.
2. WPF and Silverlight share same syntax, code and libraries.
Difference between WPF and Silverlight
1. WPF is tool for writing full-featured desktop applications that run on Windows while Silverlight is for web application.
2. WPF competes with Windows Forms only for writing desktop applications while Silverlight competes with Flash, Java web apps etc.
When should we go with WPF instead of Adobe Flash?
1. For creating rich web content, Flash is currently the most popular option because of its ubiquity.You can put Flash-based content on a website with confidence that the overwhelming majority of visitors already have the necessary player installed. (And if they don’t, it’s a very quick download.)
2. WPF applications can also run within a web browser. WPF has the advantage of better development tools and programming model, a richer feature set, robust control reuse, broad programming language support, and full access to the underlying platform (when security permits). But viewing such content requires Windows and the .NET Framework 3.0 (installed by default on Windows Vista or later).
What is the Class Hierarchy in Windows Presentation Foundation?
There are ten important namespaces / classes in WPF, will explain with hierarchy
1. System.Threading.DispatcherObject:
1. All WPF objects derive from the DispatcherObject. WPF works on STA model i.e. Single Threading Apartment Model.
2. The main duty of this object is to handle concurrency and threading. When any message like mouse clicks, button clicks etc are initiated they are sent to the DispatcherObject who verifies whether code is running on the correct thread.
2. System.Windows.DependencyObject
1. When WPF was designed property based architecture was considered. In other words rather than using methods, functions and events object behavior will interact using properties.
2. For now we will only restrict ourselves to this definition.
3. System.Windows.Media.Visual
1. Visual class is a drawing object which abstracts drawing instructions, how drawing should be drawn like clipping, opacity and other functionalities.
2. Visual class also acts like a bridge between unmanaged MilCore.dll and WPF managed classes. When any class derived from visual it can be displayed on windows. If you want to create your own customized user interface then you can program using visual objects.
4. System.Windows.UIElement: UIElement handled three important aspects layout, input and events.
5. System.Windows.FrameworkElement
1. FrameWorkElement uses the foundation set by UIElement. It adds key properties like HorizontalAlignment , VerticalAlignment , margins etc.
2. It Introduces data binding and data templates, styles and animation.
3. Framework element is divided into three section Shape, Control and Panel
i. System.Windows.Shapes.Shape: This class helps us to create basic shapes such as Rectangle,Polygon, Ellipse, Line, and Path.
ii. System.Windows.Controls.Control:
a) This class has controls like TextBox,Button,ListBox etc. It adds some extra properties like font,foreground and background colors.
b) Controls further divided into two sections I. ContentControl II. ItemControl
1. System.Windows.Controls.ContentControl: It holds a single piece of content. This can start from a simple label and go down to a unit level of string in a layout panel using shapes
2. System.Windows.Controls.ItemsControl: This is the base class for all controls that show a collection of items, such as the ListBox and TreeView.
iii. System.Windows.Controls.Panel
a) This class is used for all layout containers—elements that can contain one or more children and arrange them as per specific layout rules.
b) These containers are the foundation of the WPF layout system, and using them is the key to arranging your content in the most attractive, flexible way possible.
Describe the purpose of the following classes and how they relate – DispatcherObject, DependencyObject, Visual, UIElement, FrameworkElement
1. DispatcherObject – keeps objects on the same thread
2. DependencyObject – provides functionality for dependencies properties Following methods are
present: 1. GetValue() 2. Set Value() 3. Clear Value()
3. Visual – provides functionality for all elements to be drawn on the screen
4. UIElement – adds event handling
5. FrameworkElement – provides functionality for DataBinding, Styles, Triggers
Tricky Question
1. What is the use of "System.Windows.Markup" namespace in WPF?
The System.Windows.Markup namespace provides some helper classes for XAML code.
2. Which namespace provide classes for integration with WPF and Win32?
The "System.Windows.Interop" namespace provides classes for integration of WPF with Win32.
3. Which class of the WPF is the base class of all the user-interactive elements?
Control class is the base class of all the user-interactive elements in WPF.
4. Which class is the base class of all the visual elements of WPF?
Window Visual is the base class of all the visual elements of WPF.
5. Control class of WPF is derived from which class?
Control class of WPF is derived from FrameworkElement class.
6. The core classes for User Interface is located in which namespace?
System.Windows.Shapes namespace defines UI classes like Line, Rectangle,etc.
7. Which namespace provide classes to work with images, sound, video, etc?
The System.Windows.Media namespace provide classes to work with images, sound, video, etc.
8. What is the use of System.Windows.Navigation namespace in WPF?
System.Windows.Navigation namespace contains different classes for navigation between windows.
9. Which namespace is used to work with 3D in WPF?
The System.Windows.Media.Medi3D namespace is used to work with 3D.
10. Which of these is not a class defined by System.Windows.Shapes namespace?
Polypath is not the classs.Ellipse,Polygon,Line are the classes defined by this namespace
References
1. http://www.wpftutorial.net
2. http://www.dotnetfunda.com
3. http://www.a2zdotnet.com
A well compiled set of essential questions for the niche tech like WPF. Hats off. Looking forward to see the subsequent parts of this ASAP. Doing a nice job for the community.
ReplyDelete