0% found this document useful (0 votes)
48 views5 pages

WPF

The document discusses key concepts in WPF including that it uses DirectX for rendering, uses device-independent units of 1/96 inch, respects the system DPI setting, uses XAML for defining user interfaces, requires a code-behind class to handle events, and requires the InitializeComponent method to be called to connect the XAML to code.

Uploaded by

tomas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views5 pages

WPF

The document discusses key concepts in WPF including that it uses DirectX for rendering, uses device-independent units of 1/96 inch, respects the system DPI setting, uses XAML for defining user interfaces, requires a code-behind class to handle events, and requires the InitializeComponent method to be called to connect the XAML to code.

Uploaded by

tomas
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

WPF - DirectX

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:

[Physical Unit Size] = [Device-Independent Unit Size] x [System DPI]


= 1/96 inch x 96 dpi
= 1 pixel

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

[Physical Unit Size] = [Device-Independent Unit Size] x [System DPI]


= 1/96 inch x 120 dpi
= 1.25 pixels

XAML

The xmlns attribute


specialized attribute in the world of XML that’s reserved for declaring namespaces. These URIs look
like they point to a location on the Web, but they don’t. The URI format is used because it makes it
unlikely that different organizations will inadvertently create different XML-based languages with the
same namespace.

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.

http://schemas.microsoft.com/winfx/2006/xaml/presentation is the core WPFnamespace.


It encompasses all the WPF classes, including the controls you use to build user interfaces.

http://schemas.microsoft.com/winfx/2006/xaml is the XAML namespace.


It includes various XAML utility features that allow you to influence how your document is interpreted.

The Code-Behind Class


XAML allows you to construct a user interface, but in order to make a functioning application, you need a
way to connect the event handlers that contain your application code. XAML makes this easy by using the
Class attribute that’s shown here:
<Window x:Class="WindowsApplication1.Window1"

The InitializeComponent() Method


InitializeComponentmethod plays a key role in WPF applications. Therefore, you should never delete
the InitializeComponent() call in your window’s constructor. Similarly, if you add another constructor to
your window class, make sure it also calls InitializeComponent().

Single-Threaded Apartment (STA)


Single-threaded apartments contains only one thread. All objects in this apartment can receive method
calls from only this thread. Objects does not need synchronization because all methods calls are comes
synchronously from single thread. Single-threaded apartment needs a message queue to handle calls
from other threads. When other threads calls an object in STA thread then the method call are queued in
the message queue and STA object will receive a call from that message queue.

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.

Dispatcher owns the message queue for the STA thread.

When WPF application starts, it creates two threads:

1. Render thread
2. UI thread

WPF Dispatcher is associated with the UI thread.


The UI thread queues methods call inside the Dispatcher object.

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.

DispatcherOperation op = Dispatcher.BeginInvoke((Action)(() => {


btn1.Content = "By BeginInvoke";
}));

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.

The WPF Toolkit


Before a new control makes its way into the WPF libraries of the .NET platform, it often begins in a
separate Microsoft download known as the WPF Toolkit. But the WPF Toolkit isn’t just a place to preview
the future direction of WPF—it’s also a great source of practical components and controls that are made
available outside the normal WPF release cycle. For example, WPF doesn’t include any sort of charting
tools, but the WPF Toolkit includes a set of controls for creating bar, pie, bubble, scatter, and line graphs.

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.

public partial class MainWindow : Window


{
public MainWindow()
{
InitializeComponent();
Task.Factory.StartNew(() =>
{
InvokeMethodExample();
});
}

private void InvokeMethodExample()


{
Thread.Sleep(2000);
Dispatcher.Invoke(() =>
{
btn1.Content = "By Invoke";
});
}
}

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).

You might also like