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.

You can get the sample project we’ve build in this article @ http://JustinAngel.net/storage/WP7.Languages.zip

Why?! Why has Microsoft forsaken us?!

Reading about VB.Net & F# Windows Phone 7 support online can be quite misleading.

On the official MSDN WP7 forums we can find the following quote:

“Windows Phone 7 currently is supported only for C#” - Mark Chamberlain

During Windows Phone 7 jumpstart webcasts an audience member asked about VB.Net support and was answered:

“I'm afraid Visual Basic is not supported for WP7 dev” - Andy Wigley

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.

4) in the C# WP7 application, initialize your F# class and invoke the proper members.



// Constructor public MainPage() { InitializeComponent(); var FSharpClass = new WP7FSharpLibrary.myClass(); ContentGrid.Children.Add(new TextBlock() { Text = FSharpClass.myMethod()}); }

When we run our app we can clearly see that F# executes in Windows Phone 7 without a hitch.

It’s also possible to have F# initialize various UI elements and create the UI for us.

But that really isn’t where functional programming normally excels in, so I’ll avoid demoing that worst practice.

Using a Visual Basic .Net UserControl in a Windows Phone 7

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

2) Add a VisualBasic.net Silverlight 3 project to the solution.

3) Add a new UserControl to your Silverlight 3 VB.Net project.

In our case we’ll add a simple user control with a <ListBox /> that has a few items.

Partial Public Class SL3VbNetUserControl Inherits UserControl Public Sub New InitializeComponent() Me.DataContext = {"Hello from VB.Net on WP7!", "World", "Foo", "Bar", "Baz"} End Sub End Class

<UserControl x:Class="WP7.VisualBasicNet.SL3VbNetUserControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="400"> <Grid x:Name="LayoutRoot"> <ListBox ItemsSource="{Binding}" /> </Grid> </UserControl>

4) Add a reference from the WP7 app to the new VB.Net library and build.

5) Add the new SL3 VB.Net UserControl to your MainPage in the WP7 project.

<!--ContentPanel - place additional content here--> <Grid x:Name="ContentGrid" Grid.Row="1"> <VisualBasicNet:SL3VbNetUserControl xmlns:VisualBasicNet="clr-namespace:WP7.VisualBasicNet;assembly=WP7.VisualBasicNet" /> </Grid>

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?

Sincerely,

-- Justin Angel