What are the most valuable .Net Compact Framework Tips, Tricks, and Gotcha-Avoiders?

Mat Nadrofsky picture Mat Nadrofsky · Dec 12, 2008 · Viewed 11.3k times · Source

We work extensively in the .Net Compact Framework and Windows Mobile. I've seen plenty of questions come up regarding specifics to development of ASP.Net apps or other .Net based desktop apps but nothing CF specific.

Anyone else a mobile developer out there that can share some things to start doing, stop doing, and avoid doing when developing in the Compact Framework?

Answer

ctacke picture ctacke · Dec 12, 2008

Sure:

  • Use a physical device whenever possible (not the emulator)
  • Test with multiple devices (different vendors, different models)
  • Concentrate testing around sleep/wakeup behaviors
  • When using MSTEST unit tests, never use private accessors
  • Avoid ActiveSync like the plague - debug using CoreCon direct
  • Get familiar with RPM and start using it early
  • Reuse objects when possible
  • Avoid doing a lot of work in a Form's ctor - off load it for lazy load or in a background thread
  • Load Forms on demand when possible (not all of them at once)
  • Cache the frequently used Forms, create infrequent ones on demand
  • Keep image resolutions low
  • If a class exposes Dispose use it. Always.
  • No app is too small to benefit from MVC/MVP patterns
  • Don't use the Microsoft CAB/SCSF port for the CF (the people who ported it obviously never actually used a resource-limited device)
  • Get familiar with the concept of "occasionally connected" if you will be doing any remote data/service activity
  • Docking and Anchoring are your friend and your enemy - test run-time screen rotations and multiple resolutions (even if you think you won't target them, because you're probably wrong in that thinking)
  • Look at, but don't heavily invest in the device deployment package project type. It has major limitations that will likely bite you. A batch file works surprisingly well or a custom MSBUILD task to call CabWiz
  • Brush up on your C++ and P/Invoke skills. You will need them. It's almost impossible to write a useful CF app without P/Invoking something.
  • Code to the lowest common denominator for targets.
  • Partial classes are your friend, especially for dividing logic between target types (PPC, Phone, non-mobile CE).
  • Avoid running an app from persistent storage, especially for CE and pre-WInMo 5. Copy to RAM and run from there to prevent demand-paging from killing you, especially after a sleep/wake cycle.
  • Apps should not care about sleep/wake transitions, but that's pure theory. Sleep wake **will* change your app behavior, so again test, test, test.
  • Did I mention test? Especially on every device you can get your hands on? Buy cheap hardware off of eBay for your test lab. Having more devices is more important than having the latest unless you intend to use a specific feature of a newer device.
  • Ask for divine intervention if you plan to use bluetooth programmatically. Get familiar with the Widcomm and Microsoft stacks and understand that they aren't the same.
  • Watch the MSDN webcast on memory management in the Compact Framework. Watch it again for the stuff you missed the first time.
  • Watch out for sleep/wake invalidating internal handles and causing access violations. This is more esoteric but certainly happens. For example, if you're running an application off of a storage card, the entire app isn't loaded into RAM. Pieces in use are demand-paged in for execution. This is all well and good. Now if you power the device off, the drivers all shut down. When you power back up, many devices simply re-mount the storage devices. When your app needs to demand-page in more program, it's no longer where it was and it dies. Similar behavior can happen with databases on mounted stores. If you have an open handle to the database, after a sleep/wake cycle the connection handle may no longer be valid.
  • Install the evaluation version of Platform Builder. The source code for a whole lot of things is in there (like the network UI, many drivers, etc) and when your P/Invoke code isn't doing what you expect you'll at least have a place to go look for the "why".

added 5/25/10

added 7/27/10

  • If you're after a aesthetic UI, be prepared to do a lot of custom or manual drawing.
  • If you're doing custom or manual drawing and you need to use transparency, get ready for a load of frustrations and having to write wacky code or call native code directly to work around shortcomings in the CF.

added 11/22/11

  • Don't assume that just because a namespace or class exists in the BCL that it is actually implemented in any useful manner. Certificates certainly aren't.

I'm simply adding to the list as they occur to me...