C# WPF First Window Introduction


This entry is part 4 of 18 in the series C# WPF

What would be the first thing you want to do when you are beginning to code a Windows application? You might consider starting with the About window. You will likely have a few windows to develop along the way and your designer should have already provided you with a colour scheme and some design elements for your application.  This article describes some of the steps you need to take. These steps are based on the Karli Cards application in the book Beginning C# 6 Programming with Visual Studio 2015 by Wrox Press, page 436. For this example, I will use myApp as the official name of the application.

First Window

Create a new WPF application and name it myApp Gui Name the Solution myApp In Solution Explorer, right-click the myApp Gui project and select Add -> Window Name the window About.xaml Set the properties of the window (Height, Width, MinWidth, MaxWidth, ResizeMode etc)
Now, according to your design, you add controls and set their properties Right-click the project and select Add -> New Folder, create a directory named Images Right-click the new directory in Solution Explorer and select Add -> Existing Item Browse to all of your images, select them, click Add.

Add any event handlers and code for buttons, such as the Close button (this.Close();)
In Solution Explorer, double-click App.xaml and change the name MainWindow.xaml to About.xaml
Run the application (F5)

Second Window

Your second window could be an Options window. It may look similar to the About window and therefore it is a good time to develop it now. We can reuse some of the code from the first window. Here are some general steps to do just that.

Right-click the project in Solution Explorer and choose Add -> Window and name it Options.xaml
Delete the Grid control that is inserted by default
Open the About.xaml window you created earlier and copy the Grid control and all its content and paste it into the new Options.xaml file.
Change window properties (Title=”Options” …)

Delete controls as needed and add controls as needed.

Open the App.xaml file and change StartupUri to Options.xaml

Run the Application

A Class to Store Options

Your users will want to store data when they use your application. When they pick their options they will want their choices to persist throughout the use of the application and probably after it has been shut down and restarted as well. To store their options while using the program and after closing and re-opening the Options window, we will use a class. To start with, the new class will just have some properties and will be marked Serializable to make it possible to save it to a file. An example of this will be presented in another post.

Series Navigation<< C# WPF without XAML – Window ClassC# WPF Create a Simple Binding Object in Code Behind >>