Writing code for both the full .NET Framework and the .NET Compact Framework

Porting my Offline Maps application to the .NET Compact Framework, I was in for a big supprise:

I thought it would be simple: I intentionally seperated most of the GUI code from the Business code thinking that I would simply need to port the GUI and everything would be OK.

No sir, that didn’t work…

Problems encountered

The first supprise was that in Visual Studio 2005 you can not set a reference in a Device Application project to a normal .NET library. You must specify a special Class Library project specific to Windows Mobile (I’ ve been using the Windows Mobile 6 Professional SDK).

Secondly, not being used to programming for the .NET Compact Framework, I thought that having explicitly used mainstream classes, a simple recompile for the Compact Framework would suffise. However that didn’t work: apparently you can have equally named classes, but they don’t necessarily have the same methods. The Compact Framework mostly just supports a subset of the full Framework class methods.

Solutions

Looking for solutions I came across this article.
It offers a multitude of solutions from which I mostly used the “one folder, two projects” and “use partial classes”.

Another point to keep in mind is that if you have overloaded methods in a class with versions taking objects and versions taking the bare properties of those objects, sometimes the Compact Framework only supports those taking bare properties.

An example of this last case is a method which takes a rectangle as an argument opposed to a method taking an X and Y coordinate for the position and a Width and Height for the size of the rectangle.

Also, if some classes or methods aren’t supported that can lead to a whole area of functionality not being supported. An example of this are the StartCap, EndCap and LineJoin properties of a pen which are not supported in the Compact Framework. They can require you to implement a complete abstraction layer for your functionality. Fortunately I got a way with a partial class with two different implementations of a “CreateRoundedPen” method.

Something which surprised me the most is that some Environment static properties which after all are meant to enable cross platform development aren’t supported neither.

As an example the Compact Framework does not support the Environment.NewLine property. Weel, at least not in the version I used. I noticed in the Compact Framework 3.5 documentation that it is now supported.

Conclusion

So, cross device development from a single code base does take some preparation. It requires you to think about how to setup your projects folder structure and to read the documentation on classes and methods so to choose the method supported in both platforms.

But what experience, and derived from it tips, do you have?

Links

[1] Share Code: Write Code Once For Both Mobile And Desktop Apps
[2] MSDN documentation on the Environment.NewLine property

Advertisements

One thought on “Writing code for both the full .NET Framework and the .NET Compact Framework

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s