Windows Phone Guide for Android Application Developers

Arya MirMobile - sans fil

12 oct. 2011 (il y a 3 années et 26 jours)

1 341 vue(s)

Microsoft 10/07/2011 Rev 4.0





Microsoft

10
/
07
/2011

Rev
4.0

Windows Phone Guide for
Android

Application
Developers

Windows Phone Guide for
iPhone

Application Developers




Table of Content

Microsoft
................................
................................
................................
................................
....................

1

Table of Content

................................
................................
................................
................................
.....

2

About this Development Guide

................................
................................
................................
.........

5

Target Audience

................................
................................
................................
................................
......

5

Conventions Used in this Document

................................
................................
................................

5

Chapter 1: Introducing Windows

Phone Platform to Android Application Developers

.

6

The Developer Tools
................................
................................
................................
................................
.

6

Windows Phone Architecture

................................
................................
................................
...............

6

Co
mparing the Programming Stack of Windows Phone with Android

................................
.

8

Summary

................................
................................
................................
................................
....................

13

Related Resources

................................
................................
................................
................................
...

13

Chapter 2: User Interface Guidelines

................................
................................
.............................

14

Designing the Application Interface

................................
................................
................................
.

15

Application User Interface Design

................................
................................
................................
.....

16

Comparing Windows Phone and Android Navigation

................................
..............................

20

Windows Phone Frame and Page Structure

................................
................................
..................

21

Application Templates

................................
................................
................................
...........................

23

Summar
y

................................
................................
................................
................................
....................

23

Related Resources

................................
................................
................................
................................
...

23

Chapter 3: The Developer and Designer Tools

................................
................................
...........

25

A Comparison of Android and Windows Phone Tools

................................
..............................

25

Development Life Cycle and Windows Phone Developer Tools

................................
.............

26

The UI Design Tools

................................
................................
................................
................................

29

Building Applications

................................
................................
................................
.............................

36

Debugging

................................
................................
................................
................................
.................

37

Summary

................................
................................
................................
................................
....................

41

Chapter 4: C# programming

................................
................................
................................
............

42

Managed Programming
................................
................................
................................
........................

43

A Comparison between C# Features and Java Classes
................................
...............................

44

A Comparison of Important Class Libraries

................................
................................
....................

54

The New features of C#

................................
................................
................................
........................

57

Comparing API Documentation Tools

................................
................................
.............................

61

NDoc

................................
................................
................................
................................
............................

61

NDocs vs. Javadoc

................................
................................
................................
................................
...

64

Windows Phone Guide for
iPhone

Application Developers




Summary

................................
................................
................................
................................
....................

65

Related Resources

................................
................................
................................
................................
...

65

Chapter 5: A Comparison of Application Life Cycles in Windows Phone and Android

66

Multitasking in Android and Windows Phone

................................
................................
..............

66

Tombstoning of Applications in Windows Phone

................................
................................
.......

67

Life Cycle of a Windows Phone Application

................................
................................
..................

67

Role of Handlers in an Application’s Life Cycle

................................
................................
.............

69

Comparing Life
-
cycle Methods

................................
................................
................................
..........

71

Tombstoning and Windows Phone Life Cycle
................................
................................
...............

72

Summary

................................
................................
................................
................................
....................

77

Related Resources

................................
................................
................................
................................
...

77

Chapter 6: Storing Data
and Preferences

................................
................................
.....................

78

Isolated Storage in Windows Phone

................................
................................
................................
.

78

Use Cases

................................
................................
................................
................................
....................

81

Managing the IsolatedStorage Space

................................
................................
..............................

82

Why Windows Phone Does Not Use External Storage

................................
...............................

83

Managing Content across Applications

................................
................................
...........................

84

Comparing Intent API with Launchers and Choosers API

................................
.........................

85

Summary

................................
................................
................................
................................
....................

89

Related Resources

................................
................................
................................
................................
...

89

Chapter 7: XML Parsing in Windows Phone and Android

................................
......................

90

XML Parsing in Windows Phone

................................
................................
................................
........

90

XML Namespaces for Windows Phone

................................
................................
............................

90

XML Parsing Using X
ML
Reader

................................
................................
................................
..........

90

TRAVERSING XML

................................
................................
................................
................................
...

99

Summary

................................
................................
................................
................................
..................
104

Related Resources

................................
................................
................................
................................
.
104

Appendix A: Migration Samples

................................
................................
................................
...

105

In
-
App Advertisements

................................
................................
................................
.......................
106

Geo
-
Location

................................
................................
................................
................................
..........
111

Group Messaging

................................
................................
................................
................................
..
117

Appendix B: Migration Tips

................................
................................
................................
............

123

Starting a New Project

................................
................................
................................
............................
123

Managing Project Properties

................................
................................
................................
................
125

Adding Controls & Managing Layouts

................................
................................
................................
.
128

Configuring Control Properties

................................
................................
................................
.............
131

Adding a New Page

................................
................................
................................
................................
..
133

Listening for Button Clicks

................................
................................
................................
.....................
135

Adding External Libraries

................................
................................
................................
.......................
137

Windows Phone Guide for
iPhone

Application Developers




Displaying Alerts within an Application

................................
................................
..............................
139

Hiding the System Tray

................................
................................
................................
...........................
140

Interacting with the Windows Phone Emulator

................................
................................
...............
141

Appendix C: Using the API Mapping Tool

................................
................................
.................

144

What’s the API Mapping tool

................................
................................
................................
............
144

How to use the tool

................................
................................
................................
..............................
144

What's next?

................................
................................
................................
................................
............
144



Windows Phone Guide for
iPhone

Application Developers




About this Development Guide


If you have been developing
Android

applications and are interested in
building your applications for
Windows Phone
, this guide is for you.

The guide cover
s

what you need to know to add
Windows Phone

development to your skill set, while leveraging what you have already
learned building
Android applications
.




Target Audience

This document is for Android application developers willing to develop applications for
Windows Phone.

Conventions
U
sed in th
is
D
ocument

Term

Abbreviation

Windows Phone

WP

Application Programming
Interface

API

Integrated Development
Environment

IDE


Windows Phone Guide for
iPhone

Application Developers

Chapter 1: Introducing Windows Phone Platform to Android Application Developers

6


Chapter 1: Introducing
Windows Phone

Platform to Android Application
Developers

Ever since the release of
Windows Phone

(
WP
) platform, developers from around the
globe rushed to develop applications for this platform. Now,
WP

devices are available
with manufacturers all over the world. A lar
ge number of applications are already
available in the
WP

marketplace.

For
WP
, Microsoft went back to the drawing board to figure out what phone users really
want, and built a phone from the ground up. The operating system, the user experience,
and the ap
plication development platform have all been engineered with users in mind.
The revenue opportunities in the Windows Phone marketplace, accompanied by a great
set of development tools, make
WP

a very attractive destination for developers to build
applicati
ons and games.

The Developer Tools

Microsoft recommends using these tools for
WP

application development. This toolset is
free and can be downloaded from
here
. The toolset includes:



An IDE
(for developers): Visual Studio Express for Windows Phone



A user interface design tool (for designers): Express Blend for Windows Phone



Silverlight for Windows Phone and XNA Game Studio for Windows Phone



Windows Phone emulator to test and debug applicatio
ns

The tools are designed to let you develop consumer applications, business applications,
and games.

Windows Phone

Architecture

WP

utilizes a layered architecture. Like Android OS,
WP

runs on multiple phones. To
provide a consistent user experience and f
eatures that
you

can rely on,
WP

defines a
minimum set of hardware specifications that all phones must meet. They include:



ARM7 CPU



A DirectX capable GPU



Camera



Multi
-
touch capacitive display



Standard sensors include:

Windows Phone Guide for
iPhone

Application Developers

Chapter 1: Introducing Windows Phone Platform to Android Application Devel
opers

7


o

A
-
GPS

o

Accelerometer

o

Compass

o

Proximity

and light sensors

There are three standard buttons on the phone: Back, Start, and Search. As we will see in
a subsequent section, these buttons provide an easy and natural navigation model for
the user.

In
WP
, Microsoft provides most of the device dri
ver code. A device manufacturer has to
write very little code

specific to their device. This
improves the consistency and quality
across various devices.
WP

takes advantage of hardware acceleration through
encapsulation layers, such as DirectX or XNA.


WP

applications use managed programming and run within sandboxed environments.
For more details about
WP

architecture, watch the
MIX’10 presentation

by Istvan Cseri, a
WP

architect.



Windows Phone Guide for
iPhone

Application Developers

Chapter 1: Introducing Windows Phone Platform to Android Application Developers

8


Comparing the Programming Stack
of Windows
Phone

with Android

The Application Model shown above provides services, such as installation and update,
for managing the application lifecycle. The UI model helps to manage application user
interface. Applicatio
ns are built using various
WP

frameworks.

This table gives an overview of the
WP

frameworks that provide features comparable to
the Android programming layers.


Android Frameworks

Functionality

Windows Phone

Frameworks

Android
A
pplication
F
ramework

Application UI, Device
integration (sensors, camera)

Windows Phone

Phone
Framework, Silverlight controls

Android
A
pplication
F
ramework

Graphics, Animation, Media

XNA for games or Silverlight
media and graphics for others

Android
L
ibraries

Base services,
Networking,
Text, XML, storage

Common Base Library

Android runtime
(Dalvik)+ Linux kernel


Windows Phone




Windows Phone Guide for
iPhone

Application Developers

Chapter 1: Introducing Windows Phone Platform to Android Application Developers

9


Android and
Windows Phone

Stacks
S
ide

by
S
ide

This table compares the Android and
WP

Frameworks side by side.





Android Frameworks



Windows
Phone

Frameworks


Application UI and Phone

Multi
-
tasking




Java




C# or VB.NET



Application UI
and Phone
integration

Application UI


Application UI

Device integration


Device integration

Browser Control


Browser Control

Notifications


Notifications

Peer to Peer
Gaming




Silverlight

Gamer
Services



XNA


Two Application Types

Controls & Gestures


Controls &
Gestures


Media

Telephony


Media


Media

Media Framework


Animations


Animations

Animations

Graphics


Graphics

Graphics

O
pen
GL
-
ES


Isolated Storage

Content


Core Libraries

File System


SQLLite




Base Class Library

Location


Location

XML


XML, LINQ

Networking


Networking,
Windows Communication
Foundation

Libc
, Dalvik VM


CLR Base Classes

Android runtime + Linux
kernel


Windows Phone

OS

Windows Phone Guide for
iPhone

Application Developers

Chapter 1: Introducing Windows Phone Platform to Android Application Developers

10


Managed Code

I
n Android, every application runs in its own process, with its own instance of the Dalvik
virtual machine.
WP

only supports “
managed code
” applications using C# or VB.net;
there is no native access available to the system or the phone hardware.

.NET Common Language Runtime (CLR) manages
the execution of such code. CLR
provides garbage collection and there is no memory management to worry about or
pointers to take care of. The
WP

application stack is built on the .NET compact
framework 3.7. As Dalvik virtual machine is optimized for mobile

devices, the .NET
compact framework is optimized for resource constrained devices and is designed to be
portable across various hardware platforms.


Windows Phone Guide for
iPhone

Application Developers

Chapter 1
: Introducing Windows Phone Platform to Android Application Developers

11


Base Services

WP

Base Class Library classes roughly correspond to those provided in the Android Core
Libr
aries.

Android core libraries include:



Media framework for audio and video codec support



SQLite
for database



O
pengl
-
ES



SGL

for graphics and animation



Networking stack



L
ibc for embedding with Linux kernel



Dalvik VM

The
WP

Base Class Library layer also inc
ludes:



Networking stacks: such as HTTP and the Windows Communication Foundation
(WCF). WCF provides an easy interface with XML and SOAP services across the
Web
, with features supporting the XML data transfer, serialization, deserialization
and XML parsing
.



Database support:
WP

does not have a local database, such as SQLLite. You can
write SQL
-
like queries in C# using Language Integrated Query (LINQ) to query
XML data, stored in isolated storage (see below), or in remote databases such as
SQL Azure.

Appl
ication UI and Device Integration

You can develop
WP

applications using two libraries: Silverlight and XNA. Silverlight
contains the visual components same as Android standard widgets.

Generally, it is recommended that you use Silverlight for consumer or
business
applications and XNA for games. However, you can certainly develop great games using
Silverlight animation.



Windows Phone Guide for
iPhone

Application Developers

Chapter 1: Introducing Windows Phone Platform to Android Application Developers

12



Android applications




Windows Phone

counterparts


UI using activity(contains
widgets) with navigation
between them



Silverlight applications with pages
connected by flows


2D or 3D games built with
O
pengl
-
ES





XNA games with 2D / 3D graphics
and Xbox connectivity

XNA for Games

For a high performance game, XNA is the right option. XNA framework, originally
developed for XBOX, provides hardware accelerated 2D and 3D rendering and bitmap
graphics. XNA also provides gamer services such as authentication and connectivity with
XBOX Li
ve, as well as
P
rofiles and
L
eaderboards.

Silverlight Controls and Media

If you use Android activities and widgets, you will find a large set of Silverlight UI controls
specifically designed for the OS.
WP

UI components are designed for multi
-
touch.
Silve
rlight uses a declarative language called Extensible Application Markup Language
(XAML) to specify user interfaces. You can use separate code
-
behind files, written in C#
or VB.NET, to respond to events or manipulate the controls.

Silverlight provides high

performance audio and video with variety of CODECs. It
supports both vector and bitmap graphics with hardware acceleration. As opposed to a
file system, Silverlight provides sandboxed storage, called isolated Storage, to store the
application
-
specific dat
a. With the isolation of storage, one application cannot affect
other applications that are running on the phone.

Windows Phone Frameworks

Windows Phone Guide for
iPhone

Application Developers

Chapter 1: Introducing Windows Phone Platform to Android Application Developers

13


If you need to use HTML in your application, you can use the Internet Explorer based
browser control in your application for HTML UI. Windows Phone Framework layer also
provides interfaces to various sensors, such as the accelerometer or the camera.
Micros
oft provides a push notification service, called Microsoft Push Notification Service.

In Android, multitasking is supported. In
WP
, support for multitasking will be provided in
the next release. In

Android, you need to use third
-
party platforms like ADMo
b to
publish advertisements in applications. Microsoft has made this task easier in
WP

by
introducing Microsoft Advertising SDK for
WP
.

For more information, visit:

Microsoft Advertising SDK for
Windows Phone

Summary

In this chapter we looked at the
WP

architecture and the Android and
WP

programming
stacks. Now that you have a high
-
level idea of how the
WP

programming stack maps to
the Android st
ack, we are now going to go one level deeper. In the next section, we will
look at the user interface guidelines of
WP

applications.

Related Resources

To learn more about the topics covered in this blog,
visit
:



App Hub


Central
P
lace for
Windows Phone

development
. Getting started,
download tools and read all about
Windows Phone

development




MIX ’10 presentation

on
Windows Phone

Arc
hitecture by Istvan Cseri



Windows Phone

D
evelopment for
A
bsolute
B
eginners
. Video series that will help
aspiring
Windows Phone

developers get started.



App Hub Jump Start Tutorials




Introduction to
WP

programmin
g

on Codeproject.com

Other Resources you may find useful:



Overview of the
Windows Phone

Application Platform



Windows Phone

Team Blog




Windows Phone

Programming




Windows Phone Guide for
iPhone

Application Developers

Chapter 2: User Interf
ace Guidelines

14


Chapter 2: User Interface Guidelines

Microsoft’s
Windows Phone (WP)

uses a novel user interface called Metro.
WP

sets itself
apart with
its clean and simple design and emphasizes on color and typography.

In contrast to the application
-
focused design of Android,
WP

uses an information
-
centric
design. Instead of an array of application icons, the start screen of a
WP

consists of
dynamic tiles

that display critical information at a glance to the user. The tiles are
dynamic and they continuously display the up
-
to
-
date status of the ap
plications. For
example, they show you the next appointment on your calendar, or the number of new
emails waiting for your attention. Users can personalize their phones by pinning the tiles
that they care most about.

WP

introduces a new paradigm called hu
bs. Hubs bring related information together.
There are six hubs, People, Pictures, Music + Videos, Marketplace, Office, and Games. The
People hub
, in the inst
ance shown below, aggregates your address book contacts and
Facebook friends.




Windows Phone Guide for
iPhone

Application Developers

Chapter 2: User Interface Guidelines

15


Designing the Application Interface

While the design of the
WP

user interface is different from that of the Android, the core
design principles are very similar. Like Androi
d,
in WP

you

need

to keep in mind the
compact screen, lower CPU, and limited memory while designing the applications. In
WP
,
users use one application at a time, with just one screen visible.

Similar Application Design Goals

Usability and UI design are not after thoughts, but are the primary goals behind
applications on both the Android and
WP
. Applications need to be simple and focus on
key scenarios that most users care about.

Visual Elements and Direct Manipulation

Like An
droid, visual elements and direct manipulation of objects by touch are the key
characteristics of the
WP

application.
WP

provides a complete set of UI controls designed
for the phone.

WP

utilizes the same set of core multi
-
touch gestures as the Android wi
th similar
semantics, these include: tap, double tap, pan, flick, touch and hold, and pinch and
stretch.

Implications of the
S
imilarities for the
D
evelopers

For the most part, the application planning process is similar on both the platforms.
While designi
ng your
WP

application, you will focus on the same information that is
critical to the user. Your key design principles from the Android application will get
carried over
:

metaphors and direct manipulation with multi
-
touch. The need for
immediate feedback
and aesthetic appeal remains the same.



Windows Phone Guide for
iPhone

Application Developers

Chapter 2: User Interface Guidelines

16


Application User Interface Design

While there are similarities in the design principles of the applications on both platforms,
pay close attention to the user interface of the application for the
WP
. It is best to
take
advantage of the unique features and strengths of
WP

platform.

For the interface to provide a consistent experience across applications, applications on
WP

need to adopt the new
Metro desi
gn guidelines.

XML and XAML

Android uses XML for mapping purposes. A part of the compilation reads the XML,
performs translations, and generates Java. For example, in layout XMLs of Android, you
need to set it as content view in Activity, after defining th
e whole UI.

In
WP
,
XAML

act as an instantiation language. XAML does not act as a mapping medium
in
WP

as XML does in Android. The XML in a
XAML

document in
WP

contains the actual
code needed for UI and needs not be transformed into code, as in case of And
roid.

Controls and the Application Interface

The
WP

development tools and SDK

include a rich collection of Silverlight controls that
are designed specifically for usability and aesthetics.
While you can create your own
controls, it is best to use the standard controls wherever possible. These standard
controls respond to theme changes and provide a consistent user interface.

The following table shows the mapping between
WP

Silverlight cont
rols and
corresponding Android controls.

Android control

Windows Phone

control

Border

Border

ButtonView

Button

AbsoluteLayout

Canvas

CheckBox

CheckBox

GridView

Grid


HyperlinkButton

ImageView

Image

Windows Phone Guide for
iPhone

Application Developers

Chapter 2: User Interface Guidelines

17


Android control

Windows Phone

control

ListView

ListBox

MediaController

MediaElement


MultiScaleImage

ViewGroup

Panel

EditText

PasswordBox

ProgressBar

ProgressBar

RadioButton, RadioGroup

RadioButton

ScrollView

ScrollViewe
r

SeekBar

Slider

LinearLayout

StackPanel

EditText

TextBlock

EditText

TextBox

MapView

Map

WebView

WebBrowser


Panorama


Pivot

TimePicker

Timepicker

DatePicker

Datepicker

ExpandableListView


Gallery


ImageSwitcher


Spinner


Windows Phone Guide for
iPhone

Application Developers

Chapter 2: User Interface Guidelines

18


Android control

Windows Phone

control

TableLayout


TextSwitcher


ViewFlipper


ZoomControl


TabHost


SlidingDrawer


RatingBar


Toggle button

ToggleSwitch*

* ToggleSwitch and Datepicker/Timepicker control are part of the Silverlight for Windows
Phone Toolkit available on Codeplex:
http://silverlight.codeplex.com/releases/view/55034

As you can see,
WP

offers controls that correspond to almost all Android controls. While
the look and feel is different, they provide simi
lar functionality.

New Controls

in Windows Phone

WP

introduces a few novel controls that have no counterpart on the Android. Here are
some of the examples:



A multi
-
scale image, with image data at various resolutions, is appropriate for
allowing the user

when zooming into a photo.



Panorama control is a multi
-
screen page and allows a page to span horizontally
beyond the width of the phone.



The people hub is another great example. It allows a large amount of related
information to be presented.



Pivot con
trol, a novel control that is useful to manage views and display
information that is logically divided in sections.

Windows Phone Guide for
iPhone

Application Developers

Chapter 2: User Interface Guidelines

19





Notifications

Both Android and
WP

have notification services, but notifications play a key role in
WP
.
The tiles are used to display no
n
-
critical information without disrupting what the user is
doing. If you are using status bar notifications in Android, you can use tile notification as
a replacement in
WP

to show critical information.

The notification service also display toast notifica
tions that provide time sensitive
information, such as an SMS. The toast notifications are shown for about 10 seconds, but
the user may choose to ignore them. These are the same as Android toast notification.

Android

Functionality

Windows Phone

Status bar

notification

Non
-
critical information that user may
not respond to. That is informative to
user

Tile notifications

Toast notification

Time sensitive data that user may not
respond to

Toast Notifications

Alerts

Modal alerts that the user must
respond to

Application
notifications

Windows Phone Guide for
iPhone

Application Developers

Chapter 2: User Interface Guidelines

20




Tool and Tab
B
ar vs. Application
B
ar

WP features a versatile
application bar. The application bar can include up to 4 of the
most common views or application tasks. You can also use application bar menus for
additional context
-
sensitive tasks. If you are using action sheets in your Android
application, application b
ar menus give you
similar functionality.

Android

Functionality

Windows Phone

Status bar

Information about
device

Status bar

Title bar: to show title of
application or activity

Navigation, Title,
Buttons for views or
actions

Back button for back
navigation

Page title

View and actions on
Application bar

Comparing
Windows Phone

and Android Navigation

WP

application is a collection of multiple pages. Like on the Android, the user navigates
through different Activities using widgets such as buttons and links.

Like

Android,
on WP

the
Back

button

on the phone

allows the user to navigate back
between pages w
ithin an application, or across applications. It behaves much like the
Back

button in a browser. The
Back

button also closes menus and dialogs. As a
developer, you should consider what the
Back

button means to your user and plan to
override it appropriatel
y. For example, you may decide to pause a game
by
using the
Back

button

on the Phone
.

The other two hardware buttons on the
WP

phone, namely, Search and Home, have fixed
behavior.



Windows Phone Guide for
iPhone

Application Developers

Chapter 2: U
ser Interface Guidelines

21


Windows Phone
Frame and Page Structure

Each
WP

application has a single frame, and it includes areas for:



A page where application content is rendered. This is the content where widgets
or graphics are rendered.



A reserved space for the system tray and application bar. It also exposes certain
prope
rties, such as orientation to the application.


System Tray and Application Bar

On
WP
, the system tray includes indicators for various system
-
level status information.
The application bar includes the area for the most common application menus, which
may
include various data views or tasks.




Windows Phone Guide for
iPhone

Application Developers

Chapter 2: User Interface Guidelines

22


Page Structure of
Windows Phone

Application

The following diagram shows the structure of a typical
WP

data
-
bound application,
which resembles a navigation
-
based Android application.


When the user first starts t
he application, he or she is presented with a splash screen,
designed to welcome the user, as well as to create the perception of fast response. Splash
screens are usually an image file of the entire size of the display.

Usually the application starts with

the home page, the main navigation page, with links
for search, and other page widgets. Consider an application that shows information
about baseball teams and their players. The primary content page, marked as the
widgets page in the above diagram
, have
the content of interest.

For example,

a list of all
baseball teams. However, depending on requirement, the home page can also be the
primary content page.

This is a possible
application
usage scenario:



A user clicks one of the team links to visit the
team details page (
“W
idget
D
etails
P
age

) which can provide multiple views. The team details page may employ a
pivot control or panorama to display different views such as the team summary
and the list of all players (
“L
ist of
G
adgets

Page”
) from that team




A user selects one of the baseball players

and the application
takes the user to
the page with player statistics (

Gadget Details page

). The player statistics page
uses controls such as textblocks, multi
-
scale images, or other multimedia using a
MediaEl
ement control


Windows Phone Guide for
iPhone

Application Developers

Chapter 2: User Interface Guidelines

23




A user can also use the search widget to search and directly access the team page
(
“Widget D
etails

Page”
) or the player page (
“G
adget
D
etails

Page”
)

Application Templates

Eclipse does not provide the project templat
e for Android applications
. But

Visual Studio
provides numerous templates
for WP application development

to make your life easier.

Functionality

Visual Studio Template

Eclipse

Information drilldown
applications

Data
-
bound applications

Not available

Utility applications. For
example, Bubble Level

Windows Phone

utility
applications

Not available

Games

Windows Phone

Game (XNA)
applications

Not available

Flexible template to design
any application

General
Windows Phone

applications

Not available

You can choose the Windows Phone application template to either create an application
with functionality similar to the view
-
based or the window
-
based Android application
type. Lastly, the XNA based games application template gives you functionality
like

t
he
OpenGL
-
ES application.

Summary

In this chapter we looked at t
he similarities between the application design goals of the
Android platform and the
WP

platform. When you plan your
WP

application, you will be
able to leverage your existing work on Android
applications.

We also looked at the

application interface design to make sure you are taking
advantage of the
WP

metro design that uses
WP

interface guidelines. You will find that
the
WP

tools offer a large library of controls and gestures that have close

counterparts
on the Android.

This chapter also showed you the
use of innovative controls like panorama, and explore
the use of live tiles to build an engaging
WP

experience.

Related Resources

To go deeper into the topic discussed,
visit
:

Windows Phone Guide for
iPhone

Application Developers

Chapter 2: User Interface Guidelines

24




Windows Phone

User Interface Guidelines




Windows Phone

Developer Tools



Silverlight for Windows Phone toolkit

on CodePlex



Design resources for Windows Phone


Other Resources that you may find useful:



Application Page Model for
Windows Phone



Frame and Page Navigation Overview for Windows Phone



Windows Phone Guide for
iPhone

Application Developers

Chapter 3: The Developer and Designer Tools

25


Chapter 3:
The
Developer and Designer
Tools


With the release of the
Windows Phone

(
WP
) developer tools, Microsoft brings the user
-
friendly, high productivity Visual Studio
Development environment to WP. As

Android
application developers
you

are familiar with Eclipse
, and you
can quickly migrate to

WP

developer tools and work with ease.

A Comparison of Android and
Windows Phone

Tools

Visual Studio 2010 Express for Windows Phone is a full
-
featured Integrated Development
Environment (IDE). Visual Studio 2010 facilitates designing, developing, and debu
gging
of
Windows Phone

applications. The other
WP

tools that help you in the development
cycle of the Windows Phone application are:



Expression Blend



XNA Game Studio



Windows Phone Emulator

The
WP

developer tools offer the complete functionality that is pro
vided by the Android
application developer tools. The following table gives an overview of the functionality of
each of these tools. The table also indicates the equivalent tools you use for Android
application development.

Functionality

Audience

Android t
ools

Windows Phone

tools

Primary UI
design: Colors,
gradients, and
animation

UI designers

Defined in
XML, any XML
tool

Expression Blend

UI design

UI designers
and
programmers



ADT UI
plug
-
in



Third
-
party
tool like
DroidDraw

Visual Studio 2010 Express


and
Expression Blend

for Windows
Phone

Application
development
(coding)

Programmers

Eclipse

Visual Studio 2010 Express

for
Windows Phone


Windows Phone Guide for
iPhone

Application Developers

Chapter 3: The Developer and Designer Tools

26


You can use the Android team structure and overall development process to build
applications for
Windows Phone
. The
WP

toolset ensures that the entire team of
designers, developers, and testers familiar

with Android development tools will find it
easy to migrate to the
WP

toolset
.

Development Life
C
ycle and
Windows Phone

Developer Tools

Expression Blend 4 and Visual Studio facilitate a close collaboration between designers
and developers.

Both these tool
s share the same file structure and source files. Expression Blend uses
XAML for UI design and the XAML is consumed by Visual Studio. This system allows
separation of responsibilities between the designer and the developer while allowing
them to work toget
her seamlessly.


Project Management

for Windows Phone

Like Eclipse, Visual Studio 2010 Express for
WP

is a fully
-
featured IDE. Visual Studio 2010
Express allows
you

to:

Game
development
(coding)

Programmers

Eclipse

XNA Game Studio

Testing /
Emulation

Testers

Android
Emulator

Windows Phone Emulator in
Visual Studio 2010 Express

Windows Phone Guide for
iPhone

Application Developers

Chapter 3: The Developer a
nd Designer Tools

27




Manage the entire structure of the development project, the source, and the
resource
files



Configure the application codebase, known as Visual Studio Solution, as a
collection of projects in a separate functional unit

With Visual Studio 2010 Express,
you

can manage source files, share code, and manage
the work among team members. Visual St
udio integrates a compiler and a debugger.
You can access both the compiler and the debugger either from the GUI or the
command line.

To create a sample application, do the following:

1.

Start Visual Studio Express for
WP

from
Start

menu.

2.

In Visual Studio Exp
ress, click
File
.

3.

Click
New Project
.

4.

In the
New Project
dialog box, select
Windows Phone Application
.

5.

In the
Name of the project

text box, enter ShoppingList as the name of the
project, and click
OK
. Visual
Studio Expre
ss creates

the new project
.

The
Solut
ion Explorer

pane

displays the solution that you created. This
example
solution has only

the ShoppingList project. The project contains the sources, resources,
and properties.


Windows Phone Guide for
iPhone

Application Developers

Chapter 3: The Developer and Designer Tools

28


Note
:

Visual Studio Express for
WP

does not provide integration with source co
ntrol
systems. The Visual Studio Professional edition provides features such as integration with
various source control systems like Subversion. You can also use Visual Studio Team
System, which is designed for greater communication and collaboration among

the
development teams.



Windows Phone Guide for
iPhone

Application Developers

Chapter 3: The Developer and Designer Tools

29


The UI Design Tools

The
WP

developer tools include:



Expression Blend



Visual Studio UI Designer

WP uses Silverlight and a specific

XML markup language for the UI specification.

Visual Studio UI design tool is equivalent to the Ecl
ipse ADT UI plug
-
in.
As
Android
application developers

you are

familiar with
E
clipse ADT UI plug
-
in
and you
will find it
easy to use this tool. In our example, the main page for the ShoppingList solution,
MainPage.xaml, is alread
y open in the Visual Studio

UI D
esigner tool for editing.

To change the titles of the application and the current page, do the following:

1.

Right
-
click the title MY APPLICATION and select
Properties
.

2.

In the
Properties

window, select
Text

and enter SHOPPING LIST.

You can also change the title of the page by entering my list in the
Text

property of the
title.

To change the title of a page, do the following:




Windows Phone Guide for
iPhone

Application Developers

Chapter 3: The Developer and Designer Tools

30


3.

Drag a Text box from the
Toolbox
and place it above the textblock.

4.

Update the Text box’s
Text

property to delete any existing entry.

5.

Below
Properties
, click
TextBox1
, and enter
txtItem

to change the ID of the
textbox to
txtItem
.

6.

Resize the Text box by dragging its lower right corner so that its width is 300.

7.

Drag a
Button

to the right of the
Text
Box
.

8.

Change the button’s
Content
property to
Add
, and its
ID

to
btnAdd
.

9.

Resize the button so that its width is 140.

10.
Drag another
TextBox

and place it underneath the
txtItem

textbox.

11.
Resize the new
TextBox

so that it covers the rest of the phone screen.

12.
Upd
ate the
ID

of the new TextBox to
txtList
.

13.
Update the
Text
property of the new TextBox to
Nothing here yet!.


Your application looks as shown below:



14.
To start debugging, compile the application and launch
,

press
F5

or click
Debug


The application starts
the
WP

emulator, deploys,

and runs

the ShoppingList application.
You can click
Add.
But the application does not respond
because

there is no code for the
button you inserted in the previous steps.

Windows Phone Guide for
iPhone

Application Developers

Chapter 3: The Developer and Designer Tools

31



Note
: You can use the context menus to add the event han
dlers or to set the control
properties. The emulator’s integration with Visual Studio allows for a direct manipulation
of controls and makes it easy to add logic to the UI controls.

Expression Blend for
Windows Phone

Expression Blend for
WP

is a visual UI
design tool that is suitable for designers. There is
no exact equivalent to Expression Blend in Android development toolset. Expression
Blend is
like

VS Design. With Expression Blend, you can:



Drag and drop UI elements to design the UI



You can layout the c
ontrols accurately up to pixels. You can easily create and use
color palettes and gradients



Add special effects, such as reflections and shadows



Import Photoshop files and Android application resources to the Windows Phone
application



Create fine
application behavior and animations without any programming


Windows Phone Guide for
iPhone

Application Developers

Chapter 3: The Developer and Designer Tools

32



Expression Blend is for UI designers and Visual Studio Design is for programmers.
Programmers can also use Visual Studio Design

to integrate the application logic to the
UI design. You can also

use the Visual Studio UI design tool to design UI for applications.

Both Expression Blend and Visual Studio Design have:



A single control set that provides accurate fidelity to their run
-
time visual
representation. This feature allows you to easily visual
ize the application.



Same project structure and share the same source files for designers and
developers.



Use or produce XAML, which is a Silverlight XML declarative markup language
used for interface design. This feature allows a designer to work on the d
esign
using Expression Blend while the developer uses Visual Studio to design the logic
behind the application. You can establish a smooth design and development
workflow.



Windows Phone Guide for
iPhone

Application Developers

Chapter 3: T
he Developer and Designer Tools

33


Visual Studio

Visual Studio has a simple to use, full
-
featured, and a configurable

source editor. The
editor tool has various features that are familiar to Eclipse users. The features of the
editor include:



Flexible search



Rich editing



Format code



Auto
-
completion of code



Outline or hide code

To add logic to the application, do the follo
wing:

15.
To stop the running application, click
Debug
.

16.
Click
Stop Debugging
.

17.
Double click
Add
. The dialog displays
MainPage.xaml.cs

with a method
btnAdd
.

18.
Click the
MainPage

class.


19.
To add logic to the items that are added to the shopping list, edit the
btn
Add_click

method
.

20.
Enter the following code
: string tStr = txtItem.Text
;
.

Note
: When you enter t for txtItem, Visual Studio displays the auto
-
completion dialog
box. The Visual Studio equivalent for Eclipse auto
-
completion is called content assistant.

Windows Phone Guide for
iPhone

Application Developers

Chapter 3: The Developer and Designer Tools

34




21.
Enter the following code in the
btnAdd_click

method:


if (!String.IsNullOrEmpty(tStr))

Note:

When you type String, Visual Studio displays the auto
-
completion dialog box.
When you type Is, Visual Studio displays the class methods of the String cla
ss.



VS IntelliSense in Visual Studio

VS IntelliSense is a feature of the IDE which uses history, code context, and .NET
reflection for intelligent auto
-
completion. VS IntelliSense can suggest and complete
variable names, parameters, classes, and method
names. VS IntelliSense can also
generate appropriate code where needed, as shown in the
code

below:


To complete the event hookup, Visual Studio generates an empty stub for the event
handler
button1_click

method.




Windows Phone Guide for
iPhone

Application Developers

Chapter 3: The Developer and Designer Tools

35


Code Snippets in Visual Studio

Code Sn
ippets in Visual Studio is equivalent to code templates in Eclipse. Code Snippets
allows you to insert code fragments with a few clicks. Visual Studio contains a large
number of snippets. These snippets help
you

to create their own snippets. You can also
c
reate an index and search for the
code snippets by using the self
-
defined terms.

To use a code snippet, do the following:

22.
To start the
Insert Snippet

prompt, press
CTRL+K

CTRL+X

23.
To insert a code snippet for an if statement in the code, select
Visual C#

followed
by

i



The inserted snippet shows the parts of the code that
you need

to complete.

private void

btnAdd_Click(
object

sender, RoutedEventArgs e)

{


string

tStr = txtItem.Text;


if

(!String.IsNullOrEmpty(tStr))


{



if

(
true
)



{



}


}

24.
To complete

writing the code for the method, enter the required code so that the
body of the method is complete as follows:


string

tStr = txtItem.Text;


if

(
!String
.IsNullOrEmpty(tStr))


{



if

(txtList.Text == “Nothin here yet”)



{




txtList.Text = “”;



}



txtL
ist.Text += txtItem.Text + “
\
n”;



txtItem.Text = “”;


}

Windows Phone Guide for
iPhone

Application Developers

Chapter 3: The Developer and Designer Tools

36


Visual Studio supports various refactoring mechanisms. You can select any piece of code
and right
-
click the code to access the refactoring menu.

Visual Studio
E
ditor

Visual Studio editor is customi
zable. Visual Studio editor allows you to define various
keyboard shortcuts or create your own macros. Macros help you to automate repetitive
actions by combining a series of commands and instructions.
You

can easily customize
Visual Studio
E
ditor to use s
hortcuts and keyboard combinations with which they are
familiar.

Instead of opening a separate window for each file, as in Eclipse, the default view in
Visual Studio uses tabbed windows. You can change this behavior to suit your need. You
can change the w
ay in which various windows are docked within Visual Studio Shell.

Building Applications

Visual Studio Express for
WP

allows you to build the Visual Studio solution on demand. It
allows you to separately build each project to make a part of the complete
solution.

Visual Studio uses an XML based, declarative build system called MSBuild. MSBuild

is
equivalent to Ant and Nant. You can access the builds either from the GUI or by using
the command line for batch processing. MSBuild is flexible and allows you t
o create a
specific target either as a debug build or as a release build.


Windows Phone
Emulator

T
he
WP

Emulator provides a virtualized environment in which you can deploy, debug,
and test applications. The
WP

Emulator is designed to provide a comparative
performance of an actual device and meets the peripheral specifications required for
application development.

To access the
WP

Emulator

from Visual Studio
,
do any of the following:

Windows Phone Guide for
iPhone

Application Developers

Chapter 3: The Developer and Designer Tools

37




From
Deploy

menu, click
Deploy Solution



Press
F5

in the key board, this starts the Emulator along with the debugger



Press
CTRL+F5,

this start only the Emulator

The
WP

E
mulator

is
like

the Android
S
imulator.
However, these differences are there:



I
n
A
ndroid emulator you can have ac
cess to all the system application
like

Phone
Dialer, Contacts, Mail,
and
Media Gallery
. But the
WP

E
mulator

the system

applications are not directly accessible to
you
. Though it can be accessed by
the
L
aunchers and
C
hoosers.



A
ndroid
supports
various displ
ay height and width
. Likewise, you can
customize
the
Android
emulator
for parameters
like
S
creen
H
eight,
W
idth,
RAM

size,
and
Sensors
. As
WP

have specific hardware requirements
, you cannot modify the
WP

E
mulator

for these hardware parameters.

Debugging

Vis
ual Studio Express Phone 7 includes a symbolic debugger that you can use with the
WP

EMULATOR
or with a remote device. Once the application loads in the debugger,
you can view the variables in the application and control their execution.

To work with
debugger, do the following:

25.
To
start

the debugger, press
F5

26.
In
Textbox

you previously created, enter
napkins

and click
Add
. Refer to the
image below.


Windows Phone Guide for
iPhone

Application Developers

Chapter 3: The Developer and Designer Tools

38


Note
:
napkins

is added after Nothing here yet!

27.
In Visual Studio, click the light blue area to the left
of the string tStr =
txtItem.Text; line.


Visual Studio inserts a breakpoint at that line.


28.
Launch the debugger again using
F5
.
When the application loads into the
debugger, rest the mouse pointer over
txtItem

in the code

29.
Click
+

in the pop
-
up to view t
he variable
txtItem
.

Tracking Variables

You can view the details of the variable
s

such as the
T
ype,
Fi
elds, and
P
roperties. The
dialog

below shows how you can
scroll

up and down the
T
ype
H
ierarchy to inspect the
objects.


You can set a watch on certain va
riables to keep them under observation continuously.

Windows Phone Guide for
iPhone

Application Developers

Chapter 3: The Developer and Designer Tools

39


To track variables, do the following:

30.
Right
-
click
txtList
, and click
Add Watch.

The watch window displays the variable
txtList
.

31.
Click
+ icon
near txtList
,

to expand txtList.


32.
To ensure that the
control does not enter the if statement, press F10 to step
through the code.

if

(txtList.Text == "Nothing here yet")


{


txtList.Text = "";


}


You can observe in the
Watch

window that the value of txtList.Text is
Nothing here yet.

But it gets compared with
Nothing here yet

(with no exclamation point.) This is a bug in
the code.

Change the statement as shown below to add the exclamation point:

if

(txtList.Text == "Nothing he
re yet!")


Note
: While in the debugger, you can use the VS immediate mode where you can write
the managed code instructions to modify or view the variables or execute code to help
with debugging.


33.
Update the code and re
-
launch the debugger by pressing
F5
.

Windows Phone Guide for
iPhone

Application Developers

Chapter 3: The Developer and Designer Tools

40


34.
To test the application, add items to the
shopping list
.


A Note about Debugging

You will observe that you can easily debug a
WP

application using the managed
programming environment.
Like

Eclipse, the debugging in
WP

application is done
entirely at the

application level using the C# code and types.

Note
: The .NET framework includes two classes called Debug and Trace, which help you
to write run
-
time debug messages to the output window. C# also supports an assert
statement, which is evaluated at run time
. If the statement returns true, Visual Studio
does not respond. But if the statement returns false, th
e program enters the debugger.



Windows Phone Guide for
iPhone

Application Developers

Chapter 3: The Developer and Designer Tools

41


Summary

In this chapter we looked at
t
he
Windows Phone

Developer Toolset
. The Toolset

includes
the rich tools that are
designed to support every step in the entire application
development lifecycle. The design, development, and the testing tools are equivalent to
the existing Android team roles and processes. The tight integration between the
WP

tools help
s

you to streamli
ne the design, develop the workflow, and test the workflow.
These tools provide an end
-
to
-
end functionality and are highly customizable.

Windows Phone Guide for
iPhone

Application Developers

Chapter 4: C# programming

42


Chapter 4: C# programming

This chapter discusses the C# features that map to the most common Java features. This
chapter includes the code snippets, which help you to work faster with C# code. It
describes important C# features that help you write safe code and enhance productivity.

The chapter discusses the following features of C# programming:



Managed Programming



C
lass Declaration



Strong Typing



Class Constructors



Properties



Parameter Types



Access Privileges



Methods with multiple Parameters



Inheritance



Protected Access



Instance vs. Class Level Access



Abstract Classes



Interfaces



Polymorphism



Structs



Object Lifecycle:
Creation and Deletion of Objects



Type Checking vs. Reflection



Exception Handling



Windows Phone Guide for
iPhone

Application Developers

Chapter 4: C# programming

43


Managed Programming

WP

supports
managed programming

in C# and VB.NET.


The C# compiler (and
similarly, the VB compiler) compiles the C# (or VB.NET) code in an
intermediate language (IL) byte code and metadata. The Common Language Runtime
(CLR) executes the bytecode. The C# uses metadata to manage type safety, exception
handling, and array bounds.

CLR also manages memory and performs garbage
collection. In Android, the Java code is compiled in a .class file, and the file is converted
to a .
dex

file. The .
dex

file is optimized for low foot print on memory. The .
dex

file is then
converted to an
apk

f
ile, which is executed on an android platform.



Windows Phone Guide for
iPhone

Application Developers

Chapter 4: C# programming

44


A Comparison between C# Features and Java Classes

Class Declaration

Like Java, C# does not separate class definition from class implementation. The compiler
derives the metadata about the classes from class

implementation. As in Java we can
define multiple

class definition in same file.

C# also defines the multiple classes in same
file
.

using System; // In java this is similar to import

namespace FirstApplication // scope for classes. Same lik
e package declaration in Java

{

class
Person



// only uses class implementation

{


private
DateTime

birthDate; // a private field accessible to this class


private int ageOn(
DateTime

date)
// a private method


{


TimeSpan

span = date.Subtract(birthDate); //uses a .notation to invoke


return span.Days;


}


public int age
// this is a property.



{


Get // just a getter; it’s a read
-
only property


{


return this.ageOn(
DateTime
.Now);





}


}



public Person(
DateTime

dob) // instance constructor. Same like Java


{





// it combines allocation and initialization


birthDate = dob;




}

}

class
Program

//
Same like Java, another class in the same file.

{


static void Main(string[] args) // main entry point into the program


{


Person

p = new
Person
(new
DateTime
(1973,11,12));
//construct an instance


System.
Console
.WriteLine(
"The age is is"
+ p.age.ToString());


DateTime

dt = p.birthDate; //error in compilation birthDate is private



}


}

}


The following example shows the public signature of the class Person that consists of
proper
ty, age, and constructor.

Instead of using the
import

statement in Java, C# employs a
using

statement to refer to
the metadata of other classes. The namespace declaration that is shown at the top of the
file is used to declare the scope and to organize th
e code. You can access classes in other
namespaces by referring to a fully
-
qualified name. You can refer to
System.Console.WriteLine in the example above, where the console is in the System
namespace.

C# uniformly uses the “ ” notation for referring to all

methods, fields, and properties.

Windows Phone Guide for
iPhone

Application Developers

Chapter 4: C# programming

45


Strong Typing

C# is a strongly typed language
like

Java. The types must be specified for variables and
input/output parameters. The compiler enforces the types. In the section on generics,
you can see how C# uses stro
ng typing for collection classes.

Strong typing works similarly for all classes. The code example below shows the strong
typing for primitive types.


int

a = 5;

int

b = a + 2;
//OK


bool

test =
true
;
// OK

int

c = a + test;
// Error.
Operator '+' cannot mix type 'int'
and 'bool'.

Class Constructors

Like Java, C# uses instance constructors to create and initialize instances. For example: p
is an instance of the Person class. You can construct and initialize p with a given
birthdate, in a single statement.

Person

p =
new

Person
(new
DateTime
(1973,11,1
2));

Properties

You

often need to decide about whether to implement a member as a property or a
method. In this case, the design pattern is identical for both Java and C#. As a general
rule, use properties when you need to access data. To take any action o
n the data, use
methods.

Properties helps to abstract away from directly accessing the members of a class. This
technique is similar to using accessors (getters) and modifiers (setters) in Java. You can
make the property read
-
only by providing the getter,
write
-
only by providing the setter,
or read
-
write by providing both getter and setter.

Parameter Types

Like

Java, C# uses the value parameters by default. C# does not have pointers. But C#
allows the passing of parameters by using the ref modifier. Instead

of pointers, you can
use parameters with ref where you want to achieve an additional functionality in a
method. In some cases, you need to use the reference parameters for better efficiency
since they avoid data copying.



void

Foo

(
ref

int x, int y)

Windows Phone Guide for
iPhone

Application Developers

Chapter 4: C# programming

46


{


x =
0
;


y = 0;

}

..

int a = 5;

int b = 8;

Foo (
ref

a, b);

//a is zero and b is still 8

C# also provides the parameters with an out modifier. This out modifier represents the
parameters that must be initialized by the called method before returning. This de
sign
pattern is often used to return the error in addition to the value of the function.

Access Privileges

Like Java, C# allows the access privileges on fields (for example, birthDate), properties
(for example, age) and methods (for example, ageOn). C# use
s public, private, and
protected as modifiers to denote the three different levels of access privileges.

In the above example, the compiler gives an error response on p.birthDate since that
variable is private. Therefore p.birthDate is not accessible from

the Program class.
Similarly, the ageOn method is also private and inaccessible from the Program class.

Methods with
Multiple P
arameters

Both Java and C# support methods with multiple parameters. While C# traditionally uses
positional and unnamed param
eters, the latest version of C# has introduced the named
parameters. The following example shows the comparative syntax for Java and C#.

Java

C#

void addEmployee(string name, int
id, int age);

void addEmployee(string name, int
id, int age);


Off.addEmployee("Phil",2345, 23);

Off.addEmployee("Phil",2345, 23);

Off.addEmployee(name: "Phil",
age:23, id:2345);

Like Java, C# explicitly supports method overloading. C# differentiates between the
methods that have the same name by using information a
bout the parameter types. The
following code sample shows how C# differentiates between methods that have the
same name.

Windows Phone Guide for
iPhone

Application Developers

Chapter 4: C# programming

47


void

insert (myClass obj,
int

index);

void

insert (myClass obj, myClass before);

The method insert may be called with both these
signatures:

list.insert (my
Obj1
,
4)
;

list.
insert (myClass obj, myClass before);

The following code sample shows another example of method overloading:

using

System;

namespace

SecondApplication

{


struct

Point

// In contrast Java where struct not
supported, C# structs are closer


{
// classes.


public

double

x;
// struct fields can also have access
modifiers


public

double

y;


publ
ic

Point(
double

p1,
double

p2)
//a constructor for the struct


{


x = p1;


y = p2;


}


}


interface

IThreeDShape

// an interface, like an Java interface


{

// defines the behavior


double

volume


{


get
;
// Volume is a read
-
only property. no
setter


}


}


abstract

class

Shape

// this class is marked abstract, i.e. may
not
be instantiated.


{


protected

Point

origin;
//only derived classes may access


protected

static

int

counter = 0;
// Similar to protected variables in Java


public

string

ID;


protected

Shape()
//a constructor. Same name as the class
name

{


counter++;
// class variable being updated


}


public

Point

Origin
// similar to Java class

variables


{



set


{


origin =
value
;


}


}


public

abstract

double

Area
//denotes that this property must be
overridden


{
// in a derived class


get
;



}


public

abstract

bool

contains(
Point

p);
// this method must also be
overridden


}



class

Rectangle

:
Shape

//Similar to Java, single iinheritance

Windows Phone Guide for
iPhone

Application Developers

Chapter 4: C# programming

48



{


public

double

length;

//field accessible from others


public

double

width;


public

Rectangle(
Point

o,
double

l,
double

w)
//a public constructor


{


ID =
"Rectangle_"

+ counter.ToString();


origin = o;


length = l; width = w;


}


public

Rectangle(
double

l,
double

w)
// one constructor using another
constructor

//creates a rectangle at the origin


:
this
(
new

Point
(0, 0), l, w)


{


}


public

overri
de

double

Area
// unlike Java, overridden method must


{
// use override keyword


get


{


return

length * width;


}


}


public

override

bool

cont
ains(
Point

p)


{


if

((origin.x < p.x && origin.x + length > p.x) || (origin.x > p.x &&
origin.x
-

length < p.x))


if

((origin.y < p.y && origin.y + length > p.y) || (origin.y > p.y &&
origin.y
-

length < p.y))



return

true
;


return

false
;


}


}


class

Square

:
Rectangle


{


public

double

side;


public

Square(
double

s)


:
base
(s, s)
//constructor


{


ID =
"Square_"

+ counter.ToString();


side = s;


}


}

class

Cube

:
Shape
,
IThreeDShape

//similar to Java, class implements
interface


{


public

double

side;


public

Cube(
double

s)


{


ID =
"Cube_"

+ counter.ToString();



side = s;


}


public

override

double

Area


{


get


{


return

6 * side * side;


}


}


public

double

volume


{


get


{


return

side * side * side;


}

Windows Phone Guide for
iPhone

Application Developers

Chapter 4: C# programming

49



}


public

override

bool

contains(
Point

p)





}


class

SecondProgram


{


static

void

printVolume(
IThreeDShape

tdShape)


{


Console