Windows Presentation Foundation (WPF)

  1. WPF History
    1. Work began in early 2000s by Microsoft under code name "Avalon"
    2. Effort to provide a clearer separation between the interface and business logic
    3. Avalon renamed WPF in July 2005
    4. WPF released in 2006 as part of .NET Framework 3.0
    5. Silverlight (released in 2007) is a subset of WPF
    6. Universal Windows Platform (UWP) apps is similar in many respects to WPF, uses XAML
  2. WPF Features
    1. Works on top of DirectX to provide more advanced UI features
    2. Support for 2D and 3D graphics, audio, and video
    3. GUI appearance and content manipulated with XAML
    4. WPF controls can be embedded in Windows Forms app and vice versa
    5. Uses vector-based graphics for resolution independence
  3. Extensible Application Markup Language (XAML)
    1. Pronounced "zammel"
    2. XML-based markup language for defining and arranging GUI controls
    3. Can be manually generated/edited by Visual Studio and Blend
    4. Example XAML document
      <Window x:Class="WPF_HelloWindows.Window1”   	
      	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      	Title="Hello WPF" Height="150" Width="250">
          <Grid Background="AntiqueWhite" >
              <Label x:Name="label1" VerticalAlignment="Center" 
      			HorizontalAlignment="Center">Hello, WPF!</Label>
          </Grid>
      </Window>
      

      XAML Window
    5. XAML documentation
  4. Creating a WPF application in Visual Studio
    1. Create a WPF Application project
    2. App.xaml
      1. Auto-generated file defining the Application object and settings
        <Application x:Class="HelloWPF.App"
                     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                     StartupUri="MainWindow.xaml">
            <Application.Resources>
                 
            </Application.Resources>
        </Application>
        
      2. StartupUri indicates XAML file to be executed first when app starts
    3. App.xaml.cs
      1. Code-behind file for App.xaml, which handles application-level events
      2. Example which looks for a command-line argument when Startup event is triggered
        namespace HelloWPF
        {
            public partial class App : Application
            {
        		private void Application_Startup(object sender, StartupEventArgs e)
        		{				
        			if (e.Args.Length == 1)
        				MessageBox.Show("Opening file: " + e.Args[0]);				
        		}
            }
        }
        
    4. MainWindow.xaml
      1. Main window of application
      2. All child elements go between <Window> tags
        <Window x:Class="HelloWPF.MainWindow"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                Title="MainWindow" Height="350" Width="525">
            <Grid>
                
            </Grid>
        </Window>
        
      3. <Grid> is a layout container that places widgets in rows and columns
    5. MainWindow.xaml.cs
      1. Code-behind file for XAML
      2. Handles events and calls business and data access logic in response
      3. Class inherits from System.Windows.Window
        namespace HelloWPF
        {
            public partial class MainWindow : Window
            {
                public MainWindow()
                {
                    InitializeComponent();
                }
            }
        }
        
    6. Other files
      1. obj\Debug\App.g.cs - Auto-generated file defining Main()
        public static void Main() {
        	HelloWpf.App app = new HelloWpf.App();
        	app.InitializeComponent();
        	app.Run();
        }
        
      2. obj\Debug\MainWindow.g.cs - Auto-generated file defining InitializeComponent() and all controls
        internal System.Windows.Controls.Label label1;
        
      3. obj\Debug\MainWindow.baml - Compiled binary (Binary Application Markup Language) of MainWindow.xaml
      4. Nice comparison of WinForm project and WPF project : Part 1, Part 2, Part 3