Thursday, December 16, 2021

Explore .NET MAUI Apps with .NET 6.0 & Visual Studio 2022 Version 17.1.0 Preview 1.1

The birth of .NET MAUI among the family of .NET products is very exciting. Although we did have Xamarin to develop cross-platform mobile applications in the past, .NET MAUI is a little different because it uses a unified version .NET 6.0 for mobile devices and desktop computers.

MAUI is not yet officially released. The current bits offer a glimpse into what the final product will look like. In this article, I will introduce you to .NET MAUI Apps. 

Meantime, this is the environment that I am using:

  • Windows 11 Version 21H2
  • Visual Studio 2022 Version 17.1.0 Preview 1.1
  • .NET 6.0.101
Source code for this application can be found at:


You will find installation instructions for .NET MAUI at:

The only workload I installed in Visual Studio 2022 (Preview) is "Mobile development with .NET", as shown below:

It is also worth noting that I do not have any other Android development application (like Android Studio) installed on my computer. The above Visual Studio 2022 workload also installed an Android emulator.


Let's get started exploring what apps we can develop with .NET MAUI. Start Visual Studio 2022 (Preview) and select "Create a new project":

Enter "maui" in the filter field. You will discover that there are three MAUI-related projects that you can create - namely: 
  1. .NET MAUI App
  2. .NET MAUI Blazor App
  3. .NET MAUI Class Library
In this tutorial, I will explore the first in the above list - .NET MAUI App. Select this project type then click Next:

I named my application FirstMauiApp:

Firstly, let's run our app on Windows. From the drop-down-list at the top of Visual Studio 2022, make sure you have chosen "Windows Machine".

Click on "Windows Machine" to run the application. Soon after, you should experience the following application running on your desktop:

Stop the application by either closing it or clicking on the red square button on the top of Visual Studio 2022. You will find that the application gets installed in your "Apps and Features" on windows. You can, of course, uninstall it if you so desire.

Adding our own page

Add “.NET MAUI ContentPage (Preview)” named ToonPage.xaml:

Edit App.xaml.cs so that it starts ToonPage instead of MainPage.

MainPage = new ToonPage();

Run the application. It should look like this:

Stop the application.

We will modify ToonPage so that it reads an online API that contains some cartoon characters. If you point your browser to it will show the following data:

[{"id":1,"lastName":"Flintstone","firstName":"Fred","occupation":"Mining Manager","gender":"M","pictureUrl":"","votes":0},{"id":2,"lastName":"Rubble","firstName":"Barney","occupation":"Mining Assistant","gender":"M","pictureUrl":"","votes":0},{"id":3,"lastName":"Rubble","firstName":"Betty","occupation":"Nurse","gender":"F","pictureUrl":"","votes":0},{"id":4,"lastName":"Flintstone","firstName":"Wilma","occupation":"Teacher","gender":"F","pictureUrl":"","votes":0},{"id":5,"lastName":"Rubble","firstName":"Bambam","occupation":"Baby","gender":"M","pictureUrl":"","votes":0},{"id":6,"lastName":"Flintstone","firstName":"Pebbles","occupation":"Baby","gender":"M","pictureUrl":"","votes":0},{"id":7,"lastName":"Flintstone","firstName":"Dino","occupation":"Pet","gender":"F","pictureUrl":"","votes":0},{"id":8,"lastName":"Mouse","firstName":"Micky","occupation":"Hunter","gender":"M","pictureUrl":"","votes":0},{"id":9,"lastName":"Duck","firstName":"Donald","occupation":"Sailor","gender":"M","pictureUrl":"","votes":0}]

Each JSON object contains the following properties:

id (int)
lastName (string)
firstName (string)
occupation (string)
gender (string)
pictureUrl (string)
votes (int)

To represent the above data, we will add a class named Toon with the following content:

public class Toon {
  public int Id { get; set; }

  public string LastName { get; set; }

  public string FirstName { get; set; }

  public string Occupation { get; set; }

  public string Gender { get; set; }

  public string PictureUrl { get; set; }

  public int Votes { get; set; }

  public string FullName {
    get {
        return string.Format("{0} {1}", this.FirstName, this.LastName);

  public override string ToString() {
    return string.Format($"{Id}\t{FullName}\t{Occupation}\t{Gender}\t{PictureUrl}\t{Votes}");

You will need to resolve the appropriate namespace for JsonPropertyName. This will add the following to your using statements:

using System.Text.Json.Serialization;

We will need to update the UI file named ToonPage.xaml, so that we  can display the contents of the Toon[] array. In ToonPage.xaml, replace the existing <Label .... /> control with:

<CollectionView x:Name="cvToons" ItemsLayout="VerticalGrid, 2">
      <Grid Padding="10" RowDefinitions="60" ColumnDefinitions="70,*">
        <Image Grid.RowSpan="2" 
        Source="{Binding PictureUrl}" 
            <RectangleGeometry Rect="0,0,160,160"/>

        <Label Grid.Column="1" 
        Text="{Binding FullName}" 
        LineBreakMode="TailTruncation" />

        <Label Grid.Column="1" 
        Text="{Binding Occupation}"
        VerticalOptions="End" />


Next, add the following method to your ToonPage.xaml.cs file, which reads the content of the API, hydrates it into an array of Toon objects Toon[], then binds the data to the CollectionView control with ID cvToons:

public async void GetToonsAsync() {
  HttpClient client = new HttpClient();
  var stream = client.GetStreamAsync("");
  var data = await JsonSerializer.DeserializeAsync<Toon[]>(await stream);
  Dispatcher.Dispatch(() => cvToons.ItemsSource = data);

You will need to resolve the namespace for the JsonSerializer class, which is:

using System.Text.Json;

Finally, append a call to the above method in the constructor by adding this statement just below InitializeComponent():


Run your application and you will see the following output:

Let us see what this app looks like in an android emulator. To setup an emulator, choose Tools >> Android >> Android Device Manager...

You can configure an Android device of your choice. In my case, even though I configured both Pixel 4 & Pixel 5, I found Pixel 4 to be more cooperative.

You can start the emulator of your choice from within the "Android Device Manager".

Choose the android emulator of your choice in the run drop-down-list at the top of Visual Studio 2022:

Run your app in the Android emulator. This is what it should look like:

I hope you found this useful. I am sure this product will evolve and, perhaps, change by the time it is finally released.

No comments:

Post a Comment