Alarm Clock Sample -- Part 2

Now that I know that myapp.xaml holds the key to the application's startup, I want to look closer at where the AppStartup method lives: myapp.xaml.cs.

This file is a complete C# file leveraging the .NET Framework Class Library. The first thing I notice is the file uses six namespaces all ultimately from the System namespace. System is clearly one of the most important namespaces of any C# project. From the descriptions over at the .NET Framework Reference, my first take on their usage is

  • System is required for the most fundamental aspects of the application like class and struct types (eg. the Object class from which all other classes are derived
  • System.Windows is required because this will be a WPF application
  • System.Data has no apparent usage at first glance
  • System.Xml is required to parse XAML files since they are XML files
  • System.Configuration is required to configure the application (i'm guessing configuration is detailed in the XAML files)
  • System.Windows.Media is required for drawing stuff (like the alarm clock image) and producing sound (I don't think this alarm clock actually has an alarm though)

You don't actually have to give everything a namespace but it seems to be a best practice guideline since Microsoft always does it. Here, the application is given the namespace Microsoft.Samples.WinFX.AlarmClock. So far, I don't think they are necessary unless you plan to write your own library (compiling as DLLs).

On to the meat: public partial class MyApp : Application.

Of course, the application class should be "public" but what's with "partial"? In Part 1, I mentioned that the build process will automagically create an entry point method so the application knows how to start, I'm guessing the build process also creates a class based on myapp.xaml which gets put into separate file from the compiled myapp.xaml.cs in which case "partial" is needed to specify this is just part of the class definition. I gathered as much from Building a WPF Application

In addition, a language-specific code file is generated for every XAML file. For example, for a Page1.xaml page in a Visual Basic project, a Page1.g.vb is generated; for a Page1.xaml page in a C# project, a Page1.g.cs is generated. The ".g" in the file name indicates the file is generated code that has a partial class declaration for the top-level element of the markup file (such as Page or Window). The class is declared with the partial modifier in C# (Extends in Visual Basic) to indicate there is another declaration for the class elsewhere, usually in the code-behind file Page1.xaml.cs.

The main application class is given the name "MyApp" and inherits from Application which comes from the System.Windows namespace. The Application class "Encapsulates a Windows Presentation Foundation (WPF) application." That makes sense, Alarm Clock Sample is a WPF application.

Finally, the application process is run and, on startup, the AppStartup method is called. I already talked about the prototype so let's look at the body:

  • TraditionalClock tradClock = new TraditionalClock(); There's an object TraditionalClock defined somewhere (probably in traditionalclock.xaml and traditionalclock.xaml.cs)
  • tradClock.Opacity = .6f; Clearly, this will set the Opacity of the object (which, by now, I'm assuming consists of at least the image of the clock and hands)
  • tradClock.Show(); Most likely renders the clock to the screen.

The other method, void clockWindow_Closed(object sender, EventArgs e) is pretty straightforward now that I understand the AppStartup method. It is my understanding that every method triggered by an event will have an "object sender" and "EventArgs e" argument where EventArgs may be different depending on the event. The body of this method merely contains Application.Current.Shutdown(0); which speaks for itself but the .NET Framework Reference gives some more input: The Current property is thread safe and allows access to the application's instance in the current AppDomain (kind of like a sandbox for each executing application) from any thread.

No comments:

Post a Comment