Windows Phone 8 Guide for Android Application Developers

vermontdroningΚινητά – Ασύρματες Τεχνολογίες

10 Δεκ 2013 (πριν από 3 χρόνια και 8 μήνες)

232 εμφανίσεις






Microsoft Open Technologies, Inc.

8
/
1
/
2013

Re
v 1.0

Windows Phone
8
Guide for
Android

Application Developers

Windows Phone Guide for
Android

Application Developers





Table of Content

Table of Content

................................
................................
................................
................................
2

About this Development Guide

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

4

Chapter 1: An Introduction to Windows Phone Platform for Android Application
Developers

................................
................................
................................
................................
..........
5

The Developer Tools

................................
................................
................................
................................
...
5

Windows Phone Architecture

................................
................................
................................
...................
5

Comparing the Programming Stack of Windows Phone with the Android Stack

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

7

Summary

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

11

Related Resources

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

11

Chapter 2: User Interface Guidelines for the Windows Phone

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

12

Designing the Application Interface

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

13

Application User Interface Design

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

14

Comparing Windows Phone and Android Navigation

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

19

Windows Phone Frame and Page Structure

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

20

Application Templates

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

21

Summary

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

22

Related Resources

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

22

Chapter 3: An introduction to Windows Phone Developer and Designer Tools

...........

24

A Comparison of Android and Windows Phone Tools

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

24

Visual Studio is not only a powerful IDE but it can also be extended in a variety of ways. It
allows the ability to add macros, options, menus and toolbar commands, visualizers and
other tools. In addition, Visual Studio supports NuGet, a package manager,
to add, remove
and manage libraries and tools in Visual Studio and the .NET Framework.

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

25

Windows Phone tools l

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

26

Tool for coding

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

26

Tools for designing

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

27

Building Application

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

37

Debugging

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

3
8

Summary

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

43

Chapter 4: An
Introduction to C# programming for Java developers

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

44

Managed Programming

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

45

A

Windows Phone Guide for
Android

Application Developers




Comparison between C# and Java Features

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

46

Key features compared

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

57

Summar
y

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

66

Related Resources

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

66

Chapter 5: Application Lifecycle Differences Between Windows Phone and Android

.

67

Android and Wind
ows Phone Application Lifecycle

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

67

Programming for application States and navigation

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

69

Windows Phone Application LifeCycle

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

69

Comparing Life
-
cycle Methods

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

70

Windows Phone 8 Application LifeCycle and Tombstoning Example

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

72

Summary

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

77

Related Resources

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

77

Chapter 6: Storage on Windows Phone

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

78

IsolateStorageSettings lets you save any serializable object to the settings store.

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

83

Managing the IsolatedStorage Space

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

83

Sharing Data Between Applications

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

83

Content producers
................................
................................
................................
................................
....

84

Summary

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

84

Related Resources

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

84

Chapter 7: XML Support in Windows Phone and Android

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

85

Traversing XML

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

89

Summary

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

91

Related Resources

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

91

Appendix A
: Migration Sample

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

92

In
-
App Advertisements
................................
................................
................................
............................

93

Appendix B: Using the API Mapping Tool

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

101

What’s the API Mapping tool

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

101

How to use the tool

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

101

What's n
ext?

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

101



Windows Phone Guide for
Android

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
.





Windows Phone Guide for
Android

Application Developers

Chapter 1: An Introduction to Windows Phone Platform for Android Application Developer
s

5


Chapter 1:
An
Introduc
tion to

Windows
Phone

Platform
for

Android Application
Developers

In late 2010,

Microsoft announced the release of
Windows Phone

7 which was followed by
the release of Windows Phone 8 in late 2012. A large number of

devices
running Windows
Phone operating system

are available
from a variety of manufacturers all over the world.
Over a hundred thousand

apps

are available on the
Windows Phone

Store
.


For
W
indows
P
hone
, Microsoft went back to the drawing board to figur
e out what phone
users really want, and built a phone from the ground up. The operating system, the user
experience, and the application development platform have all been engineered with users in
mind. The revenue opportunities in the Windows Phone market
place, accompanied by a
great set of development tools, make
W
indows
P
hone

a very attractive
platform

for
developers to build applications and games.

The Developer Tools

Microsoft
has
released a set of tools
as part of the Windows Phone
Software Developme
nt Kit
(
SDK
)

8.0
for
developing Windows Phone applications
. Th
e SDK

is free and can be
downloaded from
here
.

The Windows Phone SDK 8.0

includes:



Visual Studio Express
2012
for Windows Phone,
an IDE for developers



Blend for
Visual Studio 2012
,

a

User Inter
face design tool for designers



A Windows Phone
emulator to test and debug applications

The tools

can help

you develop consumer applications, business applications or games.
While
the
emulator must be installed on Windows 8 pro edition

on a
CPU supporting
Second Level
Address Translation (
SLAT
)

for running Hyper
-
V
, you can still use the SDK to develop
Windows Phone 8 apps even if your computer doesn’t support the emulator.

See
System
requirements for Windows Phone Emulator

for more information.



Windows Phone

Architecture

Like Android OS,
W
indows
P
hone

OS

is designed to
run on multiple p
hones

from multiple
vendors
. To provide a consistent user experience and features that
developers

can rely on,
W
indows
P
hone

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

Windows Phone Guide for
Android

Application Developers

Chapter 1: An Introduction to Windows Phone Platform for Android Application Developers

6


Minimum hardware specifications

include an ARM7 CPU, a DirectX capable GPU, a camera,
and a multi
-
touch capacitive display. Standard sensors include: an A
-
GPS, an accelerometer,
a compass, proximity and light sensors. There are three standard physical buttons on the
phone


back, start
and search. As we will see in a subsequent chapter, these buttons provide
an easy and natural navigation model for the user.




Most of the Windows Phone 8 apps are built as managed apps using XAML with C# or VB.
Windows Phone 8 games are built using Direct3D using C++. It is also possible to render 3D
graphics within a XAML UI. It is also possible to build a Direct3D game using a
thin XAML
wrapper.

For more information, see
Developing apps for Windows Phone
. Windows Phone 8
adopts many of the components of Windows 8, providing
better compatibility between the
two.



Figure
1

Windows Phone 8 Programming APIs

.NET API for Windows
Phone

Windows Phone
Runtim
e

Win32 & COM

C# / VB

C++

Apps with XAML

Apps with XAML
and Direct3D

Games with Direct3D

Windows Phone Guide for
Android

Application Developers

Chapter 1: An Introduction to Windows Phone Platform for Android Application Developers

7


Comparing the Programming Stack
of Windows Phone

with
the
Android
Stack

The Application Model shown above provides services

for managing the application lifecycle

such as installation and update. The UI model helps t
o manage
the
application user interface.
Applications are built using various
W
indows
P
hone

Application Programming Interfaces,
APIs, known collectively as the Windows Phone API

This table gives an overview of the
W
indows
P
hone
8

frameworks that provide f
eatures
comparable to the Android programming layers.


Android Frameworks

Functionality

Windows Phone

8

Frameworks

Android
A
pplication
F
ramework


Application UI, Device
integration (sensors, camera)

Windows Phone API

Graphics, Animation, Media

Direct3D

or
XAML

media and
graphics for others

Android
L
ibraries

Base services, Networking, Text,
XML, storage

Windows Phone API

Android runtime
(Dalvik)+ Linux kernel

Operating System

Windows Phone

8

OS




Windows Phone Guide for
Android

Application Developers

Chapter 1: An Introduction to Windows Phone Platform for Android Appli
cation Developers

8


Android and
Windows Phone

Stacks
S
ide

by
S
ide

This table compares the Android and
W
indows
P
hone
8

Frameworks side by side.


Managed Code

On
Android,

you have been developing applications primarily using Java and some native
components using C++.

On Windows Phone 8


you have the option of using C# or
Visual
Basic

(

managed code

)

for XAML
applications
or C++ for Direct3D game development


Execution of C# or
Visual Basic (
VB
)

code

is managed

by the
.NET Common Language
Runtime (CLR)
.
.

Similar to Dalvik VM

on Android
,
CLR
is the V
irtual
M
achine (VM)

on
W
indows
P
hone OS
.
Similar to Java, C# and VB do not require memory management;
CLR
handles

garbage collection.


Android Frameworks



Windows Phone Frameworks


Application UI and Phone

Multi
-
tasking



Java



C#
,

VB
, or C++

Multi
-
tasking

Application UI and
Phone integration

Application UI


Application UI

Device integration


Device integration

Telephony


Telephony


Browser Control


Browser Control

Notifications


Notifications

Peer to Peer Gaming




XAML



Direct3D


Two Application Types

Controls & Gestures


Controls &
Gestures

CoreWindow
(PointerP
ressed


Media

Media Framework


Media

Media

Found
ation
,
XAudio2, WASAPI

Animation


Animation

Direct3D

Graphics


Graphics


Direct3D

O
penGL
-
ES


File System


IsolatedStorage


Windows Phone API

SQLLite



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
Android

Application Developers

Chapter 1: An Introduction to Windows Phone Platform for Android Application Developers

9


While most Windows Phone apps are written in C# or VB, y
ou may want to use native C++
code for the following reasons:

1.

Portability: This is
useful if you are targeting multiple platforms you can write core
login in C++.

2.

Direct3D
: If you want to use Direct3D esp. to develop games, you must use C++.

3.

Performance: For some scenarios, you may achieve higher performance using native
code.

In this
paper we are going to focus on managed code.

Base Services

W
indows
P
hone

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

Android core libraries include

functionality for

media services such as
audio and video
, graphics and animation using OpenGL and SGL, SQLite for database
support and networking services. It also includes LibC and interface libraries to interact with
Dalvik VM and
the
Linux kernel.


The
W
indows
P
hone

Base Class Library layer

include
s

base classes, collections, threading, text
processing and IO.
The

base class library

also
includes networking stacks, such as HTTP and
the Windows Communication Foundation (WCF). WCF provides an easy interface with XML
and SOAP services across the web, wi
th support

for

XML data transfer,
serialization/deserialization and XML parsing.

While
Windows Phone 8

does not
have a pre
-
installed

local database such as SQLite,
developers can
use SQLite in their own application or use Language Integrated Query (LINQ)
.
LINQ support

SQL
-
like queries in C# to query data

loaded in memory,
stored in isolated
storage (see below), or in remote databases such as SQL Azure.




Application UI and Device Integration

You
have
a
choice of two different stacks

in
W
indows
P
hone

m
edia

applications
,
namely,

XAML
and
Direct3D
.
XAML for Windows Phone

provides controls that are similar to
Android
standard widgets.

I
t is recommended that you use
XAML for Windows Phone

for consumer or business
applications and
Direct3D

for games, altho
ugh you can certainly also develop games using
XAML

animation.
It is also possible to mix XAML and Direct3D to take advantage of specific
features available in the two stacks.


Windows Phone Guide for
Android

Application Developers

Chapter 1: An Introduction to Windows Phone Platform for Android Application Developers

10


Android applications





Windows Phone counterparts


UI using
activity(contains
widgets) with navigation
between them



Windows Phone

Applications
:

XAML

apps with pages connected by flows


2D or 3D games built with
O
pen
GL
-
ES





Direct3D
games

Direct3D

for Games

Windows Phone 8 supports Direct3D for gaming and
enables

hardware accelerated 3D
rendering and graphics.

For a high performance game,
Direct3D

is the
right

option.

Controls and Media

Y
ou
have been
us
ing

Android
controls

in your application,
,
corresponding control
s and
support for multi
-
touch are

availa
ble via
XAML
.
Extensible Application Markup Language
(XAML)
is a declarative language used to design UI
.
Windows Phone

controls

are specifically
designed for the primary interaction with touch on a small form factor device.
Developers
can use separate code
-
behind files, written in C# or VB, to respond to events or
to
manipulate the controls.



Windows Phone

provides high performance audio and video
using a

variety of CODECs. It
supports both vector and bitmap graphics with
hardware acceleration. As opposed to a file
system,
Windows Phone

provides sandboxed storage, called
I
solated
Storage, to store
application
-
specific data. With the isolation of storage, one application cannot affect other
applications that are running on
the phone.

Windows Phone
Platform Features

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

Windows Phone Guide for
Android

Application Developers

Chapter 1: An Introduction to Windows Phone Platfor
m for Android Application Developers

11


Similar to multitasking in Android,
Windows Phone supports multi
-
tasking.
Windows Phone
mu
l
ti
-
tasking support
s

fast application switching which allows

users
to

rapidly switch between
applications. Multi
-
tasking also supports running scheduled tasks such as alarms and
reminders, background music
, GPS tracking

and
remote communication
.
I
f you have used
ADMob
API
to
publish advertisements in
your Android
application
, you will find similar
functionality in the
Microsoft Advertising SDK for Windows Phone
.


Summary

In this chapter we looked

at the
W
indows
P
hone
8

architecture and the Android and
W
indows
P
hone
8

programming stacks. Now that you have a high
-
level idea of
mapping
between the two
, we are now going to go one level deeper. In the next section, we will look
at the user interface guidelines of
Windows Phone 8

applications.

Related Resources

To learn more,
visit
:



Windows Phone Dev Cent
er
. Getting started, download tools and read all about
Windows Phone

development



Building Apps
for Windows Phone 8 Jump Start
s
. Video series
tailored for
developers
to build Windows Phone apps
.



Windows Phone Developer Forums



Developing for Windows Phone Jumpstart Series on Channel 9



Developer Documentation



Windows Phone Code Samples

Other Resources you may find useful
:

1.

Overview of the
Windows Phone

Application Platform

2.

Windows Phone team blog
.


3.

Windows Phone

Programming
: Programming guide and reference documents.

4.

Visual C#


5.

XAML for Windows Phone




Windows Phone Guide for
Android

Application Developers

Chapter 2: User Interface Guidelines for the Windows Phone

12


Chapter 2: User Interface Guidelines

for the
Windows Phone

Microsoft’s
Windows Phone

uses a novel user interface
that
sets itself apart with its clean and
simple design
,

emphasi
s on

color and typography. In contrast to the application
-
focused
design of Android,
W
indows
P
hone

uses an information
-
centric design. Instead of an array of
application icons, the start screen of a
W
indows
P
hone

consists of
dynamic tiles

that display
critical information at a glance to the user. The tiles are dynamic
in the sense
they
continuously display the up
-
to
-
date status of the applications
. For example,

the

Calendar app
tile

show
s

you the next appointment
,

the
email tile shows the
number of new emails waiting
for your attention. Users can personalize their phones by pinning the tiles that they care most
about.

Windows Phone

introduces a new paradigm called “hubs”. Hubs bring related information
together. There are six hubs, namely, People, Pictures, Music + Videos, Marketplace, Office,
and Games. The
People hub
,
is more than an address book. It

aggregates your address book
c
ontacts
, as well as people and their updates from
Facebook
, Twitter, and LinkedIn
.





Windows Phone Guide for
Android

Application Developers

Chapter 2: User Interface Guidelines for the Windows Phone

13


Designing the Application Interfac
e

While
Windows Phone user interface is different from that of the
Android
,

there are
similarities in the core design principles
.

Si
milar Application Design Goals

Like Android
,
W
indows
P
hone

developers have to keep in mind the compact screen, lower
CPU and limited memory while designing the applications. Users

can

use one application at a
time, with just one screen visible.
Both Androi
d and W
indows
P
hone

are supported on variety
of devices with differing screen resolutions and hardware support. Developers must keep this
in mind while designing their application. For additional details on how to built multi
-
resolution apps, read this
article
. In both Android and Windows Phone, u
sability and UI
design are not after thoughts, but are the primary goals behind. Applications need to be
simple
and focus on key scenarios that most users care about.

Visual Elements and Direct Manipulation

Like Android, visual elements and direct manipulation of objects by touch are the key
characteristics of the
W
indows
P
hone

application.
W
indows
P
hone

provides a complete set
of UI controls designed for the phone

that can be manipulated using touch
.

The controls
provide functionality similar to those on Android.
It utilizes
similar

set of core mul
ti
-
touch
gestures as Android
with similar semantics


th
ese 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
designing your
W
indows
P
hone

a
pplication, 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:
direct manipulation with multi
-
touch, the need for immediate feedback and aesthetic
appeal, will

still remain the same.




Windows Phone Guide for
Android

Application Developers

Chapter 2: User Interface Guidelines for the Windows Phone

14


Application User Interface Design

While there are similarities in the design principles of the applications on both platforms, pay
close attention to the
W
indows
P
hone

application user interface
. It is best to take advantage
of t
he unique features and strengths of
the
W
indows
P
hone

platform.

Design Guidelines

W
indows
P
hone

provides clear
design guidelines
, and recommends
consistent UI controls
,
colors, typography and images
.

Together, they provide a consistent user experience.

This can
be contrasted with
Android which
, for a long time,

did not encourage simplicity and
consistency among application designs.
. Unlike Android,
Microsoft strongly encourages you
to follow the de
sign guidelines so that the users have a consistent user experience.



XML and XAML

Android UI elements such as widgets and layouts may be specified using XML or created
programmatically at runtime or both.
Windows Phone uses a similar
mechanism
. Typical
ly,
you use
XAML

to develop
W
indows
P
hone

applications. ,
Extensible Application Markup
Language

(
XAML
)
is an XML
-
based markup language used for visual representation of
applications in Windows Phone.


While
both Android and
W
indows
P
hone

use XML based languages to specify the UI
elements,
designing
W
indows
P
hone

UI is much easier thanks to XAML support in
W
indows
P
hone

design tools

namely,

Visual Studio and Blend
. Both tools support WYSIWYG UI

design
and
produce
common
XAML code.


Controls and the Application Interface

The
Windows Phone 8 development tools and SDK

include a rich collection of controls

for
Windows Phone

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
Android controls and corresponding
W
indows
P
hone
8

controls.

Android con
trol

Windows Phone

control

Notes

Border

Border


ButtonView

Button


Windows Phone Guide for
Android

Application Developers

Chapter 2: User
Interface Guidelines for the Windows Phone

15


Android con
trol

Windows Phone

control

Notes

AbsoluteLayout

Canvas


CheckBox

CheckBox


GridView

Grid



HyperlinkButton


ImageView

Image


ListView

ListBox


MediaController

MediaPlayerLauncher


MediaPlayer

MediaElement



MultiScaleImage


ViewGroup

Panel


EditText

PasswordBox


ProgressBar

ProgressBar


RadioButton,
RadioGroup

RadioButton
,
ToggleSwitch
*

ToggleSwitch avail on
CodePlex

ScrollView

ScrollViewer


SeekBar

Slider


LinearLayout

StackPanel


EditText

TextBlock


EditText

TextBox


MapView

Map


WebView

WebBrowser



Panorama


Windows Phone Guide for
Android

Application Developers

Chapter 2: User Interface Guidelines for the Windows Phone

16


Android con
trol

Windows Phone

control

Notes


Pivot


TimePicker

Time
P
icker
*

Available on Codeplex

DatePicker

Date
P
icker
*

Available on Codeplex

ExpandableListView



Spinner

ListBox


TableLayout

Grid Layout


ViewFlipper



ZoomControl



TabHost

PivotControl


RatingBar



Toggle button

ToggleSwitch
*


* ToggleSwitch and Datepicker/Timepicker control are part of the
Windows Phone Toolkit

available on Codeplex:

As you can see,
W
indows
P
hone

offers controls that correspond to
many
Android controls.
While the look and feel is different, they provide similar functionality.

New Controls

in Windows Phone

W
indows
P
hone

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



A multi
-
scale image

con
trol

allow
s

the user
to

zoom
in and
pan

a photo.



Panorama control is a multi
-
screen page
spanning

horizontally beyond the width
of the phone.
It allows a large amount of related information to be presented.

The
People

hub is a great example of this contro
l.



The
Pivot control,
shown below,
is useful to manage views and display information
that is logically divided in
to

sections.

Windows Phone Guide for
Android

Application Developers

Chapter 2: User Interface Guidel
ines for the Windows Phone

17





Notifications

Both Android and
W
indows
P
hone

provide

notification service, but notifications play a key
role in
W
indows
P
hone
.
Windows Phone provides number of different means to show
notifications to users


via status bar update, a dialog
,

as a toast
,

or live tile notifications.
It is
t
he tile notifications
that
make
Windows
P
hone
come alive.

Windows Phone sets itself apart
with live tiles that show critical information at a glance.

L
ive

tiles

are used to display non
-
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 replacem
ent in
W
indows
P
hone

to
show critical information.
However, tiles have the ability to provide far more information,
including

photos
, a message and a number
.

The notification service also display
s

toast notifications that provide time sensitive
informati
on, such as an SMS.
T
oast notifications are shown for about 10 seconds

and
the user
may choose to ignore them

or click to launch the app directly
. These are
similar to

Android
toast notification
s
.

The following table shows the Windows Phone notifications t
hat are closest to
various
Android notification mechanisms. While you may replace status bar notifications with Tile
notifications, tile notifications provide much richer functionality.

Additionally, tile notifications
do not require user response.



Windows Phone Guide for
Android

Application Developers

Chapter 2: User Interface Guidelines for the Windows Phone

18


Andro
id

Functionality

Windows Phone

Status bar
notification

Persistent

information that
require
s

user
response.

Tile notifications

Toast notification

Time sensitive

non
-
persistent

data that
the
user may not respond to
.

Toast Notifications



ActionBar

vs. Application
B
ar

In Android
,
the
Actio
nBar identifies the application and

user
location. It also

shows

action
s

available to the user as well as

navigation

such as tabs.

The status bar
on Android
shows the
device status and various notifications.
While
b
oth Status bar and ActionBar are at the top of
the screen
, action bar may

be split and may also be shown

at the bottom of the screen
.


A: Status bar

B: Action bar


The status bar on Windows Phone provides functionality similar to Android status bar.
On
W
indows
P
hone
,
P
age or application menus are supported using a

versatile
application bar

shown at the bottom of the screen on Windows Phone
. 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.
Application bar does not provide navigation nor tabbed
views as provided by the ActionBar.


A: Status bar

B: Title, Navigation

Windows Phone Guide for
Android

Application Developers

Chapter 2: User Interface Guidelines for the Win
dows Phone

19


C: Appbar


Android

Functionality

Windows Phone

Status bar


Device status
and
notifications

Status bar

Action

bar

Navigation, Title,
Buttons for views or
actions

Back button for back
navigation

Page title

ActionBar

Buttons for views or
actions

App bar

Comparing
Windows Phone

and Android Navigation

W
indows
P
hone

app
s

are

a collection of pages. Like Android, the user navigates through
pages

using
controls

such as buttons and links.
Like

Android,
all

W
indows
P
hones have

a

Back

button

on the
m.
On Windows Phone t
he back button

behaves much like the back
button in the browser
allowing
the user to navigate
between pages of an app or even
across
apps. For example, consider that the user clicked a link in the email application to launch the
browser. With
the
Back
button, the user
w
ould return

back

to the email application
.

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 appropriately. For example, you may
decide to pause a game
by
using the
Back

button

on the Phone
.

The other two h
ardware buttons on the
W
indows

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

The
Home button takes the user to the Windows phone main page
,

much
like Android.
Unlike Android where search button provides context sensitive search, t
he
W
indows
P
hone

s
earch button
performs
a
web

search
using Bing
.




Windows Phone Guide for
Android

Application Developers

Chapter 2: User Interface Guidelines for the Windows Phone

20


Windows Phone
Frame and Page Structure

Each
W
indows
P
hone

application has a single frame
.

I
t includes areas for:



A page where application content is rendered. This is the content where
controls

or
graphic
s are rendered.



A reserved space for the s
tatus bar

and application bar. It also exposes certain
properties, such as orientation
,

to the application.



S
tatus Bar

and Application Bar

On
W
indows
P
hone
, the
status bar

includes indicators for
a variety of

system
-
level status
information

items
such as connection status
. The application bar includes the area for the
most common application menus, which may include various data views or tasks.
The
application can manipulate the status bar to change its visibil
ity, opacity or the color. The
application can use it to show progress.




Windows Phone Guide for
Android

Application Developers

Chapter 2: User Interface Guidelines for the Windows Phone

21


Page Structure of
a
Windows Phone

Application

The following diagram shows the structure of a typical
W
indows
P
hone

data
-
bound
application, which resembles

the structure of

a navigation
-
based Android application.



When the user first starts the application, he or she is presented with a splash screen,
designed to welcome the user, as well as to create the perception of

a

fast response. Splash
screens are usually an image
file of the size of the display.

Usually the application starts with the home page, the main navigation page, with links for
search, and other page
controls
. 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
,
will
have the content of interest
,

for
example
,

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

The user can click on one of the team links to visit the team details page (“widget details
page”) which can provide multiple views. The page may employ a pivot control or
a
panorama to display different views such as the team summary and the list of all p
layers (“list
of gadgets”) from that team. Selecting one of the baseball players will take the user to the
page with player statistics (“Gadget Details page”). Such a page may use controls such as
textblocks, multi
-
scale images, or other multimedia using a

MediaElement control.



Users may also use the search
page

to search and directly access the team page
(“widget details”) or the player page (“gadget details”)

Application Templates

Windows Phone Guide for
Android

Application Developers

Chapter 2: User Interface Guidelines for the Windows Phone

22


Visual Studio Express 2012 for Windows

Phone
provides
a
variety of

templat
es
for

developing

W
indows
P
hone
8

application
s.

Visual Studio templates create
the
appropriate structure
of
the application
automatically.



Functionality

Android Activity Template

Visual Studio Template

Information drilldown
applications

Master/Detail
Flow, Blank
Activity

Windows Phone
Data
-
bound
App,
Pivot App, Panorama App

Utility applications.
For example, Bubble
Level

Fullscreen Activity, Blank
Activity

Windows Phone

App

Games

Fullscreen Activity, Blank
Activity

Windows Phone Direct3D App (Visual
C++)

app

Flexible template to
design any
application

Blank Activity

Windows Phone

App


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.

Summary

In this chapter we looked at t
he similarities between the application
design goals of the
Android and
W
indows
P
hone
8

platform
s
. When you plan your
W
indows
P
hone
8

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

Revisit the application interface design to make sure you are taking advantage of the
W
indows
P
hone

controls

and

Windows Phone

design and

interface guidelines.
You will find
that the
W
indows
P
hone
8

offer
s

a large library of controls and gestures that have

close
counterparts on Android.

This chapter also showed you the
use of innovative controls like
P
anorama
and explore
d

the use of live tiles
in

build
ing

an engaging
W
indows
P
hone
8

experience.

Related Resources

To
learn more
,
visit
:



Windows Phone

User Interface Guidelines




Windows Phone SDK Tools
Windows Phone toolkit

on Code
p
lex


Other Resources you may find useful:

Windows Phone Guide for
Android

Application Developers

Chapter 2: User Interface Guidelines for the Windows Phone

23




App page model for Windows Phone



In
-
app navigations for Windows Phone


Windows Phone Guide for
Android

Application Developers

Chapter 3: An introduction to Windows Phone Developer and Designer Tools

24


Chapter 3:
An introduction to Windows
Phone

Developer and Designer Tools

Along with
the release of the
Windows Phone

O
S

, Microsoft

published

the user
-
friendly,
high productivity Visual Studio
Development environment
for
W
indows
P
hone
8

development
.

Developers who have used Visual Studio will find

themselves in

a familiar
environment.

A
n

Android application developer
wh
o is
familiar with Eclipse

will find it easy to

quickly migrate to
W
indows
P
hone
8

developer tools and
quickly
become productive
.

A Comparison of Android and
Windows Phone

Tools

Visual Studio Express

2012

for Windows Phone is a full
-
featured Integrated Development
Environment (IDE). Visual Studio 201
2

facilitates designing, developing, and debugging of
Windows Phone

applications.
O
ther
W
indows
P
hone
8


tools that
can
help you in the
development cycle of th
e Windows Phone
8
application are:



Blend

for Visual Studio



Windows Phone Emulator

Compared to Android developer tools, t
he
W
indows
P
hone
8

developer tools offer
richer

functionality. The following table gives an overview of the functionality of each of the
se tools.
The table also indicates the equivalent tools
that
you
would
use for Android application
development.


Functionality

Audience

Android tools

Windows Phone

tools

Application
development
(coding)

Programmers

Eclipse

Visual Studio Express
2012

for
Windows Phone


Game
development
(coding)

Programmers

Eclipse

Visual Studio Express
2012

for
Windows Phone


Testing /
Emulation

Testers

Android Emulator

Windows Phone Emulator in
Visual Studio Express

2012

UI design

UI designers
and
programmers

Defined in
XML
(Some
Visual Studio Express

2012

and Blend
for
Visual Studio

Windows Phone Guide for
Android

Application Developers

Chapter 3: An introduction to Windows Phone Developer
and Designer Tools

25



















As you plan to develop applications for
W
indows
P
hone
8
,
y
ou can
continue to leverage the
skillsets in your team.
Your development

team structure and overall development process
can remain the same
. The
W
indows
P
hone
8

toolset ensures that the entire team
of
designers, developers, and testers familiar with Android development tools will find it easy to
migrate to the
W
indows
P
hone 8

toolset
.

Visual Studio is not only a powerful IDE but it can
also
be extended
in a variety of ways. It allows the ability to
a
dd
macros,
options, menus and toolbar commands,
visualizers

and other tools
.
In addition, Visual Studio
Primary UI
design: Colors,
gradients, and
animation

UI designers

WYSIWYG support
via ADT plug
-
in)


Pixel accurate WYSIWYG
layout using
Blend
for Visual
Studio

Windows Phone Guide for
Android

Application Developers

Chapter 3: An introduction to Windows Phone Developer and Designer Tools

26


supports NuGet, a package manager, to add, remove
and manage libraries and tools in Visual Studio and
the
.NET
F
ramework.
Windows Phone tools
l

Windows P
hone

development tools facilitate a close collaboration between designers and
developers through the use of Blend and Visual Studio.

The UI for a Windows Phone app is
created using XAML, a declarative XML
-
based language. Both Blend and Visual Studio can b
e
used to edit the XAML file, allowing designers and developers to use tools targeted toward
their discipline

Tool for coding

The file structure of a

Windows Phone

app

Like Eclipse, Visual Studio Express

2012

for
W
indows
P
hone

is a
full
-
featured

IDE.
It

allows
you

to
organize

the

source

files

s
.


as a collection of projects
,

i.e.
separate
functional unit
s
.

Th
e organization of the codebase

into projects

makes it easy to

manage manage the work
among team members. Visual Studio integrates a compiler and a
debugger
, both of which
can be invoked

either
interactively
or

via

the command line.

In this chapter we will walk through the creation of a simple Shopping List app in order to
illustrate the Windows Phone development tools and steps.
Let us

create a sampl
e
application

1.

Start Visual Studio Express for
W
indows Phone

from

the Windows

Start

menu.

2.

In Visual Studio Express, click
File
.

Windows Phone Guide for
Android

Application Developers

Chapter 3: An
introduction to Windows Phone Developer and Designer Tools

27


3.

Click
New Project
.

4.

In the
New Project
dialog

expand
Installed

-
>
Templates

-
>
Visual C#

-
> Windows
Phone

S
elect
Windows Phone
App
.

5.

In the
Name
text box, enter

ShoppingList


as the name of the project, and click
OK
.
Visual
Studio Express

will prompt you select the OS you want to target. Select “Windows
Phone OS 8.0” and Visual Studio
will
create

the new project

as shown below
.

Th
e
Solution Explorer

pane

displays the solution that you
have
created.
This solution has only
one project, also named ShoppingList.

The project contains the sources

code and other files
.



Note
:

Visual Studio Express
2012
for
W
indows
P
hone

does not provide integration with
source control systems. The Visual Studio Professional edition provides features such as
integration with various source control systems like Subversion.
Alternatively,
organizations

can use the Visual Studio Team System,

an edition designed particularly for greater
communication and collaboration among
software development teams, for developing your
Windows Phone

application


Tools

for designing

The
W
indows
P
hone
8

developer tools
et

include
s two
UI
design tools
:



Visual S
tudio UI Designer



Blend

for Visual Studio

Windows Phone Guide for
Android

Application Developers

Chapter 3: An introduction to Windows Phone Developer and Designer Tools

28


C
ompared to Eclipse ADT UI plugin,
Visual Studio UI design tool is
much
simpler and
richer
.
Android application developers

migrating to
W
indows
P
hone
8

developer tools will find
the
task of designing an application

UI much easier
.

The main page for our ShoppingList solution, MainPage.xaml, is already opened in the
Visual
Studio

UI designer tool for editing (shown above).
Let us change the title of the application,
as well as the title of the current page. Right cli
ck on the title, “MY APPLICATION” and select
Properties
. In the properties window, select
Text

and type “SHOPPING LIST.” Similarly, change
the title of the page by typing “my list” in the
Text

property of the title.

Let us design the interface of the main page of the application.



1.

Open the
Toolbox

window

(
View
-
> Other Windows
-
> Toolbox
) if not already open
,

drag a
T
extBlock

and drop it on the page

in the designer window
. Position it so that it is
at the top left.
C
lick
and select the TextBlock
and update its Text property to “Item:”

in the
Properties

window.


Windows Phone Guide for
Android

Application Developers

Chapter 3: An introduction to Windows Phone Developer and Designer Tools

29


2.

Drag a
TextBox

from the
toolbox
and place it underneath the text

block

created in the
previous step
.


3.

Update the

Textbox’s
Text

property to delete any existing entry.

4.

In the
Properties

window for the textbox
, enter

txtItem


in the name field
to
set

the
Name

of the text

box to
txtItem
.

The properties pane in the VS UI is in sync with the
XAML code in the code editor window. You can set properties for controls manually in
XAML or set them in the UI using properties panel.

5.

Resize the
t
ext box by dragging its lower right corner so that it
s width is 300.

6.

Drag a
Button

to the right of the
TextBox
.

7.

Change the button’s
Content
property to

Add

, and its
Name

to

btnAdd

.

8.

Resize the button so that its width is 140.

9.

Drag another
TextBox

and place it underneath the
txtItem

textbox.

10.
Resize the ne
w
TextBox

so that it covers the rest of the phone screen.

11.
Update the
Name

of the new TextBox to

txtList

.

12.
Update the
Text
property of the new TextBox to

Nothing here yet!


At this point, y
our application

should

look
something like this
:




13.
Click
F5,

or
Debug

and
Start Debugging
,
to

compile the application and launch it.


Windows Phone Guide for
Android

Application Developers

Chapter 3: An introduction to Windows Phone Developer and Designer Tools

30


This will start the
W
indows
P
hone
8

emulator, deploy the ShoppingList application and run it.
You can click on
Add,
but nothing will happen as we have not written any logic yet.



Blend for
Visual Studio

Blend for
Visual Studio

is a full featured visual UI design tool created for designers. There is
no exact counterpart to this in t
he
Android

development toolset. Similar to
the
Visual Studio

d
esign tool, Blend also allows drag and drop to design the UI. The tool, shown below, allows
pixel accurate layout of controls. They can easily create and use color palettes and gradients,
as wel
l as special effects such as reflections and shadows. The tool can import Photoshop
files,
which
make
s

it easy to bring your
Android

application resources to your Windows
Phone application. Designers can also use the tool to define application behavior, a
s well as
certain animations, without any programming.




Windows Phone Guide for
Android

Application Developers

Chapter 3: An introduction to Windows Phone Developer and Designer Tools

31




You will need to use Visual Studio to code your application logic, but you can create the UI
in VS UI or Blend.
Both tools include the same control set

that provides accurate fidelity to
their run time visual representation, making it easy to visualize the application.




Windows Phone Guide for
Android

Application Developers

Chapter 3: An introduction to Windows Phon
e Developer and Designer Tools

32


Editing Code

Visual Studio has a simple to use, full
-
featured, configurable source editor. The editor has
various features that are fa
miliar to Eclipse users.
These include flexible search, rich editing,
code formatting, and the ability to outline/hide code.

Let us now proceed to
add some logic to our application.

:

1.

S
top the running application

by

click
ing

Debug
,
then click
Stop Debuggin
g
.

Leave the
emulator running

so that it doesn’t need to start up again the next time you debug.
This speeds up the subsequent

debug session
s
.

2.

Double click
the “
Add


button
which will open

MainPage.xaml.cs

with a method
btnAdd
_click

in the MainPage class.



3.

To add logic
for
add
ing

items to the shopping list, edit the
btnAdd_click

method
.

Enter the following code
:

string tStr = txtItem.Text;


Unlike Android where objects must be dereferenced in Java using getViewById, in XAML,
objects created in XAML are a
ccessible in code
-
behind C# or VB files. T
he
specified

x:Name

or Name attribute of a XAML object
becomes the name of a field that is
created in the underlying code when XAML is processed.

This allows us to access the
textbox using txtItem, its x:Name attri
bute.

IntelliSense in Visual Studio

Windows Phone Guide for
Android

Application Developers

Chapter 3: An introduction to Windows Phone Developer and Designer Tools

33


When you enter
"
t
"

for
"
txtItem
"
, Visual Studio displays the auto
-
completion dialog box. The
Visual Studio equivalent for Eclipse
auto
-
completion
content assistant
is called
Intellisense
.



4.

Enter the following code in the
btnAdd_click

method:


if

(!
string
.IsNullOrEmpty(tStr))

When you type
"
s
tring
"
, Visual Studio displays the auto
-
completion dialog box. When you
type
"
Is
"
, Visual Studio displays the class methods of the String class
.

As you see below, it
provides a synopsis of the IsNullOrEmpty method.




IntelliSense
has a rich feature set. It

uses history, code context, and .NET reflection for
intelligent auto
-
completion. IntelliSense can suggest and complete variable names,
param
eters, classes, and method names. IntelliSense can also generate appropriate code
where needed, as shown in the
code

below:



To complete the event hookup, it will also generate an empty stub for the event handler,
in
this case
, the button1_click method.


Windows Phone Guide for
Android

Application Developers

Ch
apter 3: An introduction to Windows Phone Developer and Designer Tools

34



Windows Phone Guide for
Android

Application Developers

Chapter 3: An introduction to Windows Phone Developer and Designer Tools

35


You will find the generated event handler in the C# code


void

button1_Click(
object

sender,
RoutedEventArgs

e)


{


throw

new

NotImplementedException
();


}

The default code would throw an exception if you forgot to
implement necessary logic for
this handler.
Subsequently, we will implement the logic to handle the button click.

Code Snippets in Visual Studio

Visual Studio provides another very useful feature called Code Sni
ppets,
similar

to code
templates in Eclipse
,

allowing
you to insert code fragments with a few clicks. Visual Studio
contains a large number of snippets

and developers can create their own library of snippets
.
They can also be indexed and searched using user defined terms.


Type
ctrl+k ctrl+x

to brin
g up the
Insert Snippet

prompt. Select Visual C#, followed by “i” to
select a code snippet for “if statement”, which will insert an
if
-
statement in the code.




The inserted snippet identifies the parts the user needs to complete:


Type the remaining
code, so that the body of the method is as follows:


string

tStr = txtItem.Text;


if

(!
string
.IsNullOrEmpty(tStr))


{


if

(!initialized)


{


txtList.Text =
""
;



}


txtList.Text += txtItem.Text +
"
\
n"
;


txtItem.Text =
""
;


}

Windows Phone Guide for
Android

Application Developers

Chapter 3: An introduction to Windows Phone Developer and Designer Tools

36




Make sure that

you declare and initialize a variable called initialized as follows.


public

partial

class

MainPage

:
PhoneApplicationPage


{


bool

initialized =
false
;

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

The
Visual Studio editor is
highly
customizable.
Developers can easily

define various
keyboard shortcuts or create
their

own macros. Macros help you to automate repetitive
actions by combining a series of commands and instructions.

The following table provides various functions and corresponding keyboard shortcuts in both
E
clipse and Visual Studio.


Tool

Run

Ctrl+f11

Ctrl+f5

Debug

f11

f5

Toggle breakpoint

Ctrl+shift+b

f9

Switch editor tabs

Ctrl+f6

Ctrl+f6

Source code management

Comment line/block

Ctrl+/

Ctrl+k Ctrl+c

Uncomment line/block

Ctrl+/

Ctrl+k
Ctrl+u

Rename

Ctrl+shift+r

f2

Refactor

Ctrl+shift+t

Ctrl+R+..

Autocompletion

Ctrl+space

Automatic

Organize imports

Ctrl+Shift+o

Ctrl+.

Format source code

Ctrl+shift+f

Ctrl+e, f

Search

Find

Ctrl+f

Ctrl+f

Windows Phone Guide for
Android

Application Developers

Chapter 3: An introduction to Windows Phone Developer and Designer Tools

37


Replace

Ctrl+f

Ctrl+h

Find next

Ctrl+k

f3

Find previous

Ctrl+shift+k

Shift+f3

Find in files

Ctrl+h

Ctrl+shift+f

Replace in files

Ctrl+h

Ctrl+shift+h

Navigation

Goto Line

Ctrl+l

Ctrl+g

Goto Definition

Ctrl+click

F12


Eclipse users can

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



Developers

can change th
e way various windows are laid out to suit their need
.
They

can
also
change the way in which various windows are dock
ed within
the
Visual Studio Shell.

Building Application

Similar to Eclipse,
Visual Studio Express

2012

for
W
indows
P
hone

allows you to build
a

Visual
Studio solution on demand.
Further, each project that is part of the solution can be built
separately.

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

which can be
compared

to Ant
or

Nant.
Builds can be invoked interactively or via a command line for
batch processing. This system is flexible and allows you to build a specific target

either as a
debug build or as a release build.


Windows Phone Guide for
Android

Application Developers

Chapter 3: An introduction to Windows Phone Developer and Designer Tools

38




Tool for Testing

Windows Phone

8
developer tools include an emulator that can be used effectively for
testing applications. It provides features that are comparable to the
Android

simulator
included in th
e


developer tools.

The
Windows Phone 8

emulator provides a virtualized environment in which you can deploy,
debug and test applications. The Windows Phone Emulator is designed to provide
comparable performance to an actual device and meets the peripheral specifications
required for applicati
on development.

As you can see below, the emulator allows emulation
of accelerometer as well as device location.

It can be invoked from Visual Studio to load an
application package [.xap] within the emulator.


Debugging

Windows Phone Guide for
Android

Application Developers

Chapter 3: An introduction to Windows Phone Developer and Designer Tools

39


Visual Studio Express
2012 for Win
dows
Phone includes a very powerful symbolic debugger
that can be used with the
Windows Phone 8

emulator or with a remote device. Once the
application breaks into the debugger, the developer can view the variables in the application
and control the executi
on.

Let us look at the debugger in action. Press F5 to launch the application again. Type
“napkins” in the textbox and click
Add

Napkins is added to our list.


Now add, “milk” but find that

n
apkins”
gets removed

-

not something we expected.

In Visual Studio, click in the light blue area to the left of the “string tStr = txtItem.Text;” line in
the code window. This will insert a breakpoint at that line.



Enter “milk” and click
Add
.
When the application breaks into the debugger,

use “Step o
ver” or
(f10)

to enter the outer
if

block. H
over over
initialized variable

in the code
as shown
and
Visual Studio

shows its value as
false

something we did not expect Step through to find that
the execution enters the inner
if

and resets txtList.Text t
o blank again


therein lies our bug.
After adding first item to the list, we should have set the value of initialized to true.

Windows Phone Guide for
Android

Application Developers

Chapter 3: An introduction to Windows Phone Developer and Designer T
ools

40



Fix the
bug
by adding,
initialized =
true
;

in the inner if statement

and restart the
application.


You

can
inspect

any
variable, its type, its fields and properties

while debugging
.
For
example, hover over txtList and click + to inspect various properties of txtList.




Windows Phone Guide for
Android

Application Developers

Chapter 3: An introduction to Windows Phone Developer and Designer Tools

41





You can set a “watch” on certain variables to inspect them continuously. Right click txtList,
fol
lowed by
Add Watch.
The watch window will show the variable txtList

througout
.



I
n the debugger, the developer can use the
Visual Studio

‘immediate mode’ where one can
write managed code instructions to modify or view the variables or execute some code

to
help with debugging.




Update the code and re
-
launch the application. Test it by adding couple of items to the
shopping list.

Windows Phone Guide for
Android

Application Developers

Chapter 3: An introduction t
o Windows Phone Developer and Designer Tools

42





Similar to Java applications using Eclipse with ADT,

the
W
indows
P
hone
8

application
debugging is done entirely at the application
source code
level.
Overall, you will find that,
with the power of
Visual Studio and C#
, debugging a
Windows Phone 8

application is very
easy

whether you are running the application in the emulator
or an actual device
.

In addition to the above debug facilities, the .NET
F
ramework includes two specific classes,
Debug and Trace, that make it easy to write run
-
time debug messages to the output window.
C# also supports an assert statement, which is evalu
ated at run time. If the statement
evaluates to true, nothing happens, but if the statement returns false, the program breaks
into a debugger.




Windows Phone Guide for
Android

Application Developers

Chapter 3: An introduction to Windows Phone Developer and Designer Tools

43


Summary

In this chapter we looked at
t
he
Windows Phone

Developer Toolset
. The
t
ool

set

includes rich
tools that are designed to support every step in the entire application development lifecycle.
The design, development, and testing tools are
amenable

to the existing Android team roles
and processes. The tight integration between the
W
indow
s
P
hone
8

development

tools help
s

you to streamline the design, develop the workflow, and test the workflow.
These tools
provide end
-
to
-
end functionality and are highly customizable, with the power to make your
team quickly productive
.

Windows Phone Guide for
Android

Application Developers

Chapter 4: An Introduction to C# programming for Java developers

44


Chapter 4:
An Introd
uction to
C#
programming

for Java developers


In the previous chapter, we looked at the user interface guidelines for
W
indows
P
hone
8

applications. We will now dive deeper into what it takes to implement a
W
indows
P
hone
8

application.

In this chapter, we
will look at the various
C#

features that map to the most common
Java

features. We will provide code snippets which will ease
the transition

into C# code. We will
point to the key C# fea
tures that help you write safe code and enhance productivity.



Windows Phone Guide for
Android

Application Developers

Chapter 4: An Introduction to C# programming for Java developers

45


Managed Programming

Windows Phone

8
supports
managed programming

in C# or VB

as well as C++
.

In this
chapter, we will only look at C# and compare it with Java.
Before we jump into the details of
C#, let us briefly review
the .NET programming environment
.


Figure
2

Managed Application Execution Flow

The C# compiler (and
similarly, the VB compiler) compiles the C# (or VB) code in an
intermediate language (IL) byte code and metadata. The Common Language Runtime (CLR)
executes the byte

code. C# uses metadata to manage type safety, exception handling, and

run
-
time validations

such as

checking

array bounds

.
The
CLR also manages memory and
performs garbage collection.
This is similar to
Android

where

the Java code is compiled in
to

a
.
dex

file

which is then executed by the Dalvik VM
.



Windows Phone Guide for
Android

Application Developers

Chapter 4: An Introduction to C# programming for Java developers

46


A Comparison between C# and Java
Features

There are many

many similarities between Java and C#.
They both



Are
object
-
oriented and
based on

C and C++.




support single inheritance

and

allow classes to implement multiple interfaces.



have

objects a
s

reference types.



create

instance
s

of a class are created on the heap and do not
support
explicit delete.



Implement automatic garbage collection.

Instances that are no longer accessible, are
automatically garbage collected.



type safe languages

and require
type
declaration for all

variabl
e
s
. As you will see
below, recently C# has started supporting implicitly typed variables where the
compiler infers the type automatically.



support

built
-
in threading and synchronization allowing developers to write
concurrent programs.

However, there are

also

many
subtle and important differences

between the two as we will
see below.

Class Declaration

Like Java, C# does not separate
class definition

from class implementation. The compiler
derives the metadata about the class from
the
class implementation. As in Java
,

we can
define multiple

class definition
s

in
the
sam
e file.

However, unlike Java, more than one class in
a file can be public and the name of the file may be different from the public class in the file.

In the example, the public signature of the class
Person

consists of the
age

property

and the
constructo
r. The rest of the class implementation is opaque.


using

System;

// This is similar to java import except


//.NET directives are for the whole namespace


namespace

FirstApplication

// scope for classes. Similar to package in Java


// Unlike C#, namespace does not force directory

{


class

Person


// Like Java, only uses class defined


// impleme
ntation not declaration.


{


private

DateTime

birthDate;
// a private field accessible
within

this class


private

int

ageOn(
DateTime

date)
// a private method

accessible in class


{


TimeSpan

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


return

span.Days;

//Unlike Java, C# supports properties


}

Windows Phone Guide for
Android

Application Developers

Chapter 4: An Introduction to C# programming for Java developers

47




}


public

int

age
//Unlike Java
, C# supports properties
.


{


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


{


return

this
.ageOn(DateTime.Now);


}


}


public

Person(
DateTime

dob)
// instance constructor

similar to Jav
a


{








birthDate = dob;


}


}



class

Program


//Unlike
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



}


}

}


Ins
tead 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
,
which

avoid
s

name conflict

and organize
s

the code. You can
access
a
class in
an
other namespace

by referring to
its
fully
-
qualified name.
See the reference
to System.Console.WriteLine in the example above, where console is in the System