The future of Windows Desktop Development

Download Report

Transcript The future of Windows Desktop Development

An introduction to
Windows Presentation Foundation
LEADING EDGE
WINDOWS DEVELOPMENT
Introducing WPF
 Available on Vista, Xp SP2,





and Windows Server 2003
Hardware acceleration
Resolution independence
Declarative user interface
Object-based drawing
Dynamic control appearance (lookless
controls)
WPF Features
 Web like layout model
 Rich drawing model
 Styles and control templates
 Animation
 Audio and Video
 Page based applications
 Command model
WPF Applications
 Windows Standalone Applications
 Full trust by default
 Installed
 XAML Browser Applications (XBAP)
 Partial trust by default
 Not installed
 Windows: IE, Firefox
 Both require .NET Framework 3.0/3.5
Display Technologies
 Same display technology for 15 years.
 User32 provides familiar look and feel.
 GDI/GDI+ provides drawing support for rendering
shapes, text, and images.
 Newer frameworks increase usability, but are
fundamentally limited by base technology.
WPF Display Technology
 WPF uses DirectX for all rendering.
 WPF is not a wrapper for GDI/GDI+, it is a
replacement.
 DirectX is more efficient as it is able to use
the video card to render textures and
gradients. GDI/GDI+ can’t.
 WPF still uses User32 for some things such as
handling user input.
WPF Rendering Tiers
 Hardware factors determine the tier. Factors
include card Ram, pixel shaders, vertex shaders.
 Tier 0
 Equivalent DirectX < 7.0.
 No hardware acceleration.
 Tier 1
 Equivalent 7.0 < DirectX < 9.0
 Partial hardware acceleration.
 Tier 2
 Equivalent to DirectX > 9.0
 All features.
XAML
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="120" Width="204">
<Grid>
<TextBlock FontSize="50">WPF!</TextBlock>
</Grid>
</Window>
BAML
XAML
 Separation of design from code.
 No conversion of graphical content.
 Main domain is WPF user interfaces.
 Understanding XAML is critical in WPF
application design.
With XAML
 Wire up event handlers
 Define resources
 Define control templates
 Data bind
 Animate
Alien Sokoban Demo
Class Hierarchy Overview
DispatcherObject
DependencyObject
Visual
UIElement
FrameworkElement
Shape
Legend
Control
Panel
ContentControl
Abstract
Concrete
ItemsControl
The Application Class
 Single Threaded Apartment State.
 Tracks all open windows.
 Allows for initialization and cleanup.
 System.Windows.Application ≈
System.Windows.Forms.Application
Layout
 Layout defined using containers.
 Coordinate based layout is discouraged.
 Resolution-independent and size-
independent interfaces.
 Advantages:
 Scale well on different monitors.
 Adjust when content changes.
 Handle transition to other languages.
Layout Panels
 StackPanel
 WrapPanel
 DockPanel
 Grid
 UniformGrid
 Canvas
Content Model
ContentControl
Tooltip
ScrollViewer
HeaderedContentControl
Label
Window
Expander
ButtonBase
UserControl
GroupBox
TabItem
Legend
Abstract
Concrete
<Button>
<Grid>
<Polygon Points=“...” />
<Grid>
</Button>
Dependency Properties
 Change notification
 Property value inheritance
 Used in animation, data binding, and styles
Property Coercion
 All WPF controls guarantee that their
properties can be set in any order.
At this point
bar.Maximum = 1
ScrollBar bar = new ScrollBar();
bar.Value = 100;
bar.Minimum = 1;
bar.Maximum = 200;
bar.Value = 1
bar.Value = 100
Registering a Dependency Property
public class MyClass
{
public static readonly DependencyProperty MyNameProperty
= DependencyProperty.Register("MyName", typeof(string),
typeof(MyClass),
new UIPropertyMetadata(MyClass.MyNameValueChanged));
public string MyName
{
get { return (string)GetValue(MyNameProperty); }
set { SetValue(MyNameProperty, value); }
}
static void MyNameValueChanged(DependencyObject dependencyObject,
DependencyPropertyChangedEventArgs e)
{
MyClass myClass = (MyClass)dependencyObject;
myClass.TextBox_Name.Text = (string)e.NewValue;
}
Property Validation
 Don’t place code in accessor, apart from
SetValue and GetValue.
 Use an event handler for property validation.
public class MyClass
{
public static readonly DependencyProperty MyNameProperty
= DependencyProperty.Register("MyName", typeof(string),
typeof(MyClass),
new UIPropertyMetadata(MyClass.MyNameValueChanged),
new ValidateValueCallback(MyClass.IsNameValid));
static bool IsNameValid(object value)
{
return value == “Daniel”;
}
Routed Events
 Tunnel down or bubble up the element tree.
 Required for the Content Model.
Raised here
first
Child UIElement
Child UIElement
Event Source
Raised here
first
Tunneling Event
Bubbling Event
Root Element
(Window)
Registering a Routed Event
public abstract class ButtonBase : ContentControl, ...
{
public static readonly RoutedEvent ClickEvent
= EventManager.RegisterRoutedEvent(“Click”,
RoutingStrategy.Bubble,
typeof(RoutedEventHandler),
typeof(ButtonBase));
public event RoutedEventHandler Click
{
add { base.AddHandler(ButtonBase.ClickEvent, value); }
remove { base.RemoveHandler(ButtonBase.ClickEvent, value); }
}
RoutedEventArgs args = new RoutedEventArgs(ButtonBase.ClickEvent, this);
Base.RaiseEvent(args);
Attaching an Event Handler
 Declaratively in XAML:
<Button Name=“Button_Test” Click=“Button_Click” />
 Imperatively in code beside:
Button_Test.Click += Button_Click;
 Directly using the Routed Event:
Button_Test.AddHandler(ButtonBase.ClickEvent,
new RoutedEventHandler(Button_Click));
 Handler:
void Button_Click(object sender,
RoutedEventArgs e)
{...}
Pages and Navigation
 Traditional Windows applications are
window-centric, while the web is pagecentric.
 WPF provides for both.
FrameworkElement
Page
Control
ContentControl
Window
Hyperlinks
<Page x:Class="WpfApplication1.Page1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Page1">
<Grid>
<StackPanel>
<TextBlock Width="120">Hello DEEWR</TextBlock>
<TextBlock Width="120">
<Hyperlink NavigateUri="Page2.xaml">Next</Hyperlink>
</TextBlock>
<Button Width="75" Click="button2_Click">Close</Button>
</StackPanel>
</Grid>
</Page>
Navigation Continued
 Fragment Navigation
<Hyperlink NavigateUri=“Page2.xaml#MyTextBox”>
link text</Hyperlink>
 Hyperlinks ok for linear and fixed series of steps.
 NavigationService used for more complex
scenarios.
 Navigation in WPF is asynchronous.
 NavigationService provides useful events for e.g.
monitoring progress and cancelling navigation.
Page Functions
 Derived from Page class.
 Adds ability to return a result.
 No need for global variables or shared state.
<PageFunction
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
x:Class="WpfApplication1.PageFunction1"
x:TypeArguments="sys:String"
Title="PageFunction1">
<Grid></Grid>
</PageFunction>
Page Functions Continued
PageFunction
Navigate()
Page
OnReturn()
Controller
Navigate()
OnReturn()
PageFunction
.
.
.
PageFunction
Commands
 Commands are higher level tasks.
 Triggered by a variety of sources: Toolbar
button, menu item etc.
 Allows for enabling and disabling of controls.
 Allows for localization.
 Lacks history and undo/redo.
Commands continued
 Business logic usually shouldn’t sit in event
handlers, and may reside in separately
compiled components.
 Wiring up event handlers for controls can be
messy.
 WPF Command model
 Designates events to commands
 Synchronizes enabled control state
 Wired up using XAML
Styles
 Collection of property values that can be
applied to a UIElement.
 More powerful than CSS.
 Able to modify behaviour as well as
appearance.
 Supports Triggers.
<Style x:Key="CenterLabels" TargetType="{x:Type Label}">
<Setter Property="Foreground" Value="White"/>
<Setter Property="FontSize" Value="25"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
Control Templates
 Allows radical redesign of every control.
 In the past
 Customization difficult or impossible.
 Had to choose between convenience (prebuilt
controls) and flexibility (roll your own).
Data Binding
 Allows binding any UIElement’s property to
another UIElement’s property.
 Eliminates tedious code.
 Binding Directions
 OneWay
 TwoWay
 OneTime
 .NET 3.5 offers binding to LINQ expressions.
Demo
Notable Omissions
 XPS
 Resources
 Accessibility
 Data binding validation
 Animation
 3D
Further Information
 http://danielvaughan.orpius.com
References
 Dependency Properties
 MacDonald, M. 2008, Pro WPF in C# 2008,
Apress
 Dependency Properties Overview