.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 signature pad in .NET MAUI using .NET MAUI Community Toolkit.
Quick Links:
Project Setup:
- Launch Visual Studio 2022, and in the start window click Create a new project to create a new project.
- In the Create a new project window, select MAUI in the All project types drop-down, select the .NET MAUI App template, and click the Next button:
- In the configure your new project window, name your project, choose a suitable location for it, and click the Next button:
- In the Additional information window, click the Create button:
- Once the project is created, we can able to see the Android, iOS, Windows and other running options in the toolbar. Press the emulator or run button to build and run the app
Implementation:
.NET MAUI Community Toolkit is the key to achieve drawing in our App is to use Community.ToolKit.Maui NuGet package. It is a collection of reusable elements such as animations, behaviors converters, among others, for developing applications for iOS, Android, macOS and WinUI using MAUI.
Install .NET MAUI Community Toolkit
- Install .NET MAUI Community Toolkit nuGet package on your .NET MAUI application.
- Now initialize the plugin. Go to your MauiProgram.cs file. In the CreateMauiApp method, place in the .UseMauiApp<App>() line and just below it add the following line of code.
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.UseMauiCommunityToolkit()
.ConfigureFonts(fonts =>
{
fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
});
- DrawView is a class provided by Community.Toolkit.Maui which is responsible for providing a surface that allows drawing lines through touch or mouse interaction.
-
Add the following namespace in the xaml file.
xmlns:mct="clr-namespace:CommunityToolkit.Maui.Views;assembly=CommunityToolkit.Maui"
-
Once the namespace added, you have to add the DrawingView tag with the
properties that we required like below.
<mct:DrawingView x:Name="DrawBoard" LineColor="Black" LineWidth="5" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" HeightRequest="250" IsMultiLineModeEnabled="True" DrawingLineCompleted="DrawBoard_DrawingLineCompleted" BackgroundColor="AliceBlue"/>
-
WidthRequest and HeightRequest: These properties help to set the
width and height respectively.
Keep in mind that you must add both properties to your DrawingView in order for it to be displayed correctly in your app. - LineColor: It’s the color of the drawing line.
- LineWidth: It’s the width of the drawing line.
- IsMultiLineModeEnabled: By default, the DrawingView allows only one stroke to be drawn at a time. The IsMultiLineModeEnabled property allows you to change this and draw multiple lines at once. It receives Bool values. To activate it you need to set the value to True.
- If you want to clear the DrawView, add the following
DrawBoard.Lines.Clear();
- The clear function can be called using the button click event and can be called like below
<Button Text="Clear Board" Clicked="Button_Clicked"/>
void Button_Clicked(System.Object sender, System.EventArgs e) { DrawBoard.Lines.Clear(); }
- Add an Image control to your XAML.
DrawingView’s properties
Cleaning the DrawView:
Previewing the signature or drawing from DrawView:
<Image x:Name="ImageView"
WidthRequest="200"
HeightRequest="200"/>
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:
can change the backgroundcolor of the image?
ReplyDelete