Welcome to MSDN Blogs Sign in | Join | Help

WPF Farm: Simple WPF

It can be helpful to start from the beginning when working with new technologies. This post explains how to create a minimal WPF application that produces a single window with a gradient in it, as shown in Figure 1. The point of this exercise is to build the app from scratch, choosing File | New Project | Empty Project rather than File | New Project | WPF Application. The benefit of this exercise is to simply see what ingredients go into the production of a minimal WPF program.

 

AColorfulWpfWindow

Figure 1: A Simple WPF window with a gradient.

  1. Begin by choosing File | New Project | Empty Project from the Visual Studio 2008 menu.
  2. Choose Project | Add Class from the menu and add a new C# class called Program.
  3. Right click on the References section in the Solution Explorer and remove System.Data and System.Xml, but leave System. Add the following References as shown in Figure 2:
    1. PresentationCore
    2. PresentationFramework
    3. WindowBase
  4. Remove System.Linq and add the following to the program's using statements
    1. System.Windows
    2. System.Windows.Media
  5. Use the svm snippet to add a Main method to your code and use the ctor snippet to add a constructor to your code
    1. Put the [STAThread] attribute above the main method
  6. Fill in the main method and the constructor as shown in Figure 1
  7. Select from the menu Project | Project Properties and set the output type to Windows Application
  8. Have class Program derive from class Window
    1. class Program: Window
  9. Optionally right click on the editor and choose Organize Usings | Remove and Sort from the menu
  10. Press F5 to run the program. You should see the window shown in Figure 1.

 

References

Figure 2: The References section from the SimpleWpf project.

The complete source code to this project is shown in Listing 1. You can see that an Application object is created in the Main method, and that a few minimal fields of the window are filled out in the constructor. I also create a WPF LinearGradientBrush and set it as the Background for the window.

using System;
using System.Windows;
using System.Windows.Media;

namespace Project1
{
    class Program: Window
    {
        [STAThread]
        static void Main(string[] args)
        {
            Application application = new Application();
            application.Run(new Program());
        }

        public Program()
        {
            Width = 320;
            Height = 260;
            Title = "A Colorful Window";
            Background = new LinearGradientBrush(Colors.AliceBlue, Colors.Aquamarine,
                new Point(0, 0), new Point(1, 1));   
        }
    }
}

I should perhaps end by reminding you that the simplest way to create a WPF application in Visual Studio is to choose File | New Project | WPF Application. I have shown you this alternative technique simply because I hope you find it interesting or entertaining. It also illustrates that it is possible, though not necessarily recommended, to build WPF applications without using XAML.

The complete Source is on my LINQ Farm. Here is direct link to the download.

kick it on DotNetKicks.com
Published Saturday, June 14, 2008 12:09 AM by Charlie Calvert
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# WPF Farm: A Simple WPF Program

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Saturday, June 14, 2008 3:11 AM by DotNetKicks.com

# re: WPF Farm: Simple WPF

Greate...

I hope success for u.

have a good time.

Saturday, June 14, 2008 11:34 AM by Sara

# re: WPF Farm: Simple WPF

Greate...

I hope success for u.

have a good time.

Saturday, June 14, 2008 11:37 AM by Sara

# re: WPF Farm: Simple WPF

Wow, I didn't know that you can create a working WPF app without using the Visual Studio WPF project template! I thought you'd need some .g.cs autogeneration magic - but apparently not!!

Nice one Charlie!

Sunday, June 15, 2008 2:12 AM by Kirill Osenkov

# re: WPF Farm: Simple WPF

I was looking for something like this...

Sometimes the best way to learn something is to learn from scratch, and I believe WPF is one of those things.

thank you  very much.

Monday, June 16, 2008 11:16 AM by Harold Gómez

# re: WPF Farm: Simple WPF

Those looking for more information about generating WPF applications from scratch might want to check Charles Petzold's book "Applications = Code + Markup".  Petzold tends to teach how to do things from scratch, and this one is no exception.  There's a lot of benefits to knowing the XAML designer and project templates aren't magic, so I recommend it!

Monday, June 16, 2008 1:17 PM by Owen Pellegrin

# Link Listing - June 17, 2008

MSBuild MSBuild Reserved Properties [Via: Sayed Ibrahim Hashimi ] Sharepoint Adding Copy and Paste...

Thursday, June 19, 2008 12:04 AM by Christopher Steen

# re: WPF Farm: Simple WPF

I guess WPF requires following Namespaces DLLs:

PresentationCore.dll

PresentationFramework.dll

WindowsBase.dll

System.dll

Totally different from forms, I guess. Which is kind of interesting as it is a parallel develpment to Windows.Forms and no dependncies, atleast that is what I understood.

Thursday, June 19, 2008 9:13 PM by Fact Pandit

# re: WPF Farm: Simple WPF

I like how the C++/CLI to get a blank WPF window is just:

Application().Run(%Window());

It makes a very nice change from the equivalent in the Windows 3.1 edition of Petzold!

Monday, June 23, 2008 11:32 AM by Daniel Earwicker

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker