Skip to content

How to create WPF application from scratch.

May 3, 2009

Our goal.

Our goal is to create most basic application in WPF. It is an application so we’ll create an Application instance. It is a WPF so we’ll add some UI element, let it be an empty Window instance.

Project and it’s basic “must have” settings.

First create new empty project. I like to start with an empty one in order to have everything under control. Later on it’s easy to create template out of our simple application and use it instead of doing everything again and again. When you’re done go to project properties and change application type to Windows Application.

Application definition.

Create Application.xaml file with following content.

  1: <Application
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     StartupUri="MainWindow.xaml"
  4:     />

After compiling that project compiler will complain, among other things, about lack of Main method. Visual Studio is able to generate Main method based on that file. In order to do that you have to change in Application.xaml’s properties it’s build action to ApplicationDefinition.

Line two works like a bunch of using statements. It imports a few core namespaces so that we can use core WPF types.
Line two instructs Application object to open resource defined in XAML. We can open resources of type Window of NavigationWindow.

Window definition.

In our case MainWindow.xaml contains a definition of new Window class which looks like this:

  1: <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"/>

What we have here is like above: import of core WPF namespaces and definition of new object of class Window.

And that’s it. We’ve got our first GUI application written using WPF library:) I won’t add anything to that example since the core idea was to show the most basic and simple application in WPF.

Why not even shorter?

It would be even more compact if we could treat Application as something similar to a ContentControl

  1: <Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
  2:     <Window />
  3: </Application>

but Application cannot have any content so we must stick with this two file solution.

Advertisement

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.