3

Xamarin MAUI: First iOS, Android and Windows Mobile App using Visual studio 2022

 2 years ago
source link: https://www.msdevbuild.com/2021/11/First-Xamarin-MAUI-Mobile-App-Ios-android-windwos-visual-studio-2022.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Xamarin MAUI: First iOS, Android and Windows Mobile App using Visual studio 2022

Posted by Suthahar Jegatheesan on Monday, November 22, 2021 No comments

LabelsMAUI Xamarin Xamarin.Android Xamarin.Forms

.Net MAUI is a cross-platform framework for creating native mobile and desktop app with c# and Xaml. In my previous article, we did warn welcome to Dotnet MAUI and shared information about the History of Xamarin.

MAUI as everybody already knows is a name for a new upgrade solution as a Multi-platform APP UI framework for building native cross-platform apps with .Net for android, iOS, macOS, and Windows. I am going to show how to create, build and debug the First MAUI application using Visual Studio 2022.

NET MAUI is the .NET Multi-platform App UI, a framework for

MAUI and other third-party framework teams started working on support for upgrading the app and old NuGet package to MAUI. MAUI will also provide you support for building the apps in different modern patterns and frameworks MVVM, MVU and RxUI.

.NET MAUI IDE

Microsoft provided 3 fantastic IDE Tools to create and develop apps with .NET MAUI and the happy news is MAUI is an open source.
  1. Visual Studio
  2. Visual Studio for Mac
  3. Visual Studio Code

Visual Studio 2022

On Windows or Mac machines you can use Visual Studio Code or Visual Studio/ VS for Mac for development. You will need the visual studio 2022 17.1.0 preview version. Microsoft released Visual studio 2022 version 17.0 in Nov month, but MUAI is still in preview so you can start to install visual studio 2022 17.1.0 preview version to create your first MUAI application.

Build your first .NET MAUI app

After installation success, Open the Visual Studio IDE

What .NET MAUI Means for Xamarin Developers

In this sample demo application, I am  going to show Android, iOS, and Windows app using MAUI, so Get started with “Create New Project”

What is .NET MAUI, and How Does it Differ from Xamarin?

On the new Project template, select project type as an” MAUI” and you will all the MAUI preview template, if you are not finding the MAUI template means, you have not installed the latest preview version so make sure you have installed the latest preview version 17.1 ++.

Microsoft Replaces Xamarin Toolkits with New .NET MAUI

In the Configure your new project window, name your project, choose your project location where you need to save, and click the Create button

The future of Xamarin: .NET MAUI

MAUI Solutions Structure

After clicking on Create, MAUI solutions default template loaded with all the related MAUI NuGet packages. There is a single project with different platform folders and resources.

Creating first .Net MAUI project

MainPage.XAML

The main page, whichever design, will support all other platforms as well, and also Main Page XAML build action modified in MAUI app.

Main Page.Xaml build action modified with MAUIXAML, make sure Xaml Build action is correct.

Right click on the XAML page >>Properties>>BuildAction of XAML Page to MauiXaml.

Creating new .NET 6 MAUI project

.NET Generic Host

The Worker Service templates, create a .NET Generic Host, HostBuilder. The Generic Host can be used with other types of .NET applications, such as Console apps.

Maui Program class enables apps to be initialized from a single location, and provides the ability to configure fonts, services, and third-party libraries.

iOS, Android, windows, and all the platform entry point calls a CreateMauiApp method of the static MauiProgram class that creates and returns a MauiApp, while lunch the application

.NET MAUI: How MAUI Will it impact on Xamarin Native applications?

Application Class

The App class derives from the Application class

Workload Repair failed: `dotnet workload repair - MAUI

Resources

The resources are a good improvement in MAUI. The AppIcon, image and font folder will be available under the MAUI project and it is just a single SVG, Looks like Maui will just automatically take care of generating all the different icon sizes for different devices.


How do I determine where the error is when I check MAUI compatibility with maui-check?How do I determine where the error is when I check MAUI compatibility with maui-check?

Run IOS App

Building MAUI iOS applications requires access to Apple's build tools, which only run on a Mac. Because of this, Visual Studio 2022 must connect to a network-accessible Mac to build iOS applications.

You must install the Xcode 13.1++ version on the Mac machine before connecting Visual Studio 2022 to the Windows machine.

Windows Configuration 

  1. Connect same wifi network
  2. Download and install Visual Studio 2022 with MAUI

Mac Configuration

  1. Connect same wifi network
  2. Install Xcode 13.1 ++
  3. On Network preference > switch on Remote login
Pair to Mac machine from windows

Pair to Mac for Xamarin.iOS Development

Program.cs

Program class file is the main entry file on IOS app and executes the main method of the application, here you can define your custom app delegate file and other configuration files

using UIKit; namespace FirstMUAIApp

{

public class Program

{

// This is the main entry point of the application.

static void Main(string[] args)

{

// if you want to use a different Application Delegate class from "AppDelegate"

// you can specify it here.

UIApplication.Main(args, null, typeof(AppDelegate));

}

}

}

Appdelegate.cs

Each platform will call MauiProgram static class for initialize. On iOS will call as below

using Foundation;

using Microsoft.Maui;

using Microsoft.Maui.Hosting;

namespace FirstMUAIApp

{

[Register("AppDelegate")]

public class AppDelegate : MauiUIApplicationDelegate

{

protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();

}

}

You can select iOS simulator and device and click Run icon for executing the application

cv4X9LlbW-fgIqPAETRNf7DNC2VIJ1zcAVdzh0X-19piqKM_H_dbBSCoUHw2UCEE1MFObWh7pdqbddGUzXtHyRg5xVqC0cp6oDaKqPihVupppBn4BGNmTaAeIX5QNqgfldRtp3v_uNLI-65J3sg

The out as like below

vEOiwyaBoneHeVQtirPJbcfusAmwEn8RRYnPxE6j9GN1quR5JIV6uQHCDwzeQmTi5AaKwMARSjYE73UeQ9pbzinlCr0FfFJXwj1BWXwY69nN52jQlExhia5df9wK4p87KZv3mKNMc4LSYRQDsIc

Run Android App

The android application, MainApplication.cs will execute first, it will call the MauiProgram static class initially as like below

using System;

using Android.App;

using Android.Runtime;

using Microsoft.Maui;

using Microsoft.Maui.Hosting;

namespace FirstMUAIApp

{

[Application]

public class MainApplication : MauiApplication

{

public MainApplication(IntPtr handle, JniHandleOwnership ownership)

: base(handle, ownership)

{

}

protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();

}

}

You can select android simulator or device as below, press F5 to run the application
7rnsNwtwXY5pUpf3Ul-UVEsiPbDXVQ09Je914XaSwz62baOjvazdseADGXTy_3IWxhH9omXc8ZZ7pf339Svn2E1LXHvgM6U-XaCPNhTTzxLDnBj6QxwTs7XG2r6nNxi7NT92p-CI78AHxad3cJM

The output like below

rX6f7WIRId-nYhvpAAQmNkbJugBkQkrrYoW2B6WwMq0wJfSY0MDOGwsOC8tIbSW3PoYCgXkNbcihL6IYVBSCnIMh_XNFWB24ZgPe5JgeoDpUmbn5IR2bHOUxPZ5_Y5lj4ZMPX8MT6gd4OiKL3uw

Run Windows App

The Windows application executes  App.xaml.cs file first, it will call the MauiProgram static class initially as like below
Select the device and run the Application

LffScZthXBXYhb0W9jlXgJd3dJqXuPPPzpJ4Oer1EFSwLXWFebv1dMqAeKfwQUUl1QOQDjYqwgzaS0wtvy51bXnRt8Sc9--m1cUppvayUjMpuO4KmOftF85_CjBA4aFviFJnUwRrnCPq6vW38hQ

MAUI Windows device

Demo Video

I have shared a recorded demo video with two versions, English and Tamil. It will help you to understand more about creating, building, and debuging the First MAUI application.

English Demo Video

Tamil Demo Video 

Hope this article is very useful for you, If you have any questions/ feedback/ issues, please write in the comment box

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK