Windows Phone 7 supports VB.Net and F#

Justin Angel picture

Justin
Angel

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?!

Man in agony

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”

DogInARainCoat

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.

Create new C# WP7 app

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

Add new project to the solution

Select project type F# Silverlight project

Use Silverlight version 3

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.

The WP7 emulator running F# code

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.

Create new C# WP7 app

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

Add new project to the solution

Select a Visual Basic Silverlight Class Library

Using Silverlight version 3

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

Build errors if the Microsoft.VisualBasic.dll reference is missing

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

Add Reference context menu on right click

Adding a reference to  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”.

Right click on Microsoft.VisualBasic and choose properties

Setting "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.

Windows Phone 7 Emulator running a Viusal Basic .Net user control

 

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



Comments

Matthijs Says:

Sorry, but I don't agree with your post, not supported doesn't meen technical not possible, it means not supported (and we sometimes translate this to 'you can't do that'. What if you use VB features which aren't on the phone?
As developer would stay away from F# and VB.net for the moment to prevent getting into trouble apps not working.

Justin Angel Says:

Hi,

I probably missed it, where did the Microsoft Windows Phone 7 team said F# and VB.Net are not supported?
From what I understood, it's just a lot of communtiy hype around "WP7 doesn't support anything but C#".

F# and VB.net end up getting compiled to MSIL, just like C#. There's no reason for them to not work under the WP7 CLR.

Additionally, if VB.Net specific features are used the Microsoft.VisualBasic.dll is part of the WP7 CLR, so those fetures are supported.

Due to these facts, I can't agree with your conclusion about avoiding using those languages.

Sincerely,
-- Justin Angel

Lennie Says:

False, the whole idea of the .NET framework is to be able to mix and match languages in any away that you want.... You wanna mix C++ and C#? Sure! You wanna mix VB.Net and F#? Sure! In this artical his just mixing C# with a VB.Net and F# Silverlight library that is very valid. Not support means that there is no VB.Net/F# templates for WP7 development etc since you need to go thru a proxy but yet again nothing prevent you from using this method and use your existing VB.Net/F# code.

Jim Wooley Says:

Justin, Thanks for taking the time to show people how you can leverage F# and VB.Net in the WP7 apps.

Mario De Ghetto Says:

Hi Justin, good job!
Can I translate this article for Italian people?
Could you give me the permission?
Thanks in advance

Mario De Ghetto
Microsoft MVP - Visual Basic Development
http://deghetto.wordpress.com
http://fsharpitalia.wordpress.com

Justin Angel Says:

Sure, go right ahead. Ping me @ J@JustinAngel.net if you want the original windows live writer document and not just copy paste stuff out of this window.

-- Justin

ZMan Says:

We've had similar 'issues' and workarounds using XNA on the Xbox. The CF has missing APIs that some languges need, C++ can output IL instructiosn that are ot supported and more.

I dont know yet how it will apply to the phone but this FAQ is a start http://forums.xna.com/forums/t/1464.aspx

Joacim Andersson Says:

Nice Justin. I was wondering though when you add a reference to Microsoft.VisualBasic.dll (I just call it VB.dll below) in the original C# project doesn't that mean that you need to deploy that file to the phone? The whole problem with getting support for VB on anything that uses just a part of the framework (such as WP7 and XNA for writing games for the X-Box) is that the VB.dll contains references to other parts of the framework that might not be part of the supplied partial framework on the device. So I wonder if you've really tried this on an actual WP7 device or have you only tested it on an WP7 emulator?
You could of course use the command line compiler to remove the reference to the VB.dll but then the problem with using CType would arise, at least in those cases the type conversions can't be resolved during compile time.

Justin Angel Says:

Hi Joacim,

The Microsoft.VisualBasic.dll is part of the Silverlight 3 ditribution which has been ported to WP7. We're explictly setting "Copy Local = False" on that reference so it won't be compiled into the XAP. So, Microsoft.VisualBasic.dll is already on the device and we're just telling our XAP to use it.

The emulator should work exactly the same way as the real phone as it's the same CLR, same GAC, same everything.

Sincerely,
-- Justin Angel

Nick Avery Says:

Thanks Justin, usefull information.

I was sure this must be possible, just hadn't had a chance to try it myself.

Joacim Andersson Says:

Sounds good to me ;)

So one must ask, what is the problem with having a VB WP7 Application template? As I understood it, Microsoft has promised that "pure" VB support for the WP7 will come though.

Justin Angel Says:

Yep, I heard the same official public statements from Microsoft as well - that VB.net will be supported once WP7 ships.

While I don't have insight as to why VB.Net isn't supported in the WP7 Betas, I'm guessing it's just a resource crunch issue. Microsoft can't build everything in the first release.

It should be possible to some entrepreneurial individual to ship a WP7 Beta application VB.net template. But with the final WP7 release happening on 9/16, I'd say that's a waste of time.

Sincerely,
-- Justin Angel

S.H. Park Says:

It is really cool!
It is no reason that VB.NET does not work at WP7.

Thank you very much.

Can I write a post about VB.NET for WP7 based on your post in Korean? I will test and make it by myself.

Justin Angel Says:

Hi,

Sure thing, go right ahead. You have my permission. Let me know if you need any help from me.

-- Justin

rj Says:

the real question is, _WHY_ would anyone ever not want to use C#??

sorry, couldn't resist. Nice article, lots of images :)

rj

me Says:

.... I want to , but the squiggley braces are very confusing.
:(

I prefer programming in the english language.


....flame away.

Knight Says:

Thanks a lot! I'm Visual Basic developer.

Abel Ansah Says:

I am very happy that there are people like you who would defend the course VB.Net. I am also happy after several hours of searching a found a good work around on the seemingly missing support for vb.net in WP7.

road Says:

dude. your mousewheel scroll implementation on this web page is really, really bad, choppy.

Harry Says:

I was disappointed to see that the september 16 released tools also did not have templates under vb.net section.
Was anyone able to try this on an actual device? Is it possible that it works in the emulator and not in the device?

Randall "texrat" Arnold Says:

Does this not work for VS 2010 Express for Windows Phone? The templates you show at step "2) Add a VisualBasic.net Silverlight 3 project to the solution." are not available for me... I can't see those Silverlight classes at all...

I read somewhere it was possible to use VB.Net with Express... maybe this isn't the approach I'm looking for?