Transcript Slide 1
Custom Components –andCustom Controls
for the
.NET Compact Framework
Paul Yao
The Paul Yao Company
http://www.paulyao.com
Windows Mobile Development Platform: Today
Visual Studio .NET
Embedded VC++
Managed
Native
MFC
ATL
Server side
ASP .NET
Mobile Controls
.NET Compact
Framework
Win32
Software Platform (APIs)
Device Management
Communication
Presentation
Data Access
Remote API
Configuration
Bluetooth
Security
Connection Manager
TAPI
SMS
MAPI
POOM
ActiveSync
Multimedia
Home Screen
User Interface/Shell
HTML Control
ADO CE
XML
CE DB
OLE DB
GAPI
Windows Mobile
Windows CE
CE DB
ActiveSync
Pocket Outlook
Pocket Internet Explorer
Windows Media Player
Drivers
Device
Hardware
Processor
Memory
Display
USB or Serial
Radio
GSM/GPRS
CDMA/1xRTT
WiFi
Bluetooth
Agenda
Part 1: Control Fundamentals
Part 2: Creating Custom Controls
Part 3: Designer Support
Downloads:
PPT: http://www.paulyao.com/msmdc_cli355.ppt
Sample: http://www.paulyao.com/postalcodecontrol.zip
Part 1: Control Fundamentals
Terminology
Supported Controls
Unsupported Controls
Support for Inherited
Properties
Methods
Events
a.k.a. “PME’s”
Terminology (1/3)
Control vs. Component
A Control
Derives from Control class
Wraps around a Windows API window
Normally is visible
A Component
Derives from Component class
Does not wrap around a Windows API window
Can be visible (Toolbar, MenuItem)
Can be invisible (Timer)
In common parlance, “custom control”
includes both controls and components.
Terminology (2/3)
PMEs = Properties, Methods, & Events
Properties – public data members; can be
read-only, write-only, or read/write (are
really fields hidden in a method)
Methods – class functions. Can be static
(per-class) or instanced (per-object).
Events – Outward-bound method calls that
let the outside world know of a change to
the inner state of an object.
Terminology (3/3)
Inherited and Supported
Inherited
A derived class inherits all PMEs from base
classes
Example: Label inherits all PMEs from
Control
Supported
Property – control uses property
Method – method is implemented
Event – control calls event handler methods
when the event occurs
“Inherited does not
mean supported”
–Seth Demsey,
.NET Compact Framework Team,
Microsoft Corporation
Controls Included
In the .NET Compact Framework
Button
CheckBox
ComboBox
ContextMenu
DataGrid
DomainUpDown
ImageList
Label
ListBox
MainMenu
MenuItem
NumericUpDown
Panel
PictureBox
ProgressBar
RadioButton
HScrollBar
VScrollBar
StatusBar
TabControl
TabPage
TextBox
Timer
ToolBar
ToolBarButton
TrackBar
ListView
TreeView
Controls Excluded
From .NET Framework controls
CheckedListBox
DateTimePicker
ErrorProvider
GroupBox
HelpProvider
LinkLabel
MdiClient
MonthCalendar
NotifyIcon
PrintPreviewControl
RichTextBox
Splitter
Tooltip
Control PMEs
Supported Properties
“Inherited does not mean supported”
Object
MarshallByRefObject
Component
Supports 27 of
76 properties
of desktop
counterpart
Control
Label
Label Supports 20
of 27 properties
Control PMEs
Supported Properties
Other Label Properties
BindingContext
Bottom, Left, Top,
Right
Bounds
ClientRectangle
ClientSize
DataBindings
Height, Width
TopLevelControl
Control PMEs
Supported Methods
“Inherited does not mean supported”
Object
MarshallByRefObject
Component
Supports 30 of
161 methods
of desktop
counterpart
Control
Label
Label supports 15
of 30 methods
Control PMEs
Supported Methods
Docs show
methods
supported
in CF
controls
Control PMEs
Supported Events
“Inherited does not mean supported”
Object
MarshallByRefObject
Component
Supports 17 of
58 events of
desktop
counterpart
Control
Label
Label supports 5
of 17 events
Control PMEs
Supported Events
CF Label Events:
Disposed
ParentChanged
TextChanged
Validated
Validating
Control PMEs
Supported Events
The “Magnificent Seven”
Disposed
ParentChanged
Validated
Validating
EnabledChanged
GotFocus
LostFocus
Three new events…
KeyDown
KeyPress
KeyUp
Supported by all controls
Partially Supported
Click
TextChanged
KeyDown
KeyPress
KeyUp
MouseDown
MouseMove
MouseUp
Part 2: Creating Custom Controls
Design Issues
Demo: PostalCode custom control
Defining & Refining:
Properties
Methods
Events
Building Custom Controls
Design Issues
Rationale for Custom Controls
Refine behavior of existing controls
Ex: Number-only text box
Create new type of control
Ex: Network status display
Wrap library services for third-party
developers
Bar-code scanner control
Printer output
Design Issues
Alternative Types of Custom Controls
1. Derive from an existing control.
Existing control closely matches requirements
Only fine-tuning is needed
Example: TextBox for date-only input
2. Create composite controls
Group existing controls to operate as a unit
Key benefit: reusability
Example: Customer contact fields
3. Create new controls
Existing controls do not match requirements
Want to create unique look and feel
Example: Bar-code reader control
Design Issues
Packaging Alternatives
In your program’s executable (.exe) file
Add to Visual Studio .NET project
Bind to your program
In a separate class library (.dll) file
Can be shipped, shared, installed, updated
Can install to the Global Assembly Cache (GAC)
Key Benefit: save mobile device storage space.
Note: no performance benefits (no pre-JIT)
Design Issues
Three choices for a base class
Existing control class:
TextBox, ListBox, etc.
System.Windows.Forms.Control
Wrapper for an OS window
Can be visible or invisible
In Designer, is placed on Form / Panel
System.ComponentModel.Component
Wrapper for everything else
Can be visible or invible
In Designer, is placed
Design Issues
Support for Paint event
Only In These Control Classes
Form
Control
Panel
PictureBox
DataGrid
Design Issues
Support for input events
Keyboard Events – support in all Controlderived classes (requires CF/ SP2)
Mouse Events – MouseDown/Move/Up
Support in: Control, DataGrid, Form,
Panel, PictureBox, TabPage
No support in other control classes
Click Event
Support in: Button, CheckBox, Control,
DataGrid, Form, MenuItem, Panel,
PictureBox, RadioButton, TabPage
No support in other control classes
Postal Code
Custom Control
1. Modify existing MyAddress program.
2. Create custom control for validating
postal codes from three countries
(Canada, Mexico, Singapore)
Postal Code Custom Control
Elements
Properties
bool AllowSpace – space character
bool AllowLetters – alphabetic letters
Methods
Override base class OnKeyPress
Events
TextChanged – register to receive from base
class
PostalCodeChanged – new event
Building Custom Controls
Defining a property
Private fields for public properties
private bool m_bAllowSpace = false;
Property definition:
public bool AllowSpace
{
get { return m_bAllowSpace; }
set { m_bAllowSpace = value; }
} // property: AllowSpace
Building Custom Controls
Overriding existing methods
Method original definition
protected virtual
void OnKeyPress(
KeyPressEventArgs e);
Declaration for method override:
protected override
void OnKeyPress(
KeyPressEventArgs e)
Building Custom Controls
Creating events (1/3)
1. Field for delegate collection
private EventHandler m_evPostalCodeChanged;
2. Event for adding/removing delegates
public event EventHandler
PostalCodeChanged
{
add { m_evPostalCodeChanged += value; }
remove { m_evPostalCodeChanged -= value;}
}
Building Custom Controls
Creating events (2/3)
3. Method as event broadcaster (‘On’ prefix)
protected virtual void
OnPostalCodeChanged(EventArgs e)
{
if (m_evPostalCodeChanged != null)
{
m_evPostalCodeChanged.Invoke(
this, e);
}
}
Building Custom Controls
Creating events (3/3)
4. Code to call event broadcaster:
private void
PostalCodeTextBox_TextChanged(
object sender, EventArgs e)
{
if (Text.Length == MaxLength)
{
OnPostalCodeChanged(EventArgs.Empty);
}
}
Part 3: Designer Support
The Goal
The Challenges
Setting Up Visual Studio .NET
Designer DLL
Fine-Tuning with Attributes
The Goal
Add control to
Designer Toolbox
Enable
Drag & Drop
Edit properties
Add event handlers
The Challenges
What
Need two DLLs
Might have
redundant
source code
Requires C#
Why
VS.NET uses .NET
Framework; devices use
.NET Compact Framework
VS .NET does not like to
share source files between
projects; need to play tricks
to ‘tame’ VS .NET
VB.NET does not tolerate
duplicate, ambiguous
names
Setting Up Visual Studio .NET
For a custom control library
Goal: Three projects in one directory
App – Smart Device App, for testing
DeviceLib – Smart Device class library
DesignLib – Desktop designer library
For each:
Create project
Rename AssemblyInfo.cs (example:
App_AssemblyInfo.cs)
Close project
Copy files to common directory, then
Project->Add Existing Item…
Setting Device Library Version
Put that version num in design library
Modify DeviceLib_AssemblyInfo.cs:
Change “1.0.*” to 1.0.<num>.<num>
[assembly: AssemblyVersion("1.0.1.0")]
Add to DesignLib_AssemblyInfo.cs:
[assembly: System.CF.Design.RuntimeAssembly(
“DeviceLib,
Version=1.0.1.0,
Culture=neutral,
PublicKeyToken=null")]
Optional – match version
[assembly: AssemblyVersion("1.0.1.0")]
Design Library
Assembly References
.NET Framework Assembly:
System.Windows.Forms.dll
Designer Helpers:
System.CF.Windows.Forms.dll
System.CF.Drawing.dll
Tip:
When building, watch for warning for duplicate
symbols. Must use CF library.
If warning message does not mention CF lib:
1. Delete reference to System.Windows.Forms.dll
2. Then add it back in (force it to end of search list)
Installing Two Control Libraries
Shut down Visual Studio .NET
Copy files to:
\Program Files\Microsoft VS .NET 2003\
CompactFrameworkSDK\
v1.0.5000\
Device DLL goes here
Windows CE\
Designer\
Designer DLL goes here
Start up Visual Studio .NET
Modifying Toolbox
Summon Context
Menu
Select Add/Remote
Items…, then select
designer library
Can also do the
following:
Add a new tab
Organize items
Adding Design-Time
Support for the Postal
Code Custom Control
1. Create .NET Framework DLL
2. Set version number in deviceside control library
3. Set version number in
designer control library
After This Session…
Visit the Windows Mobile booth to:
Get the Windows Mobile Developer Resource Kit
and start supporting landscape and high DPI
Talk to the Technical Support team
Join the Windows Mobile Solutions Partner
Program
Enter the Microsoft Mobile2Market Application
Contest
Fill in your evaluation form
sponsored by AT&T Wireless
1 MPX200 given away per session.
See www.mscorpevents.com/mdc
Your Questions…
Thank You!
© 2004 Microsoft Corporation. All rights reserved.
MICROSOFT CONFIDENTIAL. INTERNAL USE ONLY.