Create WPF Applications without XAML
Not all WPF applications are written using XAML. There are some causes why the developer wants to accomplish his goal without using XAML. For example:
- There is no know-how of XAML and no time to learn it in depth but the need to create - for example - animations.
- WPF should be used in environments where XAML is not available (micro framework).
- The user interface should be generated at runtime.
But how can you create a WPF application without XAML?
First of all it’s the simpliest way to create a standard WPF application using Visual Studio. Delete the default files App.xaml and Window1.xaml (the code-behind files are deleted automatically if you delete the XAML files). The advantage of this way is that all references are included for you. If you create a blank solution you have to add the needed references manually:
- WindowsBase
- PresentationCore
- PresentationFramework
The next step is to add a new class App.cs to your project. This class inherits Application and contains the needed entry point:
public class App : Application { [STAThread] static void Main(string[ args) { Application app = new Application(); app.Run(new MainWindow()); } }
This will create a new instance of Application. Furthermore the Run-method is called to start the application using a new instance of MainWindow which we will create in the next step.
public class MainWindow : Window { StackPanel mainPanel = null; Label label1 = null; Button button1 = null; private void InitializeComponent() { mainPanel = new StackPanel(); mainPanel.Name = “MainPanel”; mainPanel.Orientation = Orientation.Vertical; label1 = new Label(); label1.Content = “Code Only App”; label1.FontSize = 18; button1 = new Button(); button1.Content = “Close”; mainPanel.Children.Add(label1); mainPanel.Children.Add(button1); this.Content = mainPanel; } public MainWindow() { InitializeComponent(); Init(); button1.Click += new RoutedEventHandler(button1_Click); } void button1_Click(object sender, RoutedEventArgs e) { Application.Current.Shutdown(0); } private void Init() { this.Title = “Code Only App”; this.Height = 300; this.Width = 300; } }
MainWindow inherits from Window which means that MainWindow represents a window to display. Within InitializeComponents our needed elements are instantiated and added to the content of the window to get displayed.
And here is a screenshot showing the result (of course you can use custom styles and templates to get it look like a little bit better).
This is a very simple example but it is - of course - possible to implement a much more complex application.
April 23rd, 2008 at 9:46 am
[…] hogy talán könnyebben megértené a WPF-et, ha a jólismert C#-ban tolhatná a programozását, az alábbi linken egy egyszer? kis bemutatót talál ehhez. Minden ott van C#-ban is, ami kell, legfeljebb egy […]
April 23rd, 2008 at 9:58 am
To reduce the number of lines, you can use:
StackPanel mainPanel = new StackPanel {Name = “MainPanel, Orientation = Orientation.Vertical};
Label label1 = new Label {Content = “Code only App”, FontSize = 18};
Button button1 = new Button {Content = “Close”};
private void InitializeComponent()
{
mainPanel.Children.Add(label1);
mainPanel.Children.Add(button1);
this.Content = mainPanel;
}
this looks more similar to xaml, but it’s a great invention in new C# 3.5
April 23rd, 2008 at 10:34 am
@TWiStErRob: Yes you are right. Using Object initializers you can reduce sourcecode. But this isn’t always the best way to give an example to show how things work.
Furthermore be careful: Currently there is no C# 3.5, just 3.0 and this is part of the .NET Framework 3.5.
April 25th, 2008 at 1:44 pm
[…] BLOG: Create WPF Applications without XAML - we used to start many of our demos for WPF this way and then walk people into XAML…to show that […]
May 2nd, 2008 at 7:22 am
[…] is no know-how of XAML and no time to learn it in depth but the need to create - for example - ahttp://www.dotnet-blog.com/index.php/2008/04/21/create-wpf-applications-without-xaml/Garden Calendar Seattle Post-IntelligencerA look at upcoming gardening […]