C# • Build a Source-Behind File

Listing 5. Note the partial keyword in the class declaration in the code-behind file. This keyword states that the compiler should merge this class definition with other definitions of the same class. This allows you to provide multiple partial definitions of a class, each in a separate source file, that the compiler combines into a single class definition in the resulting assembly.

namespace IntroLonghorn {
   using System;
   using MSAvalon.Windows;
   using MSAvalon.Windows.Controls;
   using MSAvalon.Windows.Media;

   public partial class CodeBehindSample {
      MSAvalon.Windows.Controls.SimpleText txtElement;
      MSAvalon.Windows.Window mainWindow;

      protected override
      void OnStartingUp 
         (StartingUpCancelEventArgs e) {
         base.OnStartingUp (e);
         CreateAndShowMainWindow ();

      private void CreateAndShowMainWindow () {
      // Create the application's main window
         mainWindow = new 
            MSAvalon.Windows.Window ();

      // Add a dark red, 14 point, "Hello World!" 
      //text element
      txtElement = new
       MSAvalon.Windows.Controls.SimpleText ();
      txtElement.Text = "Hello World!";
      txtElement.Foreground = new
      txtElement.FontSize = new FontSize (14,
      mainWindow.Children.Add (txtElement);
      mainWindow.Show ();