Xamarin MAUI: First iOS, Android and Windows Mobile App using Visual studio 2022
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
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.
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.- Visual Studio
- Visual Studio for Mac
- 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.After installation success, Open the Visual Studio IDE
In this sample demo application, I am going to show Android, iOS, and Windows app using MAUI, so Get started with “Create New Project”
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 ++.
In the Configure your new project window, name your project, choose your project location where you need to save, and click the Create button
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.MainPage.XAML
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.
.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
Application Class
The App class derives from the Application classResources
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.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
- Connect same wifi network
- Download and install Visual Studio 2022 with MAUI
Mac Configuration
- Connect same wifi network
- Install Xcode 13.1 ++
- On Network preference > switch on Remote login
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 filesusing 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 belowusing Foundation;
using Microsoft.Maui;
using Microsoft.Maui.Hosting;
namespace FirstMUAIApp
{
[Register("AppDelegate")]
public class AppDelegate : MauiUIApplicationDelegate
{
protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();
}
}
The out as like below
Run Android App
The android application, MainApplication.cs will execute first, it will call the MauiProgram static class initially as like belowusing 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();
}
}
The output like below
Run Windows App
The Windows application executes App.xaml.cs file first, it will call the MauiProgram static class initially as like belowSelect the device and run the Application
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
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK