FormShellApplication in CAB

In the Composite UI Application Block, the windows application that is the host uses a class to run the project.  In C#, this is the default setup for a windows application; it includes a Program.cs file that runs the default form.  This is a great setup for CAB, because you need to change the implementation to this:

public class Program : FormShellApplication<WorkItem, MainForm>
{
   
}

This shell represents a windows forms shell that you created with your windows forms project, that the CAB project represents.  The WorkItem first parameter in the generic parameter list is the root type of object to implement, which  means we want to implement any class that inherits from WorkItem.  The second is the type of shell form to implement.  In our class, we have a static definition of:

[STAThread]
static void Main()
{
    Program program = new Program();
    program.Run();
}

The run method handles the initialization and preprations of the CAB objects in the block.  The list of services available after instantiation (as provided by the documentation) is:
  • SimpleWorkItemActivationService
  • FileCatalogModuleEnumerator
  • WindowsPrincipalAuthenticationService
  • ModuleLoaderService
  • DataProtectionCryptographyService
  • TraceSourceCatalogService
  • CommandAdapterMapService
  • WorkItemExtensionService
  • WorkItemTypeCatalogService
  • ControlActivationService
That is a lot of services!  In the FormShellApplication class, we have methods we can override, one of the more common of which is this:

protected override void AfterShellCreated()
{
    base.AfterShellCreated();
}

This method fires after the shell is created (as the name gives away), which you can do additional things, like work with these services after the shell is created and setup.  More commonly, this method will register UI extention sites, which registers certain UI elements so that you can use a command name to interact with it, instead of interacting with the object itself (more on this later).

Another handy method is the OnUnhandledException method, which receives unhandled exceptions, and allows you handle them in some way, by notifying the user, or writing an event, such as this declaration:

public override void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
{
    base.OnUnhandledException(sender, e);

    Exception ex = e.ExceptionObject as Exception;
    string message = string.Empty;
    string title = "Error";

    if (ex != null)
    {
        message = ex.ToString();
        EventLog log = new EventLog("Application");
        log.Source = "MyApp";
        log.WriteEntry(message, EventLogEntryType.Error);

        MessageBox.Show(message, title);
        Application.Exit();
    }
}

Comments

No Comments

The leading UI suite for ASP.NET - Telerik radControls
Outstanding performance. Full ASP.NET AJAX support. Nearly codeless development.