Getting Started With .Net MAUI


In this tutorial, we'll give a brief overview of what .NET MAUI is and how you can start using it in your projects. .NET MAUI is an open-source, cross-platform framework library for .NET that makes it easy to create rich, modern user interfaces. .NET MAUI provides a multi-platform app UI. We can use .NET MAUI for creating native mobile and desktop apps using C# and XAML. It also supports XAML hot reload, which means we can edit code while our .NET MAUI application runs on any machine like windows or android emulator.

It provides a variety of controls and widgets, and it's easy to use; you can get started with just a few lines of code. It is a set of libraries and tools that make it easy to create .NET cross-platform applications. We can select any target platform for the development environment.

How MAUI Works

.NET 6 unifies Android, iOS, and more into a single API that allows for Write once run anywhere development while additionally providing deep access to every aspect of each native platform. .NET MAUI framework provides developers with rich cross-device capabilities without sacrificing any functionality or productivity on either end due simply because the whims of one operating system do not limit it - you can use all four technologies interchangeably depending upon what best suits your needs at any given time!

.NET 6 allows all these frameworks to access the same .NET Base Class Library (BCL). It is abstracted from your code and relies on the .NET runtime for its execution environment. For Android, iOS, and macOS, the runtime environment is provided by Mono. On Windows, it's .NET CoreCLR. While the BCL offers a way to share business logic, each platform has a different user interface and interface options. Of course, you can make the UI for each platform with its respective framework (.NET for Android, .NET for iOS, .NET for macOS or WinUI 3), but this approach requires you to maintain a code-base specific to each family.

Getting Started with .NET MAUI Application

You need visual studio 2022 and .NET 6 framework installed for creating the .NET MAUI application writing in C#. Then, follow the next steps to create a .NET MAUI app.

Open Visual Studio

Open visual studio and click on the "Create a New Project" button. Next, search for .NET MAUI in the search bar.

Select .NET MAUI App

Select the .NET MAUI app template from the search results in visual studio. After selecting it, give it a proper name and set the location for the project. After configuration, hit the "Next" button.

Select Framework

Select the required framework, but the latest version of the .NET framework is recommended. After selecting the framework version in visual studio, hit the "Create" button.

It'll create a new .NET MAUI Project in visual studio 2022. By default, .NET MAUI creates a simple counter application.

We'll modify the .NET MAUI app and use this multi-platform app to generate PDFs using the IronPDF C# PDF library.

IronPDF: C# PDF Library

IronPDF is a PDF library for .NET developers. You can create PDFs in .NET applications from scratch using HTML files, HTML string, and URLs or convert existing documents to PDF with just a few lines of code. In addition, you can easily use it in .NET MAUI Apps for PDF operations.

Let's see how we can create PDF files using IronPDF in .NET MAUI Application.

Install IronPDF in .NET MAUI App

Using the NuGet Package manager, we can install IronPDF in the .NET MAUI app. Follow the next steps to install the IronPDF in .NET MAUI App.

  • Click on the "Tools" menu, select NuGet Package Manager from the Toolbar dropdown, and then select the "Package Manager Console".

  • Write the following command in the console and hit enter.

    install-package ironpdf
  • It'll install the IronPDF library in the .NET MAUI project. Then, you'll be able to use it for PDF processing in the .NET MAUI application.

Creating PDF Files in .NET MAUI Applications

Using IronPDF, we can create a new PDF in the MAUI app. But first, let's set up the UI for generating PDFs in the .NET MAUI app.

Setting Up Frontend

The .NET MAUI application can create multiple UI controls like buttons, text boxes, etc. We'll create a button for this task to help us create a PDF file. Open the "MainPage.xaml" file and paste the following code.

    Text="Generate PDF using HTML String"
    SemanticProperties.Hint="Generate PDF from HTML string"
    HorizontalOptions="Center" />

The above piece of code helps to create the button. Inside the tag, some properties of the button are defined, like Text, Name of the button, function to call when clicked, and positioning. You can change it according to your requirements. This code will add the button in the center position with the specified text. Now, we've to define the function to create a button.

Creating a function to Generate PDF File

We have to define the "HtmlToPdf" function to convert HTML to PDF files using IronPDF. First, open the "MainPage.xaml.cs" file and paste the following code.

ChromePdfRenderer renderer = new ChromePdfRenderer();
var doc = renderer.RenderHtmlAsPdf("<h1>Hello IronPDF!</h1> <p>I'm using IronPDF MAUI!</p>");
SaveService saveService = new();
saveService.SaveAndView("IronPDF HTML string.pdf", "application/pdf", doc.Stream);

This piece of code helps to generate PDF files using the HTML String. RenderHtmlAsPdf functions allow for conversion. After that, I created an object for SaveService class. After this section, I'll define the code for the "SaveService" class. SaveService class helps to download and open the generated File. You can get more tutorials about PDF processing using the following link.

Output PDF File

IronPDF will generate the following PDF file.

Save and Open the File in .NET MAUI

There is no direct option in MAUI to save and open the File. For the following functionality, we've to create a separate class and write code separately. Follow the next steps to develop Save and Open functionality in .NET MAUI.

Create a partial class named "SaveService.cs" and paste the following code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MAUI_Tutorial
    public partial class SaveService
            public partial void SaveAndView(string filename, string contentType, MemoryStream stream);

Now open the Windows folder inside the Platform folder. Create a new file named "SaveWindows.cs" and paste the following code.

using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
using Windows.UI.Popups;

namespace MAUI_Tutorial
    public partial class SaveService
        public async partial void SaveAndView(string filename, string contentType, MemoryStream stream)
            StorageFile stFile;
            string extension = Path.GetExtension(filename);
            //Gets process windows handle to open the dialog in application process. 
            IntPtr windowHandle = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle;
            if (!Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons"))
                //Creates file save picker to save a file. 
                FileSavePicker savePicker = new();
                savePicker.DefaultFileExtension = ".pdf";
                savePicker.SuggestedFileName = filename;
                //Saves the file as Pdf file.
                savePicker.FileTypeChoices.Add("PDF", new List<string>() { ".pdf" });

                WinRT.Interop.InitializeWithWindow.Initialize(savePicker, windowHandle);
                stFile = await savePicker.PickSaveFileAsync();
                StorageFolder local = ApplicationData.Current.LocalFolder;
                stFile = await local.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
            if (stFile != null)
                using (IRandomAccessStream zipStream = await stFile.OpenAsync(FileAccessMode.ReadWrite))
                    //Writes compressed data from memory to file.
                    using Stream outstream = zipStream.AsStreamForWrite();
                    //Saves the stream as file.
                    byte[] buffer = stream.ToArray();
                    outstream.Write(buffer, 0, buffer.Length);
                //Create message dialog box. 
                MessageDialog msgDialog = new("Do you want to view the document?", "File has been created successfully");
                UICommand yesCmd = new("Yes");
                UICommand noCmd = new("No");

                WinRT.Interop.InitializeWithWindow.Initialize(msgDialog, windowHandle);

                //Showing a dialog box. 
                IUICommand cmd = await msgDialog.ShowAsync();
                if (cmd.Label == yesCmd.Label)
                    //Launch the saved file. 
                    await Windows.System.Launcher.LaunchFileAsync(stFile);

The above code helps to save and open the file on a windows desktop. For android and IOS apps, you've to write different codes. You can get the working project from GitHub using the following link.

This is how we create PDF files using IronPDF in the .NET MAUI application.


This article teaches how the MAUI application works and the usage of .NET MAUI applications. Dot NET MAUI is an excellent tool for mobile development and desktop apps with a single code base.

Similar Articles