Sunday, December 19, 2004

Pocket PC project and developer setup

For CF control we have to do following,

  • Create CF Control solution and source code (actual control)
  • Create Windows Control solution and share the source (designer)
  • Copy them at right location and use them


Wipe you slate clean. Forget everything you know about windows/web/mobile control. Listen carefully what I say.


For any control we have following ways to look at a control

  • Control when running.
  • Control when being designed.

Reasons to highlight the abovementioned obvious perspective is due to differences in the compact framework run time and design time. This understanding of this perspective
difference will make us understand things more clearly later.

  • Control when running, is running in limited resources compact framework. Nothing but compact framework is available

  • Control when designed. It is designed in Visual studio. This requires control to interact with visual-studio and other heavy weight dlls which are not available in compact-framework.

Now the dilemma is, if we just use compact framework to generate the dll to cater design-time and run-time, we can only do so much, and if we use the windows forms regular framework to create dlls we can't run it on the CF DEVICE. In order to deal with this scenario we create TWO Dlls,

  • Control Dll (run-time dll, linked with pocket pc framework dlls) and
  • Control Designer dll (design time dll, linked with .Net PC framework dlls)

Now lets see how one/VS will use them.

  • you use Designer Dll to design the application
  • you run (F5), and the other dll (the actual CF one) gets deployed and used in run time.

Now in order to establish this connection and automatic switching of design to run-time dlls following is done. In "AssemblyInfo.cs" of Designer Dll we add following attribute.


#if DESIGN
[assembly:
System.CF.Design.RuntimeAssemblyAttribute("MySmartDeviceControl, Version=5.1.20051.3, Culture=neutral, PublicKeyToken=null")]
#endif


In summary,

  • Designer Dll designs the application,
  • Control Dll is used for running the application.
  • and above attribute does that.

Now the question is? in order to create two dlls do we write two source codes ? The answer is 'no'. Let me explain you in easy steps, Although the steps might seem to be long, but they are
described in detail for easy understanding.

1 Create a directory say at some location like following,

c:\work\PocketPC

2 Open Visual studio and Create a Windows Smart Device Application




3 Delete Form1.cs


to get


4 Open AssemblyInfo.cs and add the abovementioned attribute.




5 Add new Class "MyControl.cs" and add following code, the simplest control,



Add following code.

using System;

using System.Drawing;
namespace MySmartDeviceControl
{
public class MyControl : System.Windows.Forms.Control
{
public MyControl()
{
}
protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
base.OnPaint (e);
e.Graphics.DrawRectangle(Pens.Red, 0,0,this.Width-1, this.Height-1);
e.Graphics.DrawString("MySmartDeviceControl", this.Font, Brushes.Blue,10,10);
}
protected override void OnResize(EventArgs e)
{
base.OnResize (e);
this.Invalidate();
}
}
}
6 Save everything and close the visual studio.

7 Open Visual Studio and create a new Project of WINDOWS CONTROL LIBRARY, Yes, NO Smart Device, the plain windows Control Library.



9 Delete the "UserControl1.cs"


to get,



10 Now, pay attention as this is tricky part, from references, delete

"System.Data"
"System.Drawing"
"System.Windows.Forms"
"System.XML"


11Add the reference, click "Browse"





use path "C:\Program Files\Microsoft Visual Studio .NET 2003\CompactFrameworkSDK\v1.0.5000\Windows CE\Designer"


"System.CF.Design.dll"
"System.CF.Drawing.dll"
"System.CF.Windows.Forms.dll"

Now Add "Windows Forms" references AGAIN, (these are the ones you deleted in step 10). This sequence of removing and adding the references does matter. You will learn that in a moment. In short, when looking for a type that exist in more than one place, first one is used for binding. Add,

"System.Data"
"System.Drawing"
"System.Windows.Forms"
"System.XML"


to notice following,

12 Right click the project and open properties,






Add "Conditional Compilation Constant" "DESIGN", please notice that was used in AssemblyInfo.cs.

13 Save everything and close the visual studio.

14 Open "C:\PocketPC" directory and you will notice,

MySmartDeviceControl
MySmartDeviceControlDesigner





15 From "MySmartDeviceControlDesigner" and CUT (^X),



MySmartDeviceControlDesigner.csproj
MySmartDeviceControlDesigner.csproj.user
MySmartDeviceControlDesigner.sln
MySmartDeviceControlDesigner.suo

PASTE (^V) to MySmartDeviceControl



and delete MySmartDeviceControlDesigner directory.

16 Now open "MySmartDeviceControl\MySmartDeviceControlDesigner.sln". Right click and add existing item, select "MyControl.cs" and you.






17 Build the control [Ctrl+Shift+B"] When you compile you will get following warning,


'XXX' is defined in multiple places; using definition from 'YYY'


Since 'XXX' is defined in more than one place it uses from the location found first. In this case it happens to be "System.CF.*.dll". This is intented, and thus was reason for to add them in sequence.

'System.Windows.Forms.Control' is defined in multiple places; using definition from 'System.CF.Windows.Forms.dll'

'System.Windows.Forms.PaintEventArgs' is defined in multiple places; using definition from 'System.CF.Windows.Forms.dll'

'System.Drawing.Pens' is defined in multiple places; using definition from 'System.CF.Drawing.DLL'

'System.Drawing.Brushes' is defined in multiple places; using definition from 'System.CF.Drawing.DLL'

Thus following references,

"System.CF.Design.dll"
"System.CF.Drawing.dll"
"System.CF.Windows.Forms.dll"

must come before

"System.Data"
"System.Drawing"
"System.Windows.Forms"
"System.XML"

as you will notice in following image.





17 Summarizing , we have following

  • One directory "C:\WORK\PocketPC\MySmartDeviceControl"
  • Two Solutions "MySmartDeviceControl.sln" and "MySmartDeviceControlDesigner.sln"
  • Two DLLs that are generated after compiling above solutions.


18 Now these DLLS should be copied at following locations (this is very important),

"MySmartDeviceControl.dll"

C:\Program Files\Microsoft Visual Studio .NET 2003\CompactFrameworkSDK\v1.0.5000\Windows CE

and

"MySmartDeviceControlDesigner.dll"

C:\Program Files\Microsoft Visual Studio .NET 2003\CompactFrameworkSDK\v1.0.5000\Windows CE\Designer





19 Adding the control to the toolbox

Open Visual Studio and Create new Smart Device Application,





20Using the control and running the test Smart Device Test Application



The Application is running.