HelloWPF.cs - First WPF Application

A tutorial example is provided on how to create a simple Windows Presentation Foundation (WPF) application with a single empty window.

After learning the two very basic Windows Presentation Foundation (WPF) classes, we can start to write a simple WPF application with a window.

Here is my first WPF application, HelloWPF.cs, with an empty window.

// HelloWPF.cs
// Copyright (c) 2016 HerongYang.com. All Rights Reserved.

using System.Windows;
public class MyWindow : Window {
   public MyWindow() {
      Width = 200;
      Height = 200;
      Title = "Hello WPF";
   }

   [System.STAThread]
   public static void Main() {
      Application app = new Application();
      app.Run(new MyWindow());
   }
}

Note that we have to mark program entry point with the [System.STAThread] attribute. STAThread stands for STA (Single-Threaded Apartment) Thread, if the program is WPF application.

Now let's try to try to compile HelloWPF.cs with .NET Framework 4.6.1 SDK:

C:\herong>set "NET=\windows\Microsoft.NET\Framework\v4.0.30319"
C:\herong>%NET%\csc HelloWPF.cs
Microsoft (R) Visual C# Compiler version 4.6.1055.0

HelloWPF.cs(5,25): error CS0246: The type or namespace name 'Window'
could not be found (are you missing a using directive or an assembly
reference?)

Ok. The compiler can not find the PresentationFramework.dll assembly automatically. We need to help it with the /reference option:

C:\herong>set "REF=C:\Program Files\Reference Assemblies\Microsoft
   \Framework\.NETFramework\v4.6.1"

C:\herong>%NET%\csc /reference:"%REF%\PresentationFramework.dll"
   HelloWPF.cs

Microsoft (R) Visual C# Compiler version 4.6.1055.0

HelloWPF.cs(5,14): error CS0012: The type 'System.Windows.UIElement'
is defined in an assembly that is not referenced. You must add a
reference to assembly 'PresentationCore, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

c:\Program Files\Reference Assemblies\Microsoft\Framework
\.NETFramework\v4.6.1\PresentationFramework.dll: (Location of symbol
related to previous error)

HelloWPF.cs(5,14): error CS0012: The type 'System.Windows.Markup
.IQueryAmbient' is defined in an assembly that is not referenced. You
must add a reference to assembly 'System.Xaml, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089'.

c:\Program Files\Reference Assemblies\Microsoft\Framework
\.NETFramework\v4.6.1\PresentationFramework.dll: (Location of symbol
related to previous error)

...

Ok. Additional assembly files are needed: PresentationCore.dll and System.Xaml.dll:

C:\herong>%NET%\csc /reference:"%REF%\PresentationFramework.dll";
   "%REF%\PresentationCore.dll";"%REF%\System.Xaml.dll" HelloWPF.cs

Microsoft (R) Visual C# Compiler version 4.6.1055.0

HelloWPF.cs(5,14): error CS0012: The type 'System.Windows
.DependencyObject' is defined in an assembly that is not referenced.
You must add a reference to assembly 'WindowsBase, Version=4.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35'.

c:\Program Files\Reference Assemblies\Microsoft\Framework
\.NETFramework\v4.6.1\PresentationCore.dll: (Location of symbol
related to previous error)

c:\Program Files\Reference Assemblies\Microsoft\Framework
\.NETFramework\v4.6.1\PresentationFramework.dll: (Location of symbol
related to previous error)

Looks like one more assembly file is needed: WindowsBase.dll, which is required by PresentationCore.dll, which is required by PresentationFramework.dll.

C:\herong>%NET%\csc /reference:"%REF%\PresentationFramework.dll";
   "%REF%\PresentationCore.dll";"%REF%\System.Xaml.dll";
   "%REF%\WindowsBase.dll" HelloWPF.cs
Microsoft (R) Visual C# Compiler version 4.6.1055.0

Finally, the compiler is happy and produces the executable program: HelloWPF.exe. Now let's run it:

C:\herong>dir helloWPF.*
  374 HelloWPF.cs
4,096 HelloWPF.exe

C:\herong>helloWPF.exe

Congratulations, we have a small window showing up on the screen!

First WPF Application
First WPF Application

Table of Contents

 About This Book

 Introduction of C# (C Sharp)

 Data Type and Variables

 Logical Expressions and Conditional Statements

 Arrays and Loop Statements

 Data Type Features

 Floating-Point Data Types

 Passing Parameters to Methods

 Execution Environment Class

 Visual C# 2010 Express Edition

 Class Features

 C# Compiler and Intermediate Language

 Compiling C# Source Code Files

 MSBuild - Microsoft Build Engine

 Memory Usages of Processes

 Multithreading in C#

 Async Feature from C# 5

 System.IO.FileInfo Class

 System.Diagnostics.FileVersionInfo Class

WPF - Windows Presentation Foundation

 What Is WPF?

 System.Windows.Application Class

 System.Windows.Window Class

HelloWPF.cs - First WPF Application

 System.Windows.Controls Namespace

 WPF Button Click Test

 Compiling WPF Applications with MSBuild

 What Is XAML?

 Partial Classes and Partial Methods

 Archived Tutorials

 References

 Full Version in PDF/ePUB