Windows Phone 7 supports VB.Net and F#

JustinAngel

Hi Folks,

In this article we’ll review Windows Phone 7 support for the Visual Basic .Net and F# programming languages. Our goal is to demonstrate that Windows Phone 7 can indeed support F# and VisualBasic.Net code libraries and user controls.

On the official Silverlight WP7 forums we can find many questions asking about WP7 VB.net support with answers like:

Q: “Can I develop applications with VB.NET for Windows Phone 7?”

A: “No, You can't.”

TechRepublic tech blog is quoted in saying:

“The only language that will work up front is C#; VB.NET developers need not apply.” TechRepublic

And Canadian Heritage and official Languages Minister, the Honourable MP James Moore said:

“My dog wears a conservative party raincoat”

All of the aforementioned statements, with the exception of that last one, are all patently false. And that last statement is just plain embarrassing. Windows Phone 7 does indeed support Visual Basic .Net and F# and has done so since day #1.

Windows Phone 7 Applications are C#

The one statement that can made and is factually true is that during the WP7 Beta the only language supported for the WP7 application project itself is C#. Which means absolutely nothing, since a WP7 application can just refer to a F# Silverlight 3 library or VB.Net Silverlight 3 library.

Using F# in Windows Phone 7

Let’s go over how to setup a simple “hello world” WP7 app with F#.

1) Create a new C# Windows Phone 7 application.

2) Add a F# Silverlight 3 project to the solution.

3) Open up the module1.fs file and write some F# code. For this basic sample we’ll make do with a simple non-UI sample of returning “Hello world”.

module WP7FSharpLibrary

type myClass = class

new () as this = {}

member s.myMethod() =

"OMG! Windows Phone 7 is running F# code!"

end

3) Add a reference from your WP7 app to the new F# library and build the solution.

6) If we try and run the project at this state, we’ll see the following build warning:

Warning 1 The primary reference "d:\visual studio 2010\Projects\WP7.Languages\WP7.VisualBasicNet\Bin\Debug\WP7.VisualBasicNet.dll" could not be resolved because it has an indirect dependency on the framework assembly "Microsoft.VisualBasic, Version=2.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" which could not be resolved in the currently targeted framework. "Silverlight,Version=v4.0,Profile=WindowsPhone". To resolve this problem, either remove the reference "d:\visual studio 2010\Projects\WP7.Languages\WP7.VisualBasicNet\Bin\Debug\WP7.VisualBasicNet.dll" or retarget your application to a framework version which contains "Microsoft.VisualBasic, Version=2.0.5.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35". WP7.Languages

Make sure to add a reference from the WP7 app to Microsoft.VisualBasic.dll, and that it is marked as “Copy Local = False”. On my dev box the DLL is found @ c:\Program Files (x86)\Microsoft Silverlight\4.0.50524.0\Microsoft.VisualBasic.dll

Right click on the “Microsoft.VisualBasic” reference, choose “properties” and set “Copy Local” to false”.

After that change we can run our project and the Visual Basic .Net Silverlight 3 UserControl just shows up in our Windows Phone 7 project.

Obviously, after adding the Windows Phone 7 dlls to our VB.Net project we would be able to do anything a C# project can: GPS Location, Push Notifications, Accelerometer, Navigation, etc.

Fin

Windows Phone 7 supports all 3 main .Net 4.0 programming languages: C#, VisualBasic.Net and F#. While C# alone is supported during the WP7 beta as the root WP7 application, it’s a small hindrance which we can easily be overcome.

In the aftermath of this blog post I expect both F# developers and VB.Net developers to produce some kick-ass WP7 apps. Yeah, I’m looking at you there.

Feedback

Feel free to sound off in the comments. What are your thoughts on this topic? Is this an unbearable burden to follow this 5 simple steps? Is it exactly what you were looking for? Are there any caveats you know off and would like to share with everyone?