Sunday, December 2, 2007

Working with forms in Windows Mobile

As we've already seen, a mobile application UI is naturally modal. Does it mean every form should be open using Form.ShowDialog()?

Of course it doesn't. There are several business requirements (like complex navigation schemas), design and architectural constrains or even basic requirements like the need of implement a wizard that require a different approach. Here's where we need to face new design challenges to provide a rich user experience in our application.

How to show a form in Windows Mobile

It depends on the use case, but you should take care of the form title, the Running Programs window and how it interacts with the multitasking environment.

PocketPC and the Running Programs list

You can show a .Net CF form using Form.Show() or Form.ShowDialog(). In both you'll get in the Running Programs list one instance for each open form, as they were different applications, and you can navigate to any form from that list directly (it will appear disabled if using Form.ShowDialog()).

The first method to prevent this, is every time you show a form, you should change the current form title to an empty string (String.Empty), in the Running Programs List you'll see only the new form title. When you come back, you should change the title (Form.Text property) again to the original title and it will be the one on the Running Programs List. Here you have a sample code:

private void menuItem1_Click(object sender, EventArgs e)
Form2 form = new Form2();
this.Text = String.Empty;
private void Form1_Activated(object sender, EventArgs e)
this.Text = "Form1";

The second method is using Form.Owner (valid only for .Net CF 2.0+). In this case you should set the Form.Owner property of the new form to the current form, and the title will remain the current form's title. Due to this, we need to change the current title to the new form title and set it back when we come back to the form.

It makes sense if you're using Form.ShowDialog() or if you have a controller form and you're showing kind of a wizard opening different forms and all of them have the same "controller form" as the owner (which is where you start and where you finish the wizard).

Here is the sample code for method 2:

private void menuItem2_Click(object sender, EventArgs e)
Form3 form = new Form3();
form.Owner = this;
this.Text = form.Text;
private void Form2_Activated(object sender, EventArgs e)
this.Text = "Form2";

Handling Form_Activate and Form_Deactivate events

Form_Activate is not only useful to set the form title back when the user returns to the form. As we've seen before, Windows Mobile is a multitasking environment and we should deal with it. Here's when Form_Activate and Form_Deactivate are fully helpful.

You can stop unnecessary expensive processes if the application is in background on Form_Deactivate, and start them again when Form_Activate in order to preserve processor, memory and fundamentally battery.

Keep it in mind when you develop more mobile applications.

1 comment:

Anonymous said...

Thanks for your post! I'm now developing a mobile application and I've got a problem:

Whenever my application tries to connect to Internet automatically, the connection pop up appears and my form becomes invisible. It seems that my form is pushed back or hides when the pop up balloon comes into the scene :S

Is there any way to prevent this with .NET CF 3.5?