Saturday, May 27, 2006

C# programming for the Compact Framework part 2

If you followed the steps from part 1 you've created a .NET CF program that runs on your PocketPC. No doubt you also tried to add functionality to the basic program, and soon found out that it isn't all good clean fun.
First, whenever you add a control from the sidebar, the IDE automatically adds a new reference to the required .NET assembly so the compiler complains about 'duplicates' and will not compile untill you remove the reference. This is not a real big problem, just don't forget to remove it before you compile. Also when you add controls by just copying them from the form (select the control you need, press [Ctrl]C and [Ctrl]V to add a copy) the IDE does not add a new reference.
Second, and this is worse, whenever you change something on the form, the IDE re-creates the form.designer.cs file, and all the CF-incompatible lines you just removed are back. So I created a little program (in C# ofcourse) that walks throught all .designer. files in a directory, finds lines with unsupported CF statements and turns them into comment. You can find it here.
Actually it loads a file named 'Exclude.txt' that contains the unsupported statements, and checks every line in the source code to see if it contains on of the words in the Exclude-file. If a match is found AND the source code line is not already a comment, the line is turned into a comment by adding '//' at the beginning of the line. I agree it's a rather crude solution, but it works fine. So if we enter this program in the 'Project->Project Options->Build Events' page as the 'Pre-build event', it is executed every time when we compile our code from the IDE, and there will be no error messages like 'System.Windows.Form.ComponentX does not contain a definition for MethodY'
Since the CFPreprocessor program is very basic, you should take care of the following:
- If the pre-build event is configured as shown above, the CFPreProcessor.exe file must be in the 'bin\Debug' directory of the project to be compiled, because this is the default directory when the compiler starts. It's probably better specify the full path (E.G. C:\CF\CFPreprocessor.exe)
- There are no warning messages in the IDE when the program runs or fails. It just writes a 'CFPreprocessor.log' file with each run, so if you think something is going wrong, just open this file to see what happened.
- Just add things to the Exclude.txt file if you run into another unsupported feature. I first considered it an option create a full list of unsupported methods from the 'Compact Framework Helpfile', but I think the program will soon get very slow, since it will have to check every line of code against every line in the Exclude file. I think it is best to keep this file as short as possible.

Another issue when programming for the CF is that it is really 'compact', so apart from the unsupported methods there are also a lot of controls that are not supported at all. In general you should stick to the the very basic Windows controls like Button, TextBox, Label, CheckBox, ComboBox etc. Anything more advanced or fancy, like MaskedTextBox, RichTextBox, CheckedListBox is probably not suported. It is good practice to check the .NET help first before entering any control, and see if it is supported.

No comments: