Friday, January 19, 2007

Compact Framework Woes

So as I discussed in previous blogs, at work I have been doing mobile development using .Net 2.0 and the Compact Framework (CF). For the most part I have found the compact framework fulfills about 90% of our needs. However the remaining 10% of our needs causes a lot of headaches for our team.

I understand the need for the CF to remain compact but it is surprising some of the things that were left out of it. A great example of this is form and control loading. In a WinForms project using the full framework; Forms and UserControls inherit from Control. Control implements a Load event. So this causes Forms and UserControls to have a Load event that is fired shortly before the control or form is loaded on the screen. So if you are creating your UI dynamically at runtime, you have a way to set properties before displaying but after construction.

In the CF, Control does not implement Load. Load is only implemented on the Form. Controls have a Paint event that can be used to do the setting of properties. Most people will say see the CF still allows you to do what you need, but here is the rub.

Besides not being consistent, let's say this dynamic UI needs to be somewhat portable so that it will run on a mobile device and on a PC. the code will run nicely on the hand held device, but on the PC you get this flickering coming from the controls because the use of the Paint event.

So to solve this problem what I did was rather simple, but really tedious. Since in CF you do have a Load event on the Form, I decided to use that. Every UserControl that can be placed dynamically on a form sets the Load event. This was a bit harder than I thought it would be because my natural thought was to do the following in a method that could be called during setup of the control but before loading of the form...

Parent.Load += new System.EventHandler(Control_Load);
or
((Form)Parent).Load += new System.EventHandler(Control_Load);

Since the parent is really a Control type, and casting it as Form could be dangerous because the controls parent could be a TabPage or a Panel, I ended up setting the form as an attribute on a base class of all these dynamic control and ended up setting the event like this...

this.ParentForm.Load += new System.EventHandler(Control_Load);

This solution will allow for this dynamic approach to work both on mobile devices and PCs.

There are other places in the CF where the implementation differs greatly from the full framework. For example threading, but that is a discussion for another day.

2 comments:

Anonymous said...

Hey Chris,
I am totally new to the CF world and am trying to learn it by porting a small c# tetris clone i had written into this world.
While working on that since a cpl of days, i faced this problem where user-controls dont have Load events. Googling about it got me to ur blog and i personally found it informative. I will be trying to do the Parent.Load += workaround that u have put up.
Thanks for the info! TC and happy coding!

Anonymous said...

Hi Chris,
Could you give more in deep explanation about the solution that you propose?

Thanks a lot...