This tutorial shows how to create a project, add references, and set a license in a C# MAUI application using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers how to set a license in a C# MAUI application. |
Completion Time | 15 minutes |
Visual Studio Project | Download tutorial project (221 KB) Optional Resources :
|
Platform | C# .NET MAUI Cross-Platform Application |
IDE | Visual Studio 2022 |
Development License | Download LEADTOOLS |
Try it in another language |
|
Before any functionality from the SDK can be leveraged, a valid runtime license will have to be set.
For instructions on how to obtain a runtime license refer to Obtaining a License.
iOS Demo Resources and Android Demo Resources files are provided as optional downloads to simplify native platform features and are used by the LEADTOOLS MAUI tutorials. Developers can download and use the helper files in accordance to which operating systems will be targeted during deployment.
Launch Visual Studio and select Create a new project.
Select .NET MAUI App and click Next.
Add the project name, specify the location where the project to be saved to and the other project details, then click Next.
Note: Make sure the project name doesn't include spaces, as whitespaces can cause issues while building the project for some release targets.
Select the .NET runtimes that will be used in the application and select Create.
Building to Tizen will need to be removed as it is currently not supported. To remove Tizen support, follows these steps:
In the Solution Explorer, click on the project and remove the following line.
In the Solution Explorer, select the Platforms folder and delete the Tizen folder.
Add the necessary LEADTOOLS references. The references needed depend upon the purpose of the project. References will be added via NuGet reference:
Right-click the C# project in the Solution Explorer and select Manage NuGet Packages...
Browse for LEADTOOLS, then select one of the LEADTOOLS NuGet packages (any one will do) and install it. Accept LEAD's End User License Agreement.
Now that the LEADTOOLS references have been added to the project, code can be added to set the runtime license. The License unlocks the features needed for the project. It must be set before any toolkit function is called. The exact function called depends on the platform used. For details (including tutorials for different platforms), refer to Setting a Runtime License.
There are two types of runtime licenses:
In the Solution Explorer, open MainPage.xaml.cs
to bring up the code behind the MainPage
. Add a using Leadtools;
statement to the using
block at the top.
In the MainPage()
method, add the below code after InitializeComponent()
.
public MainPage()
{
InitializeComponent();
Dispatcher.Dispatch(async () =>
{
InitLead(this);
});
}
Create and add the InitLead()
function below the MainPage()
method.
private void InitLead(Page mainPage)
{
Leadtools.Core.Assembly.Use();
Leadtools.Svg.Assembly.Use();
Platform.RuntimePlatform = DeviceInfo.Platform.ToString();
string licString =
"[License]\n" +
"License = <doc><ver>2.0</ver>``PASTE YOUR LICENSE CONTENTS HERE``</doc>";
string developerKey = "PASTE YOUR DEVELOPER KEY HERE";
byte[] licBytes = Encoding.UTF8.GetBytes(licString);
RasterSupport.Initialize(mainPage);
try
{
RasterSupport.SetLicense(licBytes, developerKey);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
Note: If the assignment of
Platform.RuntimePlatform
is ambiguous due to overlapping namespaces, specify it asLeadtools.Platform.RuntimePlatform
.
Add the code to the default OnCounterClicked()
function to check if setting the license was successful.
private void OnCounterClicked(object sender, EventArgs e)
{
if (RasterSupport.KernelExpired)
DisplayAlert("Error", "License file invalid or expired.", "OK");
else
DisplayAlert("Success", "License file set successfully!", "OK");
count++;
if (count == 1)
CounterBtn.Text = $"Clicked {count} time";
else
CounterBtn.Text = $"Clicked {count} times";
SemanticScreenReader.Announce(CounterBtn.Text);
}
Run the project by pressing F5, or by selecting Debug -> Start Debugging.
If the steps were followed correctly, the application will run and clicking the button in the application will display "License file set successfully!".
Inside Visual Studio, select the device to deploy to and press the Play button.
If the steps were followed correctly, the application will run and clicking the button in the application will display "License file set successfully!".
Inside Visual Studio for Mac, select the device to deploy to and press the Play button.
If the steps were followed correctly, the application will run and clicking the button in the application will display "License file set successfully!".
This tutorial showed how to set a license in a new MAUI project, and how to enable permissions. This is the basis for all MAUI applications using the LEADTOOLS SDK. All the functionality in the SDK is unlocked via SetLicense
before calling any LEADTOOLS SDK member. After the SDK is purchased, the evaluation license can be replaced with a valid runtime license to disable the Nag Message.