Visual Basic and Cross-Platform: Mobile Apps with VB, Xamarin, and .NET Standard! (original) (raw)

How would you like it if you could create a Console App in Visual Basic and run it on Linux? Or running the same Xamarin.Forms-App written in Visual Basic on an iPhone, and Android and a Windows Tablet? Welcome to the world of cross-platform development, which from Visual Studio 2017 Update 3 on – thanks to .NET Standard and .NET Core – is now also available for Visual Basic! Well, currently the preview of Visual Studio 2017 lets you use this, and it is the best set of features that Update 3 (aka VS 15.3) will bring for us VBs: We are getting Visual Basic .NET Core and .NET Standard templates. Note though, that there are some limitations in this first version for Visual Basic regarding .NET Core and .NET Standard support compared to C#: There is no ASP.NET template for .NET Core yet, and Xamarin support when using .NET Standard is also limited. But we come to that later in more detail.

Before we start with a sample to show off what we VBs can do with our new cross-platform powers, let’s ask the team’s .NET Core/.NET Standard champion Immo Landwerth to explain what .NET Core and .NET Standard are with just one respective tweet:

@terrajobst For #visualbasic guys, who just got #netstandard and #dotnetcore with VS 2017 U3, can you explain both in a respective tweet?

— Klaus Löffelmann (@loeffelmann) May 14, 2017

.NET Core is our new open source and cross platform .NET implementation. It supports command line tools and https://t.co/a0lvit6464 apps.

— Immo Landwerth (@terrajobst) May 14, 2017

But, as I said, let’s be clear about one thing:

@loeffelmann That's right, we don't have templates for https://t.co/a0lvit6464 Core yet. AFAIK we plan on adding them except for Razor as that's C# tech.

— Immo Landwerth (@terrajobst) May 14, 2017

If you want to learn more about .NET Core and .NET Standard, I suggest you to read Immo’s excellent blog posts Introducing .NET Standard and Introducing .NET Core, and also watch some awesome videos about the topic on YouTube in addition!

Using Xamarin.Forms with .NET Standard in Visual Basic

With that, let’s take a look what that means for a practical sample, and we concentrate in this example on .NET Standard, not on .NET Core. (If you want to see a dedicated .NET Core VB sample, tweet me @loeffelmann or find me at ActiveDevelop). As you might know, Visual Studio supports cross-platform development on Android, iOS, Windows Universal Platform and the Mac in C# via Xamarin since Visual Studio 2015. Principally, the Xamarin tooling provides two options for creating apps for platforms other than Windows.

Here is how that works in practice: In Xamarin.Forms there are – let’s call them – Bootstrap-Projects for every platform (iOS, Android, UWP – later Xamarin will add WPF as a potential target platform as well). Those bootloader apps, which Visual Studio creates for you in C#, then initialize the Xamarin.Forms framework and utilize one shared cross-platform DLL for each platform which holds at least one so-called ContentPage with the initial UI. This ContentPage becomes the main Activity in Android, the main Page in Windows UWP and the main ViewController in iOS. So, this shared library holds the UI in a somewhat generic UI definition, and Xamarin.Forms translates these generic UI elements into the platform-native equivalents of each platform-specific app at runtime. Now, here is the important part for us VBs: Such a Xamarin.Forms library is by standard (for now, but subject to change in later versions!) a Portable Class Library project, but it can also be a .NET Standard class library project. And since Visual Basic supports .NET Standard from Visual Studio 2017 Update 3 on, we can have the lion share of a Xamarin.Forms app also in Visual Basic! Compared to C#, there is one drawback, though: While in C# a Xamarin.Forms .NET Standard DLL can define the UI for all apps via the markup language XAML, this scenario is so far not supported in Visual Basic. In VB, Xamarin.Forms libraries can define their UI only through code.

The following screenshot points out the different project types of the solution in the solution explorer.

Now, let’s see how to make this work. If you happened to read the post about Unit Testing with Visual Basic, you already learned about the Roman Numerals App we built in that context as our demo example. Our goal now: Let’s port this app to Xamarin.Forms and have it run on UWP, Android and iOS!

Before we start coding, here are some Beta prerequisites.

Now, be warned: As said before, when we develop the app(s), there will be C# projects involved, but we will just create them. We do not need to touch their code! All things we code will be pure VB. So, here we go:

When Visual Studio sets up the Xamarin.Forms project, the actual front-end code will be hosted in a C# portable class library by default. We’re going to replace this default PCL C# library by a Visual Basic .NET Standard library.

NOTE: Portable Class Libraries are the precursor of .NET Standard Libraries. In its current preview state, Visual Studio does not yet provide the Xamarin .NET Standard library templates for hosting the Xamarin.Forms based UI code – but setting up this library type manually already works! What’s important though for this to succeed: The new library must address the Standard V1.4, so it is supported by iOS, Android and UWP apps (later versions of those platforms will support .NET Standard V2.0).

Copy the content of the following listing to the project file, save the changes and close the document. With that, you are changing the Standard version to Version 1.4 for once, and secondly, this makes the project backward compatible for NuGet packages, which were originally created for Portable Class Libraries. This, by the way, will no longer be necessary, once the final templates and NuGet packages for Xamarin.Forms for Update 3 have been released.

Let’s rename the existing Class1.vb file to App.vb, and insert this code:

This code does not do that much: At some point, it will be called by the bootstrap project of each individual platform app, and instantiate the MainView (the UI).

This MainView again, needs to be placed in another code file named MainView.vb, which you need to add to the project. Here’s the code:

There is only one step left to do. Since we erased the original Xamarin.Forms project (which was in C#, and which we replaced by our Visual Basic version), the references from the three main projects for each UI got also lost. So, we need to go through each of the platform-specific bootstrap projects and add the reference back to our VB Xamarin.Forms .NET Standard project.

If everything went smoothly so far, you should be ready to fire up the app. Let’s start with the UWP app directly on our Windows machine, because that is the simplest way to test the app. For that, find the UWP project in the solution explorer, open the context menu on the project root, and click Set as StartUp project. Start the app.

If you have an Android phone or an Android tablet, let’s see how easy it is to deploy the app on a physical Android machine. Two things are necessary to make this happen.

NOTE: Enable the Developer mode before you connect your Android device to our developer machine.

And now: Happy cross-platform development in Visual Basic!

Tip: You can get the project directly from the VB Samples Git repository, which you find here (04 .NET Standard).

And, as always: Comment and like this post! Tell others about the posts here on the VB blog! And follow me also on Twitter @loeffelmann, to receive the latest tips and news about .NET Development, Software Modernization, C# and of course Visual Basic Development!

Author

Klaus Loeffelmann