Unit Testing in .NET
Download
Report
Transcript Unit Testing in .NET
Overview of WPF
Ivan Krivyakov
Senior Managing Consultant
SunGard Consulting Services
E-mail:
[email protected]
[email protected]
http://www.ikriv.com/demo/wpf/
Overview of WPF
What Is This Presentation About
WPF for an experienced developer
What makes WPF different?
Things to explore
Definitely not a tutorial
Overview of WPF
Why Is WPF Important?
It’s a major technology from a big
market player
It is gaining ground in replacing
Windows Forms
Many clients start including WPF
knowledge as a requirement for hire
Officially released in Nov 2006 and
still popular – definitely not a hype
bubble
Overview of WPF
A Contrived Mixture Formula
WPF =
XAML
+ Data Binding
+ Styles
Overview of WPF
XAML
XML-based object definition language
Defines objects and their properties
Similar to the language of Spring.Net
Primarily used to describe UI artifacts
Can describe almost any .NET type
Limited support of generics
Overview of WPF
XAML
Describing UI artifacts:
<DockPanel LastChildFill="True">
<StackPanel Orientation="Vertical" DockPanel.Dock="Top">
<TextBlock Margin="2">Count:</TextBlock>
<TextBox Margin="5" Text="{Binding Count}" />
<TextBlock Margin="2">Results:</TextBlock>
</StackPanel>
<ListBox Margin="5" ItemsSource="{Binding Results}" />
</DockPanel>
Describing custom data classes:
<my:NumericViewModel Count="5">
<my:NumericViewModel.Sequence>
<my:SquareRoots />
</my:NumericViewModel.Sequence>
</my:NumericViewModel>
Overview of WPF
XAML
Many controls accept arbitrary content
E.g. a button may contain any
combination of other controls, such as
text, images, and even text boxes, etc.
Thus no need for special image button
or Image property on a button class
Overview of WPF
XAML
ImageButtonDemo sample
Overview of WPF
Data Binding
<TextBlock Text=“{Binding
Names.Count}” />
<ListBox ItemsSource=“{Binding
Names}” />
By default binds to the DataContext
object of the UI element
Can bind to properties of other UI
elements, static instances, etc.
Overview of WPF
Dependency Properties
CLR properties: get, set
WPF dependency properties: get, set,
default value, change notification,
validation, animation, etc.
Dependency properties are set via
reflection-like API
WPF DependencyObject class has a map
from property descriptors to property values
Most WPF classes derive (directly or
indirectly) from DependencyObject
Overview of WPF
Dependency Properties
The Dependency property API is somewhat bulky:
public class MyStateControl : ButtonBase
{
public MyStateControl() : base() { }
public Boolean State
{
get { return (Boolean)this.GetValue(StateProperty); }
set { this.SetValue(StateProperty, value); }
}
public static readonly DependencyProperty StateProperty =
DependencyProperty.Register(
"State",
typeof(Boolean),
typeof(MyStateControl),
new PropertyMetadata(false));
}
Overview of WPF
Dependency Properties
GetValue(), SetValue() can accept
properties owned by other classes. These
called “attached” properties
In XAML attached properties look like so:
<DockPanel>
<TextBlock DockPanel.Dock=“Top” />
</DockPanel>
“Dock” property is owned by the DockPanel class,
but it is set on a TextBlock instance. This is
somewhat similar to extension methods in C#
Overview of WPF
Data Change Notification
Binding mechanism must know
when things change
Dependency properties are set via
WPF-controlled methods
GetValue, SetValue
Overview of WPF
Data Change Notification
For regular properties must implement
interface INotifyPropertyChanged
{
event … PropertyChanged;
}
class MyClass : INotifyPropertyChanged
{
string _data;
string Data
{
get { return _data; }
set { if (value == _data) return;
_data = value;
PropertyChanged(…”Data”…);
}
}
}
Overview of WPF
Data Binding
Binding source: regular property or
dependency property
Binding target: dependency property
Animation target: dependency property
Overview of WPF
Data Binding
NumericFunctions Sample
Overview of WPF
Data Templates
Data templates replace owner draw
items in item controls like list boxes
Rule of thumb: avoid “assembling”
text for UI in code. Use data templates
instead
Overview of WPF
Data Templates
NumericFunctions sample revisited
using data template
Overview of WPF
Styles and Control Templates
Lookless controls: behavior is
separated from appearance
Behavior: control class, appearance:
control style
Clock control may have Time property,
and can track current time
Styles can define colors, fonts, and
even completely overhaul the
appearance of the control
Overview of WPF
Styles and Control Templates
ClockControl demo
Overview of WPF
WPF Binaries
Main: shipped with .NET 3.0 and later
WPF toolkit: additional controls such
as data grid, date picker control
“Prism”: composite application
guidance for WPF
Overview of WPF
Expression Blend
Writing XAML by hand, especially
grid layouts, may be overwhelming
Visual Studio has limited editing
capabilities
Expression Blend is a separate
application for editing XAML
documents
Pros and Cons
Overview of WPF
MVVM vs MVP
Model
View
Model
View
WPF
Binding
Mechanism
ViewModel
Presenter
Overview of WPF
MVVM vs MVP
MVVM is a WPF version of MVP
View is pure XAML
ViewModel has data to display
ViewModel may be more
detailed/redundant than business
objects
ViewModel does not have a
reference to the view
Overview of WPF
MVVM vs MVP
All communication to the view is
done via bindings
Bindings take on most of the work of
the presenter
ViewModel is mostly about
presentation data. If there is no
complex business logic, model is not
necessary
Overview of WPF
MVVM and Commands
Out of the box WPF does not fully
support MVVM
Exchanging data is easy, dealing with
commands, focus, etc. is more difficult.
Not having reference to the view causes
problems
Default mechanism of routed commands
is not compatible with MVVM, since
commands are delivered to the view
Overview of WPF
Summary
WPF is very flexible, it has almost
mathematical beauty
New approach to UI programming:
binding, attached properties, MVVM
A learning curve
Fair number of gotchas and bugs
Performance is a frequent concern
Demand for WPF is on the rise
Overview of WPF
References
Book: “Programming WPF” by Chris
Sells
WPF toolkit et al.:
http://wpf.codeplex.com/
Prism:
http://compositewpf.codeplex.com/
MVVM:
http://msdn.microsoft.com/enus/magazine/dd419663.asp