Add References and Set a License - Xamarin C#

This tutorial shows how to use the LEADTOOLS SDK in a C# Xamarin application and create a project, add references, and set a license.

Overview  
Summary This tutorial covers how to set a license in a C# Xamarin application
Completion Time 30 minutes
Visual Studio Project Download tutorial project (36 MB)
Platform C# Xamarin Cross-Platform Application
IDE Visual Studio 2017, 2019
Development License Download LEADTOOLS
Try it in another language

Required Knowledge

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.

Create the Project

Launch Visual Studio and select Create a new project.

Launching Visual Studio and selecting a new project

Select Mobile App (Xamarin.Forms) and click Next.

Select Mobile App Xamarin.Forms and click Next

Add the project name, specify the location where the project to be saved to and the other project details, then click Create.

Name project, specify location, and click Create

Select the desired template and the platforms for the project and then click OK.

Select the desired template and the platforms for the project and then click OK

Add the LEADTOOLS References

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...

Selecting 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.

Installing LEADTOOLS NuGet packages

Add the Set License Code

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:

Perform the following steps to add the SetLicense() code:

In the Solution Explorer, open MainPage.xaml.cs to bring up the code behind the MainPage. Add a using Leadtools; to the using block at the top.

In the MainPage() method add the below code before InitializeComponent().

C#
      public MainPage() 
      { 
#if __IOS__ 
         Leadtools.Converters.Assembly.Use(); 
#endif 
         Leadtools.Core.Assembly.Use(); 
         Leadtools.Svg.Assembly.Use(); 
         Leadtools.Platform.RuntimePlatform = Device.RuntimePlatform; 
 
         RasterSupport.Initialize(this); 
         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.SetLicense(licBytes, developerKey); 
 
         if (RasterSupport.KernelExpired) 
            DisplayAlert("Error", "License file invalid or expired.", "OK"); 
         else 
            DisplayAlert("Success", "License file set successfully!", "OK"); 
 
         InitializeComponent(); 
      } 

Run the Project - Android

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, a message on the device will appear displaying License file set successfully!

Success display screenshot on Android device

Run the Project - iOS

Inside Visual Studio for Mac, select the device to deploy to and press the "play" button.

If the steps were followed correctly, a message on the device will appear displaying License file set successfully!

Success display screenshot on iOS device

Wrap-up

This tutorial showed how to create a new C# Xamarin.Forms Project, add references via NuGets, and set the LEADTOOLS license.

This is the basis for all C# Xamarin applications using the LEADTOOLS SDK. All functionality in the SDK is unlocked via setting a license. SetLicense must be called before calling any other LEADTOOLS SDK functions.

After the SDK is purchased, the evaluation license can be replaced with a valid runtime license to disable the NAG Message.

See Also

Help Version 21.0.2023.3.1
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2021 LEAD Technologies, Inc. All Rights Reserved.