Saturday 28 June 2008

.NET Compact Framework How-to Topics

Microsoft have provided links to How-to topics found in the conceptual documentation for the .NET Compact Framework. These topics contain procedures and sample code for developing and using .NET Compact Framework applications. They demonstrate how to use controls in the Microsoft.WindowsCE.Forms namespace, how to create custom controls, how to use interoperability, and workarounds for unsupported class members or other features.
Note that some of these are easier to achieve using new methods in .NET CF 3.5, so read the How-to topic carefully before copying code samples because it mentions that for some of them.

Some interesting ones are:

How to: Get the Device Platform

How to: Get the Application Directory

How to: Get the Device ID and Name

How to: Reset the Device

How to: Play Sounds

See all

Power Toys for .NET Compact Framework 3.5

The Power Toys for .NET Compact Framework 3.5 CTP provides tools for evaluating performance, obtaining diagnostic information, working with WCF and configuring the .NET Compact Framework.

Tools included in this package:

Remote Performance Monitor and GC Heap Viewer – Provides real time counter data (ranging from Garbage Collector activity to type loading info) on a running NETCF application. The GC Heap Viewer feature allows you to capture the managed heap at any moment your app is running to view live references, and allows you to compare multiple snapshots to find memory leak issues.

NETCF CLR Profiler – CLR Profiler is an instrumenting allocation profiler for NETCF applications. It provides detailed allocation visualizations, allocation callstacks visualizations and useful for diagnosing memory management issues.

App Configuration Tool (NetCFcfg.exe) - On-device tool for specifying what version of the NETCF runtime an application will run against, displaying installed versions of NETCF and displaying info about DLLs in the GAC. More info from Modaco

NETCF ServiceModel Metadata Tool – The .NET Compact Framework ServiceModel Metadata Tool (netcfsvcutil.exe) allows you to generate a Windows Communication Foundation (WCF) client proxy to help developers consume WCF services on device. Like svcutil.exe, which is the desktop version of the utility, netcfsvcutil.exe is a command-line tool that generates service model code from metadata documents and generates metadata documents from service model code.

Remote Logging Configuration Tool – The Logging Configuration Tool enables users to easily configure logging options on a NETCF device including: loader, interop, network, error and finalizer logs.

NETCF Network Log Viewer
– A utility for viewing NETCF network log data.

Read

How to build a custom control that responds to focus events

This snippet should let you build a panel that can receive and pass focus on when either the up or down key is pressed. This is ideal for use on a Smartphone for when you need a list of custom controls. The trick is to use the CF2 function SelectNextControl. Another trick I added is to search up the parent tree so that focus can still be passed from a control which is inside another panel to one which is not, for example one which is just on the form. Remember if you are going to paint a focus rect you need to add Invalidate into the OnGotFocus and OnLostFocus overridden events. Then in your OnPaint you can check if Focused and paint accordingly.
protected override void OnKeyDown(KeyEventArgs e)
{
Control c = this;
switch (e.KeyCode)
{
case Keys.Up:
while ((c = c.Parent) != null && !c.SelectNextControl(this, false, true, true, true));
break;
case Keys.Down:
while ((c = c.Parent) != null && !c.SelectNextControl(this, true, true, true, true)) ;
break;
}
base.OnKeyDown(e);
}

Wednesday 25 June 2008

Viewing the source code of .NET CF applications with Reflector

The best way to learn is by example. You might have found a nice looking application that has a funky looking list box with icons and are interested in achieving the same look e.g. Live Search. Unfortunately not all CF projects are open source, but fortunately there is a way to view the source code. First thing you need to check is if the application you are interested in is in fact a .NET CF application. The first indicator is if takes a long time to launch, because .NET applications are much slower to load than native because the CLR needs to start first. If it is instant to start, like Google Maps then don't bother because it is native.

First you should check if the application's source code isn't published somewhere. If you found it on Codeplex, Google Code, or Source Forge then the source code is most likely available already, still if it isn't released for the version of the executable you have you can proceed with the steps below.

The next thing you need to consider is access to the EXE or DLL. If you have already installed the application on to your smart phone then you can just active sync, browse to \Program Files\TheApplication and copy the binaries back to your PC. If you don't have a phone handy chances are the application has been distributed as a CAB file and you need to extract the files. The fastest way to do this is to use WinCE CAB Manager, just open the CAB and drag out the EXE and DLLs. They have a trial you can use.

Next you need to install Reflector for .NET. It comes as a zip file so what I usually do is create the folder "C:\Program Files\Reflector" and drag the files into there. Then I right click the Reflector exe and do create shortcut, rename it to just "Reflector" and drag it into my start menu and quick launch if you use it a lot.

Now run it and drag your .NET CF exe into the window. Expand its tree icon a few times and hopefully you will see some class names. Right click on the class name and choose "Disassemble", then click the expand methods at the bottom of the right pane. Hopefully now you will see all of the code. If you just see gobbledygook for the class and method names unfortunately the developer has protected the code from our eyes (Obfuscated code). You can still proceed it just is going to be really hard to read, luckily you can click method and class names to navigate around the code. Once you find something interesting you can copy it from the code panel.

The final step is if you want to extract all of the files of the executable or class library. Reflector can actually generate a project file you can open in Visual Studio, make changes to and run and debug. First you need to install the File Disassembler Addin. Extract the DLL and copy it to the "C:\Program Files\Reflector". Then in reflector go to the Addin menu item and install Disassembler. Now activate it under the Tools menu. You will notice everything stays greyed out until you choose an assembly. This means you need to select a top level tree item. Next choose the project type and then Generate. If it moans about missing references make sure you browse to find the correct file. This is essential to give you a build-able project file. Once finished, you should have a folder of all of the source code. Unfortunately the project file generated is for full .NET and not .NET CF so you can either modify it to look like a CF project file, or much easier just make a new one and drag in all the code and set up the references. Of course the code isn't commented because comments are removed when the application is compiled, but C# is very readable so this isn't a huge problem. Sometimes it generates the loops and case statements in an un-friendly format, and you might have to tidy them up a bit.

So there you go, a fantastic way to examine the source code of non-open source CF applications and see just how other developers right their code, a great learning technique. For example Live Search, uses an MVC design pattern which is excellent to learn, but quite difficult to implement in Visual Studio because of the way the form designer works. I'll post about any other interesting .NET CF applications I find and you can try using Reflector on them if you like.

This guide is for educational use and if you work for a company you definitely shouldn't be stealing code like this, contact the author first and ask if they will let you in on their technique. Most .NET CF developers are a friendly bunch and willing to share. This guide is particularly useful to the moderate level programmer who has learned all the basics but seeking to learn how some professionally developed software is created, with the CF being so limited chances are you will come across something useful.

Setting up the latest development tools

First install Visual Studio 2008, I usually un-select Visual Basic but it can sometimes be handy if you come across any open-source VB software you need to port. Run it once to check it is OK then quit.

Install the Windows Mobile 6 SDK Refresh. Both Standard and Professional.

Install the Windows Mobile 6.1 Emulator Images. Again, install both Standard and Professional.

Now run Visual Studio 2008 and create a C# smart device project, choosing an application and .NET 3.5 and the WM6 SDK. Now check in the deploy drop down that you have the 6.1 emulators listed.

That's it! You are now set up with the latest Compact Framework development tools.