Windows Phone 7 And IPhone - Sharing Code

Anthony Marshall
Anthony Marshall
13 Sept 2010
blog post featured image

A Match Made In C#


I got very exited when i began learning Objective-c, the prospect of being able to write native IPhone apps was very attractive to me. I don’t know why i got exited but like a large proportion of developers (based on the 200k + apps in the store) i did and i set out to conquer the beast.

I am now two commercial apps in using Objective-c and XCode and i am starting to feel like there must be a better way of going around this, With Windows Phone 7 on the horizon and a large user base of the Android platform out in the wild i want to get as close to write once and reuse everywhere as is physically possible. But how is this possible when each platform has its own Language, Framework and IDE?

Enter Mono, this excellent project which allows you to write, build and deploy .net code to non Windows platforms which is spearheaded by Miguel de Icaza means you can now write a large proportion of your code once and it can be reused on the 3 mobile platforms.

To give you an example here is a walkthrough of how you may go about doing this:

note: To follow the below guide you will need a Windows machine with Visual Studio 2010 and the Windows Phone 7 Beta tools installed, also a Mac with the IPhone SDK, MonoDevelop and MonoTouch installed.


Open Visual Studio 2010 and create a new Windows Phone 7 project, I have called the project “CodeSharing.WP7” and the solution “CodeSharing”.  I have created this solution in my drop box folder to share with the Mac i am going to be using but when creating a real application this would be done using a version control system.

New WP7 Project

Add a Button called “btnSayHello” and a TextBlock called “txtHello” to the MainPage.xaml.


Right click on the solution and add a new project, select Silverlight Class Library and make sure you have .Net 3.5 selected. When given the Option choose Silverlight 3. I have called this project “CodeSharing.Shared”.


Add a class to the new project called “HelloMobiles” and add the code to the class which is below:


Now in your Windows Phone 7 project add a reference to the new Class Library then add a click handler to the btnSayHello by double clicking it in the designer. You can then add the code below:


If you now hit F5 and click the button you will see that it has pulled the text through to the TextBlock:


So nothing special so far, we have created a very basic Windows Phone 7 Hello World application, so onto the good bit.

If we now open up the solution in MonoDevelop on the Mac you will see the following:

Screen shot 2010-09-13 at 10.37.20

The solution has opened fine but the Windows Phone 7 project has not loaded, This is not a problem as we are going to be adding an IPhone project. Right click on the solution and add a new project. Under the C# menu select IPhone Window-based project.

Screen shot 2010-09-13 at 10.44.33

Once the IPhone project is created add a new IPhone View with Controller called “MainViewController” and then double click on the MainViewController.xib to edit the view in Interface Builder. I am not going to go into here how you create the UI but there is a great tutorial here. You will end up with a view looking like below and outlets for the UIButton and the UITextField specified on your view controller.

Add a reference to the compiled dll of the CodeSharing.Shared project (you cannot add a reference to the project itself as MonoDevelop says that the project is incompatible) and add the following code to the ViewDidLoad method of the MainViewController code behind.


Screen shot 2010-09-13 at 11.17.57

And make the FinshedLaunching method in the Main class look like the following:

Screen shot 2010-09-13 at 11.18.35

Set the IPhone project as the start up project and you are good to go.


This method is definitely not the best way going about this but it does give you an idea on how we can reuse code between many different mobile platforms using C# and Visual Studio 2010. I am particularly looking forward to getting my hands on MonoDroid so i can start writing Android apps too.


Download the sample code here.

Close chatbot
Open chatbot
Open chatbot