There are quite a few things that you must know before starting with WPF applications.
What is meant by Dispatcher & Thread Affinity?
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. Thus you cannot change any element created on Dispatcher thread from any other threads, thus it follows the same Win32 based API's.
Thus it allows you to inter-operate any WPF component into HWND based API.
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.
What is Visual Tree and Logical Tree?
Every programming style contains some sort of LogicalTree which comprises the Overall Program. The LogicalTree comprises the elements as they are listed in XAML.
Thus they will only include the controls that you have declared in you XAML.
VisualTree on the other hand, comprises the parts that make up the individual controls. You do not generally need to deal with VisualTree directly, but you should know how each control is comprises of, so it would be easier to build custom templates using this.
I personally always like to see the VisualTree before using it. ExpressionBuilder is the one tool that allows you to generate the actual control.
Why RoutedEvent ?
RoutedEvent is very new to C# language, but those who are coming from javascript/web tech, you would have found it in your browser. Actually there are two types of RoutedEvent. One which Bubbles through the Visual tree elements and other which Tunnels through the visual tree elements. There is also Direct RoutedEvent which does not Bubble or Tunnel.
When a Routed event which is registered, is invoked, it Bubbles / Tunnels through the Visual Elements and calls all the Registered RoutedEventHandlers associated within the Visual Tree one by one.
To discriminate between the two, WPF demarcated events with Preview*** as the events which are Tunneled and just *** for the events that Bubbles. For instance
IsPreviewMouseDown is the event that tunnels through the Visual Child elements while MouseDown Bubbles. Thus Mouse Down of the Outermost element is called first
in case of IsPreviewMouseDown while Mouse Down for the innermost element will be called first in case of MouseDown event.
Why DependencyObject is Used ?
Every WPF control is derived from DependaecyObject. DependencyObject is a class that supports DependencyProperty, a property system that is newly built in WPF.
Every object is derived from DependencyObject 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 DependencyProperty will only create itself when you use SetValue to store something. Thus it is resource saving as well. We will look DependencyProperty in detail on other articles of the Series.
What about Hardware Acceleration and Graphics Rendering Tiers in WPF ?
Another important thing that you should know is how the WPF graphics is rendered. Actually WPF rendering automatically detects how much hardware acceleration is supported by the current system and adjusts itself accordingly. The graphics rendering detects the appropriate tier for rendering the output accordingly.
For hardware Rendering few things that has most of the impact are :
1. Video RAM : This determines the size and number of buffers that the application might use to render its output.
2. Pixel Shader : It is a graphics utility which calculates effects on per pixel basis.
3. Vertex Shader : It is a graphics processing utility that performs mathematical calculations on Vertex of the output. They are used to add special effects to
objects in 3D environment.
4. MultiTexture Blending : This is a special function that allows you to apply two or more textures on the same object in 3D.
Now the rendering engine of WPF determines which tier is appropriate for the current application and apply the rendering tiers accordingly.
=> TIER 0 : No graphics hardware acceleration takes place, rather everything is rendered using Software. Any version of DirectX 9 or less is capable of
rendering this output.
=> TIER 1 : Partial hardware and software rendering. You might use Directx9 or greater to use this tier.
=> TIER 2 : Full hardware acceleration. Directx9 or above can render this output.
What is meant by Dispatcher & Thread Affinity?
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. Thus you cannot change any element created on Dispatcher thread from any other threads, thus it follows the same Win32 based API's.
Thus it allows you to inter-operate any WPF component into HWND based API.
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.
What is Visual Tree and Logical Tree?
Every programming style contains some sort of LogicalTree which comprises the Overall Program. The LogicalTree comprises the elements as they are listed in XAML.
Thus they will only include the controls that you have declared in you XAML.
VisualTree on the other hand, comprises the parts that make up the individual controls. You do not generally need to deal with VisualTree directly, but you should know how each control is comprises of, so it would be easier to build custom templates using this.
I personally always like to see the VisualTree before using it. ExpressionBuilder is the one tool that allows you to generate the actual control.
Why RoutedEvent ?
RoutedEvent is very new to C# language, but those who are coming from javascript/web tech, you would have found it in your browser. Actually there are two types of RoutedEvent. One which Bubbles through the Visual tree elements and other which Tunnels through the visual tree elements. There is also Direct RoutedEvent which does not Bubble or Tunnel.
When a Routed event which is registered, is invoked, it Bubbles / Tunnels through the Visual Elements and calls all the Registered RoutedEventHandlers associated within the Visual Tree one by one.
To discriminate between the two, WPF demarcated events with Preview*** as the events which are Tunneled and just *** for the events that Bubbles. For instance
IsPreviewMouseDown is the event that tunnels through the Visual Child elements while MouseDown Bubbles. Thus Mouse Down of the Outermost element is called first
in case of IsPreviewMouseDown while Mouse Down for the innermost element will be called first in case of MouseDown event.
Why DependencyObject is Used ?
Every WPF control is derived from DependaecyObject. DependencyObject is a class that supports DependencyProperty, a property system that is newly built in WPF.
Every object is derived from DependencyObject 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 DependencyProperty will only create itself when you use SetValue to store something. Thus it is resource saving as well. We will look DependencyProperty in detail on other articles of the Series.
What about Hardware Acceleration and Graphics Rendering Tiers in WPF ?
Another important thing that you should know is how the WPF graphics is rendered. Actually WPF rendering automatically detects how much hardware acceleration is supported by the current system and adjusts itself accordingly. The graphics rendering detects the appropriate tier for rendering the output accordingly.
For hardware Rendering few things that has most of the impact are :
1. Video RAM : This determines the size and number of buffers that the application might use to render its output.
2. Pixel Shader : It is a graphics utility which calculates effects on per pixel basis.
3. Vertex Shader : It is a graphics processing utility that performs mathematical calculations on Vertex of the output. They are used to add special effects to
objects in 3D environment.
4. MultiTexture Blending : This is a special function that allows you to apply two or more textures on the same object in 3D.
Now the rendering engine of WPF determines which tier is appropriate for the current application and apply the rendering tiers accordingly.
=> TIER 0 : No graphics hardware acceleration takes place, rather everything is rendered using Software. Any version of DirectX 9 or less is capable of
rendering this output.
=> TIER 1 : Partial hardware and software rendering. You might use Directx9 or greater to use this tier.
=> TIER 2 : Full hardware acceleration. Directx9 or above can render this output.

No comments:
Post a Comment