Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Presentation Foundation Ruwan Wijesinghe.

Similar presentations


Presentation on theme: "Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Presentation Foundation Ruwan Wijesinghe."— Presentation transcript:

1 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Presentation Foundation Ruwan Wijesinghe

2 2 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL WPF Architecture

3 3 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL DependencyObject Every object in WPF are derived from System.Windows.DependencyObject base class XAML markup languages syntax is based on the features provided by dependency objects DependencyObjects supports rich property system This includes the support for When a dependency property in a parent control in the object tree changes the corresponding dependency property in the child object also get changed Parent objects can add properties to the child objects (e.g. layout objects) Parent properties can handle events of child properties, based on child object type (i.e. Click event of all buttons) Support two way data binding

4 4 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Visuals Tree Visual is a low level light weight graphics object derived from System.Windows.Media.Visual class They represent primitive graphic objects like, line, squire, ellipse, etc. Visual Tree supports “Retained Mode Rendering System”

5 5 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Basic Building Blocks UIElements Supports for Layouts Inputs Events FrameworkElements This layer is being build on top of UIElements layer Supports Basic Layouts Styles Templates Data Binding

6 6 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Controls Inherited from “System.Windows.Controls.Control” class Controls, by default has set of properties and attributes The actual appearance of the control can be totally customized using the ControlTemplate As a result we rarely need to create WPF custom controls

7 7 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL XAML XAML – Extended Application Markup Language Helps to create object trees with complex relationships, required by applications like WPF and WF using a Mark-up Language E.g. Hello, XAML!

8 8 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL XAML Properties Set properties using Attributes Set properties using Property Elements This is a button

9 9 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Content Properties --> Button 1 Button 2 This is equivalent to the following markup

10 10 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Markup Extensions Button 1 One Label Used with Resources, Data-Binding etc.

11 11 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Namespaces We need to add namespaces to the root element of XAML file WPF Schema: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" XAML Schema: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" When CLR data types are being used inside XAML code, we have to define an XML namespace for each CRL namespace like this xmlns:custom="clr-namespace:MySample; assembly=MySampleLibrary" (When the namespace is in the same assembly, we can do it like this) xmlns:custom="clr-namespace:MySample" Once this is done, then we can use “custom” prefix to indicate a CLR data type defined in that namespace e.g. The CLR data type “MySample.Employee” can be represented in XAML as,

12 12 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL x:Prefix X prefix is mapped to XAML namespace http://schemas.microsoft.com/winfx/2006/xaml http://schemas.microsoft.com/winfx/2006/xaml This namespace defines the following tags x:Type: Constructs a Type reference based on a type name. This is used to specify attributes that take Type, such as System.Windows.Style.TargetType. x:Type x:Key: Sets a unique key for resources in a ResourceDictionary. x:Key x:Class: Specifies the CLR namespace and class name for the class that provides code-behind for a XAML page. x:Class x:Static: Enables a value reference to a static value that is not otherwise a XAML settable property. x:Static

13 13 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Code Behind Files Code behind files are implemented using Partial Classes <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="MyNamespace.MyPageCode"> Click Me! namespace MyNamespace { public partial class MyPageCode { void Button1Clicked(object sender, RoutedEventArgs e) { Button1.Background = Brushes.Yellow; }

14 14 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Attached Properties and Attached Events Parent element in the Element Tree can add properties to the child elements I am on the left I am on the right

15 15 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Routed Events Events that can be handled by parent elements in the tree Yes No private void CommonClickHandler(object sender, RoutedEventArgs e) { FrameworkElement feSource = e.Source as FrameworkElement; switch (feSource.Name) { case "YesButton": // do something here... case "NoButton": // do something... } e.Handled=true; }

16 16 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Tunneling and Bubbling Events Most of the WPF events flows in both directions in the element tree Tunneling Events – Flows from child element to root element Event name is prefixed by the string “Preview” Bubbling Event – Flows from root element to child element Event will not be raised, once it is marked as handled E.g. PreviewMouseDown (tunnel) on root element. PreviewMouseDown (tunnel) on intermediate element #1. PreviewMouseDown (tunnel) on source element #2. MouseDown (bubble) on source element #2. MouseDown (bubble) on intermediate element #1. MouseDown (bubble) on root element.

17 17 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Commands Commands are useful in centralizing command handling logic The same command might be accessed from a Menu, on a ToolBar, or through a keyboard shortcut. TextBox control has the built-in logic to handle the ApplicationCommand.Past The default command target will be the control that has the focus

18 18 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Shortcut Keys (Key Bindings) Shortcut keys can be implemented using Key bindings <KeyBinding Key=“V" Modifiers="Control" Command="ApplicationCommands.Paste" />

19 19 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Command Bindings We can provide our customer command bindings like this <CommandBinding Command="ApplicationCommands.Open" Executed="OpenCmdExecuted" CanExecute="OpenCmdCanExecute"/> void OpenCmdExecuted (object target, ExecutedRoutedEventArgs e) { MessageBox.Show ("The command has been invoked."); } void OpenCmdCanExecute (object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = true; }

20 20 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Custom Commands public static RoutedCommand CustomRoutedCommand = new RoutedCommand(); public void ExecutedCustomCommand(object sender, ExecutedRoutedEventArgs e) { MessageBox.Show("Custom Command Executed"); } public void CanExecuteCustomCommand(object sender, CanExecuteRoutedEventArgs e) { Control target = e.Source as Control; if (target != null) e.CanExecute = true; else e.CanExecute = false; } <CommandBinding Command="{x:Static custom:Window1.CustomRoutedCommand}" Executed="ExecutedCustomCommand" CanExecute="CanExecuteCustomCommand" />

21 21 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Resources <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> One Button One Label

22 22 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Styles <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

23 23 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Styles with a Key <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Button Content="Button1“ Style="{StaticResource MyStyle}" >

24 24 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Control Templates <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Border x:Name="border" CornerRadius="80“ Background="LightBlue"> <ContentPresenter Name="content“ HorizontalAlignment="Center" VerticalAlignment="Center"/>

25 25 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Control Template As a Resource <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <ContentPresenter Name="content" HorizontalAlignment="Center" VerticalAlignment="Center"/> <Button Content="Two Button" Template="{StaticResource MyTemplate}">

26 26 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Using Triggers <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

27 27 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Simple Data Bindings <TextBlock Text="{Binding Source={StaticResource myDataSource}, Path=PersonName}" />

28 28 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Simple Class to be Data-bound namespace WPFSampleApp { public class Employee { private string personName; public string PersonName { get { return personName; } set { personName = value; } } public Employee():this(string.Empty){ } public Employee(string personName) { this.personName = personName; }

29 29 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL One-way Data Bindings <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> Enter a Name: <TextBox Text="{Binding Source={StaticResource myDataSource}, Path=PersonName, UpdateSourceTrigger=PropertyChanged}" /> The name you entered: <TextBlock Text="{Binding Source={StaticResource myDataSource}, Path=PersonName}"/>

30 30 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Data-binding to Collections <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <ListBox Width="400" Margin="10" ItemsSource="{Binding Source={StaticResource empList}}">

31 31 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Collection Class namespace WPFSampleApp { class Employees : ObservableCollection { public Employees() { this.Add(new Employee("AAA")); this.Add(new Employee("BBB")); this.Add(new Employee("CCC")); }

32 32 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL XAML Browser Applications (XBAPS) Hello, Page!

33 33 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Page Navigation Navigate

34 34 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Using Frames We can use Frames to insert one page inside another page <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" WindowTitle="FrameHostPage" WindowWidth="250“ WindowHeight="250">

35 35 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Application Object Defining the Application <Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="App" StartupUri="MainWindow.xaml" /> Closing The application with an Exit code Application.Current.Shutdown(-1); Setting and Retrieving Application wide Properties Application.Current.Properties["CustomType"] = customType; CustomType customType = (CustomType)Application.Current.Properties["CustomType"];

36 36 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Handling Unhandled Exceptions <Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="App" StartupUri="MainWindow.xaml" DispatcherUnhandledException="App_DispatcherUnhandledException" /> public partial class App : Application { void App_DispatcherUnhandledException (object sender, DispatcherUnhandledExceptionEventArgs e) { // Process unhandled exception e.Handled = true; }

37 37 Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Application Session Ending <Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="App" SessionEnding="App_SessionEnding" /> public partial class App : Application { void App_SessionEnding(object sender, SessionEndingCancelEventArgs e) { string msg = “Do you want to close this Application?"; string title = “My Application"; MessageBoxButton buttons = MessageBoxButton.YesNo; MessageBoxImage icon = MessageBoxImage.Stop; MessageBoxResult result = MessageBox.Show(msg, title, buttons, icon); e.Cancel = (result == MessageBoxResult.No); }


Download ppt "Copyright ©2004 Virtusa Corporation | CONFIDENTIAL Windows Presentation Foundation Ruwan Wijesinghe."

Similar presentations


Ads by Google