Friday, October 30, 2009

Writing designer friendly controls for Windows Mobile

One thing that you sometimes need when writing Visual Studio designer friendly controls for Windows Mobile, is knowing if your code is running in design time - which is essentially running on the desktop or not. You need to know this because if you are running on the desktop (design time) you don't want to call device specific dlls.

The following code can be used to determine this:
public static class DesignMode
{
private static byte _mode = 255;

public static bool IsTrue
{
get
{
if (_mode == 255)
_mode = AppDomain.CurrentDomain.FriendlyName.Contains("DefaultDomain")
? (byte)1 : (byte) 0;
return _mode == 1;
}
}
}
So essentially you can then code in your application:
if (DesignMode.IsTrue)
{
//don't call coredll.dll
}
else
{
//call coredll.dll
}

4 comments:

Daniel Marbach said...

Hy Simon,
I prefer using Model View Presenter pattern. This allows me to use on the view only controls which are also working in design time on the desktop. When I have special needs I sub class user controls and adapt their behaviors. The actual business logic is then put away in a presenter which is placed in another assembly.

Daniel

Christian Resma Helle said...

You can also check if the Environment.OSVersion.PlatformID is WinCE (run time) or not (design time)

Simon Hart said...

@Daniel: I also prefer to use MVP pattern on devices and I don't see how this relates to this post?

You still need something in the view to determine what to do. I typically have a base view that contains code that pulls types off the container (IoC) but in the case of design time, I need to mock out the container. This bit of code allows you to detemine desktop/design time for that reason. Other reasons are things like custom user controls that on the desktop you need to call gdi32.dll whereas on the device you call coredll.dll.

Simon.

Simon Hart said...

@Christian:

Thanks, that is probably a simpler way.

Simon.