One of the hardest parts of writing apps for CF is the handling of the different forms, and also the navigation between the forms. The handling involves loading the forms (that takes time), showing the forms (and hiding the other forms to prevent strange behavior when switching between apps), and the memory management of forms (especially if they contain many controls, they use a lot of memory). After years with different form handling solutions (stacks with preloaded forms, etc), I finally decided to leave all those troubles behind by only having one form in my apps.
The single form in my TouchUI apps is usually called MainForm and even if it's not a part of the framework, it's really the main controller for the user interface. That is where initialization takes place, and all the events are fired in that single form. Each event (or overridden method) then gets passed on to the currently active dialog. A dialog is a TouchUI class that represent a logical form in the app, and the MainForm host a stack of currently loaded dialogs to ease navigation back up the dialog hierarchy. This is the first code (in the form's Load event) that runs when the app starts:
dialogStack = new List<Dialog>();
MainDialog mainDialog = new MainDialog(...);
dialogStack.Add(mainDialog);
As the dialog stack is a simple List, most of the other code in the MainForm will access the currently shown dialog with:
dialogStack.Last()
Here are some examples:
private void MainForm_MouseUp(object sender, MouseEventArgs e)
{
dialogStack.Last().MouseUp(new Point(e.X, e.Y));
}
private void MainForm_KeyDown(object sender, KeyEventArgs e)
{
dialogStack.Last().KeyDown(e);
}
if(dialogStack.Last() is MainDialog)
{
MainDialog mainDialog = (MainDialog)dialogStack.Last();
DetailDialog detailDialog = new DetailDialog(...);
dialogStack.Add(detailDialog);
}
The last code snippet is from the logic that navigates from the main dialog to a new dialog, and as you can see it resembles the initial code above. Therefore, moving up the hierarchy means to simply remove the last dialog in the stack (list).