Posted by: Zeeshan Amjad | September 9, 2009

Creating WPF Application without Application class


We have seen that we need at least two classes to make WPF application named Window Class and Application class. Application class is responsible to make the window message loop by calling the run method.

Application class is internally call the Run method of Dispatcher class. This function internally implement the windows message loop just like we used to have with Windows SDK.

If we can somehow do this stuff ourselves then we don’t need to create the instance of application class. Fortunately we can do it very easily.  Window class has two functions to show the window named “Show” and “ShowDialog”. The difference between theses functions are that “Show” will display the model less window, however “ShowDialog” will display the model window. It means that the control will not return to the next line until we close that window.

ShowDialog function is an interesting one. It restrict the execution of next instruction by calling the run function of the Dispatcher class. Here is a simple code to create WPF application without creating an instance of Application class.

  1: using System;
  2: using System.Windows;
  3: 
  4: public class MyWindow : Window
  5: {
  6:     MyWindow()
  7:     {
  8:         Title = "WPF Window without Application class";
  9:         Width = 400;
 10:         Height = 300;
 11:     }
 12: 
 13:     [STAThread]
 14:     public static void Main()
 15:     {
 16:         MyWindow win = new MyWindow();
 17:         win.ShowDialog();
 18:     }
 19: }
 20: 

Here is the output of this program.

WpfApplication

Advertisements

Responses

  1. […] We saw one example of creating WPF application without using Application class here. In that program we called the ShowDialog method of Window class. We can do this using the […]


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 )

Google+ photo

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

Connecting to %s

Categories

%d bloggers like this: