Introduction

.NET MAUI is a powerful platform for building cross-platform mobile applications, and with the right tools and resources, it's easy to add signature pad functionality to your app. In this tutorial, we'll walk you through the steps of the signature pad in .NET MAUI using .NET MAUI Community Toolkit.

post_signaturepad

Project Setup

Implementation of .NET MAUI

.NET MAUI Community Toolkit is the key to achieving drawing in our App is to use Community.ToolKit.Maui NuGet package. It is a collection of reusable elements such as animations, and behavior converters, among others, for developing applications for iOS, Android, macOS, and WinUI using MAUI.

How to Install .NET MAUI Community Toolkit?

var builder = MauiApp.CreateBuilder();
builder
	.UseMauiApp<App>()
	.UseMauiCommunityToolkit()
	.ConfigureFonts(fonts =>
	{
		fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
		fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
	});

DrawingView's properties

Cleaning the DrawView

If you want to clear the DrawView, add the following.

DrawBoard.Lines.Clear();

The clear function can be called using the button click event, like below.

<Button Text="Clear Board" 
        Clicked="Button_Clicked"/>
void Button_Clicked(System.Object sender, System.EventArgs e)
{
	DrawBoard.Lines.Clear();
}

Previewing the signature or drawing from DrawView

Add an Image control to your XAML.

<Image x:Name="ImageView"
       WidthRequest="200"
       HeightRequest="200"/>

Add an event for DrawingLineCompleted in the code behind.

private void DrawBoard_DrawingLineCompleted(System.Object sender, CommunityToolkit.Maui.Core.DrawingLineCompletedEventArgs e)
{
	ImageView.Dispatcher.Dispatch(async() =>
	{
		var stream = await DrawBoard.GetImageStream(300, 300);
		ImageView.Source = ImageSource.FromStream(() => stream);
	});
}

Full Code

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:mct="clr-namespace:CommunityToolkit.Maui.Views;assembly=CommunityToolkit.Maui"
             x:Class="MauiDrawingApp.MainPage">

    <ScrollView>
        <VerticalStackLayout
            Spacing="25"
            Padding="30,0"
            VerticalOptions="Center">

            <mct:DrawingView x:Name="DrawBoard"
                   LineColor="Black"
                   LineWidth="5" 
                   HorizontalOptions="FillAndExpand"
                   VerticalOptions="FillAndExpand"
                             HeightRequest="250"
                   IsMultiLineModeEnabled="True"
                   DrawingLineCompleted="DrawBoard_DrawingLineCompleted"
                   BackgroundColor="AliceBlue"/>

            <Image x:Name="ImageView"
                    HorizontalOptions="FillAndExpand"
                   VerticalOptions="FillAndExpand"
                   BackgroundColor="White"
       HeightRequest="200"/>

            <Button Text="Clear Board" 
                    Clicked="Button_Clicked"/>

        </VerticalStackLayout>
    </ScrollView>

</ContentPage>
namespace MauiDrawingApp;

public partial class MainPage : ContentPage
{
	public MainPage()
	{
		InitializeComponent();
	}
    private void DrawBoard_DrawingLineCompleted(System.Object sender, CommunityToolkit.Maui.Core.DrawingLineCompletedEventArgs e)
    {
        ImageView.Dispatcher.Dispatch(async() =>
        {
            var stream = await DrawBoard.GetImageStream(300, 300);
            ImageView.Source = ImageSource.FromStream(() => stream);
        });
    }
    void Button_Clicked(System.Object sender, System.EventArgs e)
    {
        DrawBoard.Lines.Clear();
    }
}
using Microsoft.Extensions.Logging;
using CommunityToolkit.Maui;
namespace MauiDrawingApp;

public static class MauiProgram
{
    public static MauiApp CreateMauiApp()
    {
        var builder = MauiApp.CreateBuilder();
        builder
            .UseMauiApp<App>()
            .UseMauiCommunityToolkit()
            .ConfigureFonts(fonts =>
            {
                fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
                fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
            });

#if DEBUG
        builder.Logging.AddDebug();
#endif

        return builder.Build();
    }
}

Summary

You can download the code from GitHub. If you liked this article and it is helpful to you, do like, share the article & star the repository on GitHub. If you have any doubts, feel free to post a comment.