WPF
WPF
In WPF, the underlying graphics technology isn’t GDI/GDI+. Instead, it’s DirectX. In fact, WPF
applications use DirectX no matter what type of user interface you create. That means that whether
you’re designing complex three-dimensional graphics (DirectX’s forte) or just drawing buttons and plain
text, all the drawing work travels through the DirectX pipeline.
Units
A WPF window and all the elements inside it are measured using device-independent units. A single
deviceindependent unit is defined as 1/96 of an inch. Imagine that you create a small button in WPF
that’s 96 by 96 units in size.If you’re using the standard Windows DPI setting (96 dpi dots per inch),
each device-independent unit corresponds to one real, physical pixel. That’s because WPF uses this
calculation:
1/96 of an inch
WPF respects the system DPI setting natively and effortlessly. For example, if you change the system
DPI setting to 120 dpi (a common choice for users of large highresolution screens), WPF assumes that it
needs 120 pixels to fill an inch of space. WPF uses the following calculation to figure out how it should
translate its logical units to physical device pixels
XAML
there isn’t a one-to-one mapping between the XML namespaces used in XAML and .NET namespaces
is because it would significantly complicate your XAML documents. The problem here is that WPF
encompasses well over a dozen namespaces (all of which start with System.Windows). If each .NET
namespace had a different XML namespace, you’d need to specify the right namespace for each and
every control you use, which quickly gets messy. Instead, the creators of WPF chose to combine all of
these .NET namespaces into a single XML namespace.
WPF Dispatcher
A WPF application must start in single-threaded apartment thread. STA have a message queue to
synchronize method calls within his apartment. As well as other threads outside the apartment
can't update the objects directly. They must place their method call into the message queue to
update the objects in STA.
1. Render thread
2. UI thread
Invoke
Invoke method takes an Action or Delegate and execute the method synchronously. That means it
does not return until the Dispatcher complete the execution of the method.
Above code will create a new thread using Task.Factory and immediately start the thread. In the
InvokeMethodExample if we try to directly call to update the Content property of btn1 object. It will
throws a System.InvalidOperationException. We have used Invoke method of Dispatcher. In the
Invoke method, I pass the Action and update the Content property of Button object. It will not
throws any error and successfully update the Content property.
BeginInvoke
BeginInvoke method take a Delegate but it executes the method asynchronously. That means it
immediately returns before calling the method.
Commands
Most users realize that it doesn’t matter whether they trigger the Open command through a menu or
through a toolbar; the end result is the same. Now that abstraction is available to your code, you can
define an application command in one place and link it to multiple controls.
System.Threading.DispatcherObject
WPF applications use the familiar single-thread affinity (STA) model, which means the entire user
interface is owned by a single thread. It’s not safe to interact with user interface elements from another
thread. To facilitate this model, each WPF application is governed by a dispatcher that coordinates
messages
System.Windows.DependencyObject
In WPF, the central way of interacting with onscreen elements is through properties. Early on in the
design cycle, the WPF architects decided to create a more powerful property model that baked in
features such as change notification, inherited default values, and more economical property storage
System.Windows.UIElement
UIElement adds support for WPF essentials such as layout, input, focus, and events
System.Windows.FrameworkElement
FrameworkElement is the final stop in the core WPF inheritance tree. It implements some of the
members that are merely defined by UIElement. For example, UIElement sets the foundation for the
WPF layout system, but FrameworkElement includes the key properties (such as HorizontalAlignment
and Margin) that support it.
System.Windows.Controls.Control
A control is an element that can interact with the user. It obviously includes classes such as TextBox,
Button, and ListBox. The Control class adds additional properties for setting the font and the foreground
and background colors. But the most interesting detail it provides is template support, which allows you
to replace the standard appearance of a control with your own stylish drawing.
System.Windows.Controls.ContentControl
This is the base class for all controls that have a single piece of content. This includes everything from
the humble Label to the Window.
System.Windows.Controls.ItemsControl
This is the base class for all controls that show a collection of items, such as the ListBox and TreeView.
List controls are remarkably flexible—for example, using the features that are built into the ItemsControl
class, you can transform the lowly ListBox into a list of radio buttons, a list of check boxes
System.Windows.Controls.Panel
This is the base class for all layout containers—elements that can contain one or more children and
arrange them according to specific layout rules.
Binary Application Markup Language (BAML. BAML is really nothing more than a binary representation
of XAML.When you compile a WPF application in Visual Studio, all your XAML files are converted into
BAML, and that BAML is then embedded as a resource into the final DLL or EXE assembly.
Dispatcher
Dispatcher owns the message queue for the STA thread. When you execute a WPF application, it
automatically create a new Dispatcher object and calls its Run method. Run method is used for
initializing the message queue. Invoke method takes an Action or Delegate and execute the method
synchronously. That means it does not return until the Dispatcher complete the execution of the
method.
Above code will create a new thread using Task.Factory and immediately start the thread. In the
InvokeMethodExample if we try to directly call to update the Content property of btn1 object. It will
throws a System.InvalidOperationException. We have used Invoke method of Dispatcher. In the
Invoke method, I pass the Action and update the Content property of Button object. It will not
throws any error and successfully update the Content property.
The first thing is to understand that, the Dispatcher is not designed to run long blocking operation
(such as retrieving data from a WebServer...). You can use the Dispatcher when you want to run an
operation that will be executed on the UI thread (such as updating the value of a progress bar).