Workspace Opener: A Short-Lived Detour through C# and WPF

Today I was struck, again, by just how much of a PITA it is to open up all the project folders and windows for any one of my projects. I briefly tried using batch files to do this, but they are hard to maintain. So for a change of place, I’m looking at Visual Studio 2010 and C#, to see if I can write some simple code that does this for me.

I’m not familiar with Windows Application development, but I am familiar with C# with the XNA game library, so I’m crossing my fingers.

Step the first: Launch Visual Studio 2010, create a new Windows Presentation Foundation (WPF) solution. This is the newest sauce, as of 2010, though Windows 8 will introduce that new Javascript/HTML5 based technology.

A Solution is a collection of related Projects. Starting out, I have just one C# project included.

After creating the new solution, I’m popped into the main C# application window, MainWindow.xaml and its “code-behind” file MainWindow.xaml.cs. XAML is the XML Application Markup Language that is used to define GUI interfaces. The CS is the C# file, implemented as a “partial class”. A partial class is a way of splitting up a class declaration into multiple source files, which makes it easier to hide a lot of the implementation nitty-gritty from your core logic. This makes for a cleaner experience.

Looking into the .cs file, let’s see what we can see. Immediately, there is a declaration of class MainWindow, which inherits from Window. The public method MainWindow() is called, presumably after a ton of other initialization the occurs behind-the-scenes.

At this point, it’s probably not a bad idea to review what’s being included here by default, as these are probably the baseline packages that a simple C# application needs. I’m looking at the online help for the packages:

  • System – contains fundamental classes: commonly-used values and reference data types, Events, Event Handling, Interfaces, System Attributes, Processing Exceptions
  • System.Collections.Generic – data structures like collections, dictionaries
  • System.Linq – something to do with queries of the “Language Integrated Query”. no idea what it is.
  • System.Text – character encoding and string manipulation libraries
  • System.Windows – Types used for WPF applications, including animation, controls, data binding, and type conversion
  • System.Windows.Controls – classes to create UI elements
  • System.Windows.Data – classes used for binding properties to data source
  • System.Windows.Documents – types to suppored FixedDcoument, FlowDocument, and XML Paper
  • System.Windows.Input – abstractions for mouse, keyboard, stylus, common input manager…
  • System.Windows.Media – integration of rich media in WPF
  • System.Windows.Media.Imaging – encode and decode bitmap images
  • System.Windows.Navigation – support navigation between windows and journaling
  • System.Windows.Shapes – a library a shapes accessible in XAML or code

It was instructive to browse this listing to see what’s there. It’s kind of exciting.

Anyway, next step is to see what is in the Window class. I need to know where execution begins, which means I need to know exactly when I can initialize my own data structures, when I can access application data structures, and when my application is finally ready to accept input from the user.

I actually purchased a book on this some time ago, “WPF 4 Unleashed”, so maybe it will have a pertinent chapter or two on the subject…nope. This is a more advanced book, so it doesn’t have a walkthrough in the way I’m looking. So I’m now stuck downloading Visual Studio documentation (1G+), that I neglected to install locally before.

In the meantime, let me see if I can puzzle this out. If WPF works like every other event-driven application, by the time MainWindow() executes, we’re good! It’s a matter then of creating anything programmatic we need like loading data, initializing data structures, and so forth.

I also just noticed an App.xaml that had escaped my attention. It has its own codebehind, I imagine. The xaml defines a bunch of application resources (which don’t exist). In the code-behind, there is an App of class Application. I guess this is where I would put my actual application handling code. MainWindow would be for code related to the MainWindow.

At this point, it might be more instructive to look at a sample app and see what they’re doing. I just stumbled upon the Microsoft All-In-One Code Framework which promises to have better code samples. It’s difficult to browse, though, so I’ve given up on it.

Next, I just did a search for WPF Code Samples, and found one called Notepad Demo. As I’m interested in doing some text-based stuff, I’ll have a look at this one. After much screwing around with secret download locations and awful archiving tools (WinZip extractor? Really? That POS?) I was able to finally open the sample and poke around.

From what I can tell:

  • Every WPF application has at least one Window.xaml and an Application.xaml (though it may not be named that)
  • Execution begins in the xaml.cs file
  • Events are automatically directed to the xaml.cs file

I’ve now run out of steam, and need to take a break. Disappointing that it is so difficult to find one decent starting application sample to introduce one cleanly to the making of any kind of application that does stuff that would actually be fun.

UPDATE: this msdn intro might be the place to start.