Download presentation
Presentation is loading. Please wait.
1
Leading edge windows development
An introduction to Windows Presentation Foundation Leading edge windows development
2
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)
3
WPF Features Web like layout model Rich drawing model
Styles and control templates Animation Audio and Video Page based applications Command model
4
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
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.
6
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.
7
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.
8
XAML BAML <Window x:Class="WpfApplication1.Window1"
xmlns=" xmlns:x=" Title="Window1" Height="120" Width="204"> <Grid> <TextBlock FontSize="50">WPF!</TextBlock> </Grid> </Window> XAML BAML
9
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.
10
With XAML Wire up event handlers Define resources
Define control templates Data bind Animate
11
Alien Sokoban Demo
12
Class Hierarchy Overview
DispatcherObject DependencyObject Visual UIElement FrameworkElement Shape Control Panel Legend ContentControl Abstract ItemsControl Concrete
13
The Application Class Single Threaded Apartment State.
Tracks all open windows. Allows for initialization and cleanup. System.Windows.Application ≈ System.Windows.Forms.Application
14
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.
15
Layout Panels StackPanel WrapPanel DockPanel Grid UniformGrid Canvas
16
HeaderedContentControl
Content Model ContentControl Tooltip ScrollViewer HeaderedContentControl Label Window Expander ButtonBase UserControl GroupBox TabItem Legend <Button> <Grid> <Polygon Points=“...” /> </Button> Abstract Concrete
17
Dependency Properties
Change notification Property value inheritance Used in animation, data binding, and styles
18
At this point bar.Maximum = 1
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
19
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; }
20
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”; }
21
Routed Events Tunnel down or bubble up the element tree.
Required for the Content Model. Root Element (Window) Raised here first Bubbling Event Tunneling Event Child UIElement Child UIElement Event Source Raised here first
22
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);
23
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) {...}
24
Pages and Navigation Traditional Windows applications are window-centric, while the web is page- centric. WPF provides for both. FrameworkElement Page Control ContentControl Window
25
Hyperlinks <Page x:Class="WpfApplication1.Page1"
xmlns=" xmlns:x=" 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>
26
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.
27
Page Functions Derived from Page class.
Adds ability to return a result. No need for global variables or shared state. <PageFunction xmlns=" xmlns:x=" xmlns:sys="clr-namespace:System;assembly=mscorlib" x:Class="WpfApplication1.PageFunction1" x:TypeArguments="sys:String" Title="PageFunction1"> <Grid></Grid> </PageFunction>
28
Page Functions Continued
Navigate() Page Controller Navigate() PageFunction OnReturn() OnReturn() . PageFunction
29
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.
30
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
31
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>
32
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).
33
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
34
Notable Omissions XPS Resources Accessibility Data binding validation
Animation 3D
35
Further Information
36
References Dependency Properties
MacDonald, M. 2008, Pro WPF in C# 2008, Apress Dependency Properties Overview
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.