This was my first experience with the Microsoft Compact Framework. The application is developed in Visual Studio 2005 and is targeted to run on Pocket PC 2003 devices. Basically, I had to extend a simple application that had only one form to add some functionality and a few more forms. I understand that the forms in Compact Framework are treated a bit differently compared to a desktop application. What to use for navigation between forms, Show() or ShowDialog()? I decided to use Show() because I have only about 5 forms, most of those are very simple and also, my application will be the only one running on the device. So I thought, if I create each form once and keep them all in memory, just showing and hiding them, it may use more memory, which I do not care that much about, but be easier on the device battery. Okay, I may be saying total nonsense here - I have about 7 days Compact Framework development experience at this very moment.
So I have a dictionary where all existing forms are kept.
private static Dictionary_applicationForms = new Dictionary ();
And the function that gets the form from the dictionary by name.
internal static Form GetFormByName(string formName)
{
if (_applicationForms.ContainsKey(formName))
{
return _applicationForms[formName];
}
else
{
Form newForm = CreateFormByName(formName);
AddFormIfNotExists(newForm);
return newForm;
}
}
And the function to create a form if it has not been yet created.
private static Form CreateFormByName(string name)
{
Form form = new Form();
switch (name)
{
case Constants.frmFirst:
form = new frmFirst();
break;
...
case Constants.frmLast:
form = new frmLast();
break;
default:
form = new frmLast();
break;
}
return form;
}
And the function to add the form to the dictionary if it is not there.
internal static void AddFormIfNotExists(Form frm)
{
if (!_applicationForms.ContainsKey(frm.Name))
{
_applicationForms.Add(frm.Name, frm);
}
}
And when I need to show another form, I get it from the dictionary and show, and hide the current form.
internal static void ShowFromForm(Form source, string targetName)
{
Form frm = GetFormByName(targetName);
frm.Show();
source.Hide();
}
There's a bit more to it, sometimes I need to find which form is currently visible etc, but these are the core things. Stupid? Good enough? I don't know ...
by Evgeny. Also posted on my website
No comments:
Post a Comment