Showing posts with label WPF. Show all posts
Showing posts with label WPF. Show all posts

Monday, 28 January 2013

WPF Application Types

Following are the various WPF windows possible in .Net 4.5


WPF Window
Standard Windows Application which is normally the same old style Stand alone or some time we say Desktop based application.
WPF page
Kind of the Web type application. The behavior is some what as the Wizard Type of application where you can navigate between the windows and perform the task.
WPF User Control
User controls can contain other controls, resources, and animation timelines, just like a Windows Presentation Foundation (WPF) application. The only difference is that the root element is a UserControl instead of a Window or a Page
WPF Resource Dictionary
In Extensible Application Markup Language (XAML), the ResourceDictionary class is typically an implicit collection element that is the object element value of several Resources properties, when given in property element syntax.
One reason to use resources in your applications is to promote object reuse across your application. Object reuse provides consistency across the application. Object reuse also makes it very easy to change the application; since you only need to change the resources and all consumers will pick up the change.
WPF Custom Control
These are reusable type of application specific controls that are not present by default in the WPF user controls.
WPF Flow Document
This is the window to hold flow documents, for example pdf
WPF Page Function
PageFunction in a page = Dialog box in desktop application (without Page).
You can use a PageFunction every time you use a dialog box in a desktop application and that you want to develop a webnavigation-like behavior to your program.

WPF Splash Screen
Splash screen type of window.

Thursday, 30 August 2012

WPF Architecture


WPF is built of three layers:

Layer1

Managed Layer

·         PresentationFramework.dll -: defines the layout etc
·         PresenationCore.dll : defines the control
·         WindowsBase.dll : defines the events, dispatcher , dependency properties
Layer 2
Unmanaged layer
·         Milcore.dll : Media Integration Library core , manages animation , videos etc
·         WindowsCodecs: manages the vector UI
Layer 3
Core API
·         Direct3D
·         User32
·         GDI



What is dispatcher and thread affinity in WPF?


When a windows application is loaded on the screen each control is handled using a win 32 handler.
Win32 Handle is very important for any Win32 applications. For every Win32 apps, Kernel maintains a Table which has entries to identify a memory address. A HANDLE is actually a DWORD (32 bit integer) which maps the memory address on the table. So if you get a HANDLE, you can easily locate the memory address it points to.  Each object that you have in Windows (like windows, buttons, mouse pointers, icon, menu, bitmap etc) has entries in the table and the object is traced using both internally by windows or by programs using those HANDLEs. Even though it is just an unsigned integer value, you should not edit the value, otherwise the HANDLE could not be used to point the object anymore.
WPF window is made up of two parts.

1.       Window area which is made up of Operating System Window
2.      Non - Window area which is inside a WPF window
WPF window has only one window handle (HWND) and each other controls are actually placed as content to the window and does not have any entry on Kernel table(no HWND) except of course Popup class in WPF. In this post I will try to cover the basis of HWND (for those of you who don't know) and later go on with what are the changes of it with WPF environment.
When WPF application starts, it actually creates two threads automatically. One is Rendering Thread, which is hidden from the programmer, so you cannot use the rendering thread directly from your program; while the other is Dispatcher Thread, which actually holds all the UI elements. So in other words, you might say Dispatcher is actually the UI thread which ties all the elements created within the WPF application. Conversely, WPF requires all the UI elements to be tied with Dispatcher thread, this is called Thread Affinity. 
Dispatcher is a class that handles thread affinity. It is actually a prioritized message loop through which all elements are channeled through. Every UIElement is derived from DispatcherObject which defines a property called Dispatcher which points to the UI thread. Thus from any other thread, if you want to invoke or access UI component, you need to Invoke using Dispatcher thread. DispatcherObject actually has two chief duties, to check and verify if the thread has access to the object.


Routed Events:


A routed event is an event that can invoke handlers in multiple listeners in an element tree , rather than the object who initially raised the event.
 In WPF, a typical example of control's hierarchy is root level Window object, than Grid object and then the other controls which are resides on Grid Control.



The concept of Routed Events comes into the picture when we want to handle an event, that is originated from some other control in the hierarchy.  Say for example if any user clicks the Button control, that event which is normally a Button_click event, can be raised by The Button, The Label, The Gird or The Window.

Types of Routed Events:

·         Direct Events
·         Bubbling Events
·         Tunneling Events
1.  Direct Events: Direct Events are very well known to .NET people who has worked on standard .NET controls.  A direct event gets raised by the control itself.  Say for example Button_click event which got raised by the Button control itself.
2.  Bubbling Events: Bubbling events are first raised by the control and then are raised by the controls in that control's hierarchy.  Taking our Button control example, If Button is clicked, first it will raise Button_click event, then the Grid event and at last the Window Event.
The below picture will clear all your doubts:


3.  Tunneling Events: The Tunneling Events are the opposite of the bubbling events as they are raised first by the root element in the hierarchy and then by the child elements.  Same as our previous example, First Window event will get raised, followed by the Grid Event and at last the Button_click event.



Dependency Objects:

Every WPF control is derived from DependencyObject. DependencyObject is a class that supportsDependencyProperty, a property system that is newly built in WPF.
A dependency property essentially means a property in one class can be used in other class.
For example the properties top and bottom are not defined in rectangle class but they can be used in rectangle class as

 Every object is derived fromDependencyObject and hence it can associate itself in various inbuilt features of WPF like EventTriggers,PropertyBindings, Animations, etc.
Every DependencyObject actually has an Observer or a List and declares 3 methods called ClearValue,SetValue and GetValue which are used to add/edit/remove those properties. Thus the DependencyPropertywill only create itself when you use SetValue to store something.

Difference between CLR properties and Dependency Properties

CLR property Syntax


private int count;
public int Count
{
   get
   {
      return count;
   }
   set
   {
      count = value;
   }
}

Dependency property syntax


//Registering Dependency Property
public static DependencyProperty PageSizeProperty =
DependencyProperty.RegisterAttached("PageSize",
typeof(int), typeof(AttachedPropertySample),
             new PropertyMetadata(25,
             new PropertyChangedCallback(OnPageSizePropertyChanged)));

//PageSize property declaration
public int PageSize
{
    get
    {
        return (int) GetValue(PageSizeProperty);
    }
    set
    {
        SetValue(PageSizeProperty, value);
    }
}

Major features of Dependency Properties are
·         Value Resolution
CLR property reads value directly from private member while dependency property dynamically resolves value when you call GetValue() method of dependency property. The GetValue and SetValue methods are inherited fromDependency Object. You can read more about Dependency Property here.

·         In Built Change Notification
Dependency provides change notification when its value has been changed. You can specify Call Back while registering dependency property so user will get notification. This is mainly used in Data Binding.

·         Value Inheritance
If you specify dependency property to top element it will inherited to all child elements until child element specifically override the property. Dependency property value is resolved at runtime when you call GetValue() method. 

Attached Events

The XAML language also defines a special type of event called an attached event. An attached event enables you to add a handler for a particular event to an arbitrary element. The element handling the event need not define or inherit the attached event, and neither the object potentially raising the event nor the destination handling instance must define or otherwise "own" that event as a class member.
The WPF input system uses attached events extensively. However, nearly all of these attached events are forwarded through base elements.


Object Hierarchy


There are quite a few objects in any WPF control. Let's discuss one by one as in the figure. (The abstract class is marked in ellipse while concrete class in Rectangles) 

  • ·         DispatcherObject: Mother of all WPF controls which takes care of UI thread
  • ·         DependencyObject: Builds the Observer for Dependency Properties
  • ·         Visual: Links between managed libraries and milcore
  • ·         UIElement: Adds supports for WPF features like layout, input, events, etc.
  • ·         FrameworkElement: Implementation of UIElement
  • ·         Shape: Base class of all the Basic Shapes
  • ·         Control: The UI elements that interact with the user. They can be Templated to change look.
  • ·         ContentControl: Baseclass of all controls that have single content
  • ·         ItemsControl: Baseclass for all controls that show a collection
  • ·         Panel: Baseclass of all panels which show one or more controls within it


List Vs ObservableCollection Vs INotifyPropertyChanged

List
Observable Collection
INotifyPropertyChanged
strongly typed list of objects that can be accessed by index
ObservableCollection is a generic dynamic data collection that provides notifications (using an interface "INotifyCollectionChanged") when items get added, removed, or when the whole collection is refreshed.
INotifyPropertyChanged is not a collection, it’s an interface used in the data object classes to providePropertyChanged notification to clients when any property value gets changed. This will allow you to raisePropertyChanged event whenever the state of the object changes (Added, Removed, and Modified) to the point where you want to notify the underlying collection or container that the state has changed.
Using this you can search, sort, and manipulate lists
Using this you can search, sort, and manipulate lists
INotifyPropertyChanged is compatible on all type of collections like List<T>, ObservableCollection<T>, etc.
To Update the list you need to bind the data source again.
It does not provide any notifications when any property in the collection is changed.
The values in the observable collection are added, removed and changed during runtime in the code behind. The operations (adding and removing an item) in the observable collection will be updated to the UI (Datagrid). But any change in the existing item will not be updated to the UI.



List



ObservableCollection


From the msdn documentation:
It represents a dynamic data collection that provides notifications when items get added, removed, or when the whole list is refreshed.

public class ObservableCollection<T> : Collection<T>, INotifyCollectionChanged, INotifyPropertyChanged
Notice that, like any collection that derives from Collection<T>, its methods accept null parameters and do not throw an exception.
But the main feature of the ObservableCollection<T> are the events it raises when the items it contains change. 

By implementing the INotifyCollectionChanged and INotifyPropertyChanged interfaces, the collection has events for CollectionChanged and PropertyChanged. All these events are related. The first one is raised whenever something changed in the collection, be it Add, Remove, Move, etc. 

This also trigger the PropertyChanged event for the Items[] property. When you’re adding or removing items, PropertyChanged is also raised for the Count property.


INotifyPropertyChanged

 References:

WPF - Introduction


Introduction

WPF stands for Windows Presentation Framework.
It was introduced with .Net framework 3.0
It is the new-era of desktop applications that are very rich and efficient.

Advantages of WPF

WPF delivers rich, responsive UI; it is very easy to use animations with WPF.
I am not a game developer but then also creating games application using WPF is far easier. Efficient and flexible developing WPF applications are an experience in itself.

WPF Vs DirectX

Not really, although using WPF you can create nice, responsive UIs but the video performance is better in DirectX than in WPF.
WPF is good for small game programming.

 WPF Vs Windows applications

·         Windows applications basically used the Operating system controls to build its application. Whereas WPF uses the vector graphics and WPF controls are actually drawn over the screen, and hence you can customize controls totally and modify their behavior when required.
·         WPF uses the hardware capabilities of your system making it to deliver smooth graphics.
·         WPF uses device independent DPI(dots per inch) for the visual layout whereas the windows applications use pixels. Using DPI makes a WPF application to adjust with various different resolutions of your monitor.
·         It’s very easy to apply styles to a WPF application , in case of windows applications the styles are defined in css and each control is tightly coupled to a style. Whereas in case of WPF you can create a style irrespective of the control.
For example , you can give a toggle button style to your checkbox.
·         In case of windows application , the controls are resource intensive , that is when a form is created the controls are loaded and their objects are loaded in memory. In case of WPF if the templates are loaded only once during the complete lifecycle of the form, this frees a certain part of memory.
·         Also, in case of WPF we are using data binding and dependency properties instead of CLR properties which gives the application very loosely coupled architecture.

Monday, 30 July 2012

Dependency Property : Silverlight/WPF

WPF/Silverlight introduce a new property called dependency property. In this article we will see

  • What is a dependency object
  • What is a dependency property
  • Compare dependency property with CLR property
  • Advantages of dependency properties
  • Attached Properties