Below are two applications that do exactly the same thing, but one was a whole lot easier to create. Can you guess which one? As an end-user it’s impossible to tell but in this article we examine how similar and yet different the development experience is for each of the apps. One of the apps below (on the right) is coded inAndroid 2.2 (aka Froyo) and the other using Windows Phone 7 Beta SDK. I tried to keep both projects as simple as possible so that people can understand the fundamentals over the design.
Download Sample Project Code Files
To get started with Windows Phone 7: http://developer.windowsphone.com, download the necessary software and Windows Phone SDK from the link listed. These tools will be used to run the examples; namely Visual Studio 2010, Windows Phone 7 Beta SDK, and Microsoft Expression Blend (optional).
The sample provided is just a small application which can convert between Fahrenheit, Celsius, and Kelvin.
I want to begin by addressing the similarities between the Android and Windows Phone 7 platforms. Both platforms have adopted the approach to separate design from logic. In Android, developers are used to coding “Layouts” and in Windows Phone 7 the equivalent is called a “Page”. Both of these layout methodologies involve creating an XML based layout. WP7 uses a mutated version of XML called XAML (pronounced “Zamel”). In both the Layouts and the Pages of each platform there is code behind that connects to your layout.
main.xml (Android)
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout android:id="@+id/widget0"
android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
</AbsoluteLayout>
MainPage.xaml (WP7)
<phone:PhoneApplicationPage
x:Class="TemperatureConverterSample.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
shell:SystemTray.IsVisible="True">
<!--LayoutRoot contains the root grid where all other page content is placed-->
<Grid x:Name="LayoutRoot" Background="Transparent">
</Grid>
</phone:PhoneApplicationPage>
The equivalent of an Android “Activity” would be best described by the C# class which is connected to your XAML page and extends Microsoft.Phone.Controls.PhoneApplicationPage.
Android Activity
public class MainActivity extends Activity {
// Code behind goes here…
}
WP7 Page
public partial class MainPage : PhoneApplicationPage
{
// Code behind goes here…
}
Okay so its clear that there are similarities in terms of the core fundamentals of each platform, not to mention Java and C# are like brothers from another mother.
Phone Controls
The Windows Phone 7 equivalent to Android Widgets are called Controls and are a part of the System.Windows.Controls namespace. These “UIElements” are used in much of the same way that Widgets are. For instance the “Convert” button used in the apps is defined as such in Android and WP7 respectively:
Android
<Button
android:id="@+id/calculateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Convert"
android:layout_x="106px"
android:layout_y="292px"
>
</Button>
WP7
<Button Content="Convert" Height="83" HorizontalAlignment="Left" Margin="24,487,0,0" Name="calculateButton" VerticalAlignment="Top" Width="400" Click="calculateButton_Click" />
Listeners vs Events
To handle a Button click in Windows Phone 7 we would use a Click Event rather than an Android Event Listener.
Android Click Listener
this.closeButton = (Button)this.findViewById(R.id.calculateButton);
this.closeButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
}
}
WP7 Button Click Event
Xaml Event name specified
<Button Content="Convert" Height="83" HorizontalAlignment="Left" Margin="24,487,0,0" Name="calculateButton" VerticalAlignment="Top" Width="400" Click="calculateButton_Click" />
C# Event Method Definition
private void calculateButton_Click(object sender, RoutedEventArgs e) {
// Code for button
}
The Layout of your Apps
Similar to Androids Layout Views, Windows Phone 7 supplies developers a set of objects to layout Controls. There are Grids, StackPanels, Canvas and so on which have various methodologies of displaying UIElements. You can write your own as well. The reference for all of these such layouts can be found on the Microsoft MSDN reference pages.
The equivalent for a LinearLayout in Android would be the StackPanel in WP7.
Differences Between the Platforms
So far we have yet to discuss the differences between the platforms. The main difference between Android and WP7 is in the development tools. In terms of coding, Android may be built on Java which is an open source platform with a ton of available code but C# is a very strong programming language with many nice features of its own. For instance, data binding to Controls in Silverlight is a really nice standard feature. After coding your app you can style it up in Microsoft Expression Blend 4. User interface designs can be “templated” which provides a CSS stylesheet type logic for programming your user interfaces on the phone.
Behaviors in Microsoft Expression Blend 4 allow people to build games use physics like a physics engine without even doing any code. Any Control in Silverlight can have a custom style. For instance, you can have a button which fades out or changes to all the colors of the rainbow when clicked. Whatever your imagination can think up is possible in an easy and highly customizable fashion.
Conclusion
The Android Platform and Windows Phone 7 share a lot of commonalities. It will take a while to get used to migrating from Android + Eclipse to Windows Phone 7 + Microsoft Visual Studio + Expression Blend but in the end it is a much smoother development experience. The tools which Microsoft provide add a wealth of ease to the lives of us developers.
Source: The Dirty Developer