30 Days of Bitching about .NET Compact Framework

If you're an aficionado of .NET Compact Framework development, you've probably stumbled across Chris Craft's blog and read 30 Days of .NET [Windows Mobile Applications].

All in all it was a great series. There were a good number of "fillers" so to speak, but the gems more than made up for it.

The tag line of my blog is "I like Windows Mobile development. Does that make me a masochist?"; and it is quite amazing how often that statement really rings true. Windows Mobile is by necessity a subset of the fully featured desktop version of Windows. And the same goes for .NET and it's little brother .NET Compact Framework. I retract that statement; "little brother" does not do .NET justice when comparing it to CF. I think "adopted bastard red headed stepchild" seems more fitting, don't you? [0][1]

Anyhow, the purpose of this post was to announce that in a sort of satirical homage to Chris' series I will be blogging/bitching the next 30 days about all the pitfalls of Windows Mobile and .NET CF. And if we're lucky, I'll describe any workarounds I may have found. Ok, I'll be up front: don't check this page every day expecting an update; I've been rather busy lately. However, I have more than enough to work with that I can catch up with any amount backlog should I fall behind.

And with that; I'll start with an easy one!

Day 0:

WPF or Silverlight. I want it on Windows Mobile. One of the more annoying aspects of Windows Mobile development is that there are so many screen resolutions to support. Designing an application that looks good on any screen resolution is quite a chore. To implement this properly, one would need a set of classes that allow you to lay out your screen elements in relative measurements rather than absolute dimensions.

The full version of .NET has a TableLayoutPanel class which has the aforementioned behavior. However, that class does not exist on .NET CF. In fact, no intelligent layout code exists from anything released by Microsoft. The Android SDK comes with this sort of UI framework in their V1 product. And I'm not too familiar with the iPhone SDK, but my guess is that when you are supporting a single screen size, it's probably not a huge issue.

Solutions:

Though I can't release the source, I basically wrote a bunch of WPF style classes to do intelligent layout. These classes include StackPanel, DockPanel, WrapPanel, Grid, and ItemsControl. You can probably just grab the WindowlessControls assembly (don't ask me why I called it that) out of any number of my released projects and try to figure out how to use it (hello ILDASM). I should also mention that all the controls support transparencies and "bubbling" up events to its parents; similar to WPF. The ItemsControl is also data driven and rendered using a ContentPresenter similar to WPF.

 

I somewhat lied about Silverlight not being available on Windows Mobile. It is, but it's in beta. The problem is that it requires it to be hosted in a browser. Unfortunately the WebBrowser control in Windows Mobile is not as fully featured, and does not give you access to the DOM. That makes reacting to events on the Silverlight control in non JavaScript code impossible; or at least I thought.

Recently I figured out a way how to invoke C# code through JavaScript. The first step is to get from JavaScript into unmanaged code:

  1. Create an ActiveX control project.
  2. Embed a WebBrowser in your Windows Forms application.
  3. Embed that ActiveX control in your browser HTML. (using <object>)
  4. Funnel all Silverlight events from JavaScript into your ActiveX control.  Now you are in unmanaged code.

Now to get from unmanaged code into managed code:

  1. In your ActiveX control project, create a DLL export that accepts and stores a function pointer or a COM object. This function pointer or COM object will be passed in from managed code.
  2. Create a DLLImport to that function. Pass in a pointer to a method/COM object in managed code.
  3. When JavaScript is invoked, your ActiveX is being called into. Now from that point, you can then call into the managed function pointer you provided earlier. I'll leave the exercise of passing and parsing arbitrary arguments up to the reader. You can basically invoke any method name and any method signatures of any C# function from JavaScript by using a crafty combination of string parsing and reflection. The solution I had was quite elegant for being a giant hack in the grand scheme of things.

And there you go! You are now creating Silverlight thick client applications just like on the desktop!

 

[0] I speak in jest. I do not mean any offense to anyone who is adopted, a bastard, or a step child. I did mean the red head remark though.

[1] Real developers zero index their blog footnotes.

5 comments:

Spiceworm said...

One of the more annoying aspects of Windows Mobile development is that there are so many screen resolutions to support. Designing an application that looks good on any screen resolution is quite a chore. To implement this properly, one would need a set of classes that allow you to lay out your screen elements in relative measurements rather than absolute dimensions.

No, that is not needed. From CF 2.0 and VS2005 there is a form property "AutoScaleMode". Set this to Dpi and your program will scale (like a vector drawing) nicely, without pixelating on higher res. devices.

Koush said...

Autoscale mode only works somewhat if the aspect ratio of both devices are the same. For example, scaling from 240x320 to 480x640 works by pixel doubling.
However, you can't scale an app from 240x320 to 320x240: those are different aspect ratios. The entire application needs to be properly sized; there is more relative horizontal width, so maybe it would be appropriate to show more items in a grid, for example. There is less vertical width, so maybe the UI needs a scroll bar now.

Quarrelsome said...

Nice post! I'm personally a little sick of lack of features in CF. XmlDocument heavy stuff doesn't port, Serialization stuff doesn't port, REFLECTION.EMIT doesn't port. There's plenty of other stuff too.

Are they planning on filling in the gaps so we can port more exciting things to the mobile world? I often wonder....

Seeing 3.5 is a little disappointing IMO. Sure LINQ means that at least new LINQ code will port (ish) but they haven't really given us that much that is new. LINQ is just sugar after all....

I am brutally disappointed that Silverlight is not scheduled for Windows CE. Is it the case that industry should live in the past? I want some new UI as the things I have to fulfill client's expectations of modern UIs in WinCE is utterly obscene (such as creating circular transparent regions in rectangles and merging them).

Apologies for the rant but this looked like a good point at which to let off this kind of steam. :D

abhinaba said...

Silverlight on mobile devices
-----------------------------
http://silverlight.net/learn/mobile.aspx

http://blogs.msdn.com/abhinaba/archive/2008/10/22/silverlight-on-nokia-s60-devices.aspx

Hasani Blackwell said...

Great series so far. The .NET Compact Framework is indeed a shit framework. I created several feature requests on connect.microsoft.com (see http://social.msdn.microsoft.com/Forums/en-US/netfxcompact/thread/91a21c03-d9fc-4d23-b399-5d80c8bfd850) that have fallen on deaf ears.

I also think Microsoft has a horrible track record of implementing bug fixes for both implementations of the .NET Framework.
E.x.: http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=318918 or http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=367247