Mobile Apps with Visual Basic & Xamarin.Forms

Craig Dunn

If you are a Visual Basic developer, your options for becoming a mobile developer have historically been limited to targeting Windows Phone; however, with Xamarin.Forms, Portable Class Libraries, and Visual Studio, developing iOS and Android apps entirely in Visual Basic has become a real possibility.

Last year I wrote about how Visual Basic Portable Class Libraries (PCLs) can be used in Xamarin apps. Xamarin.iOS and Xamarin.Android can access business logic written in Visual Basic PCLs, while writing the platform-specific code (including the user interface) in C# (or F#) on the Xamarin platform.

Visual Basic Xamarin.Forms

Since then we’ve released Xamarin.Forms, which makes it possible for an entire app—from business logic to user interface—to be written in a PCL and shared across platforms. This means that it’s become possible to write entire mobile applications in Visual Basic in Visual Studio using Xamarin.Forms. The Xamarin developer portal has instructions on creating a Visual Basic project from the Xamarin.Forms new project template. A “Basic” App class is shown here:

Public Class App
    Inherits Application

    Public Sub New()
        Dim label = New Label With {.XAlign = TextAlignment.Center,
                                    .FontSize = Device.GetNamedSize(NamedSize.Medium, GetType(Label)),
                                    .Text = "Welcome to Xamarin.Forms with Visual Basic.NET"}

        Dim stack = New StackLayout With {
            .VerticalOptions = LayoutOptions.Center
        }
        stack.Children.Add(label)

        Dim page = New ContentPage
        page.Content = stack
        MainPage = page

    End Sub

Once you’ve created a new project and converted it to Visual Basic (or just started with the sample) your entire application can be written in Visual Basic. The code below represents a simple ContentPage written in Visual Basic. You can use this as a basis for new pages in your apps:

Public Class Page2
    Inherits ContentPage

    Public Sub New()
        Dim label = New Label With {.XAlign = TextAlignment.Center,
                                    .FontSize = Device.GetNamedSize(NamedSize.Medium, GetType(Label)),
                                    .Text = "Visual Basic  ContentPage"}

        Dim button = New Button With {.Text = "Click me"}
        AddHandler button.Clicked, Async Sub(sender, e)
                                       Await DisplayAlert("Hello from VB", "Visual Basic.NET is back!", "Thanks")
                                   End Sub

        Dim stack = New StackLayout With {
            .VerticalOptions = LayoutOptions.Center
        }
        stack.Children.Add(label)
        stack.Children.Add(button)

        Content = stack
    End Sub

End Class 

Xamarin doesn’t implement support for the Visual Basic language directly, so you can’t use Xamarin Studio for these projects; however, if you’re a Visual Basic programmer, or have existing Visual Basic code you’d like to mobilize, there’s never been a better time to get started with mobile development with Xamarin.Forms! And, of course, if you have published a Windows Phone application written in Visual Basic, there’s still time to take advantage of our free subscriptions for Windows Phone Developers promotion before August 31, 2015.

0 comments

Discussion is closed.

Feedback usabilla icon