Overview

Skill Level: Intermediate

Prerequisites

Supported Xamarin versions: 

  • Xamarin.Forms 3.6.0 (and later)
  • Xamarin.Android 28.0.0 (and later)

Limitations

The TabView controls in Xamarin do not have their background color captured correctly by the SDK.

 

Introduction

Xamarin is a mobile development framework that enables developers to create native Android, iOS, and Windows apps in a C#/.NET based environment. Xamarin offers two tools for creating mobile apps:

  • Xamarin.Forms¬†(for creating cross-platform apps) – Xamarin Forms is assisted by the Xamarin.iOS and Xamarin.Android¬†libraries, which are C# object libraries that wrap the Tealeaf APIs and give access to the Tealeaf iOS and Android SDKs.
  • Xamarin Native¬†

Both tools allow you to reuse code between iOS and Android, although only Xamarin.Forms truly provides the “write once, run anywhere” experience. Most sample apps provide the ability to use both.

 xamarin comparison

Xamarin wrapper design

Tealeaf provides two Xamarin wrappers for the Android and iOS versions of the Tealeaf SDK. They wrap all the available Tealeaf APIs, such as logScreenLayout() and logEvent(), in C# and take the form of a dynamic-link library (DLL). 

xamarin wrapper design

Using the wrapper with Xamarin Native

Applications written using Xamarin.Android and Xamarin.iOS can directly call the Tealeaf Interface implementation for the respective platform. The interface handles calls to the Xamarin/C# wrapper for the SDK, performs any necessary pre-/post-processing, and packages them in its methods.

Using the wrapper with Xamarin Forms

Because Xamarin.Forms is platform-generic, Tealeaf cannot specifically call one wrapper or the other in the source code. Instead, Tealeaf utilizes the DependencyService class to access platform-specific functionalities.

First, Tealeaf defines an interface for the Tealeaf APIs on the Xamarin.Forms side, usually with one method corresponding to each API. Next, Tealeaf writes separate implementations of the interface on Android and iOS, which call the APIs of the Xamarin wrappers respectively.

Example

In this example, you want to call Tealeaf’s¬†logEvent()¬†API on the Xamarin.Forms side to log a click event on a view. In Xamarin.Forms, the calls occur on views that are instances of the¬†Xamarin.Forms.View¬†class, not¬†Android.View. For this reason, Tealeaf cannot call the Xamarin wrapper APIs directly.

First define an interface ITealeaf that contains the following empty method:

public interface ITealeaf {
...
void LogClickEvent(Xamarin.Forms.View view);
...
}
Then, we would implement this interface (and consequently its methods) for a specific platform. In this example, we'll implement for Android:

public class TealeafAndroid : ITealeaf {

...

public void LogClickEvent(Xamarin.Forms.View view) {

var renderer = Platform.CreateRendererWithContext(view, MainActivity);

Platform.SetRenderer(view, renderer);

var androidView = renderer.View;

Tealeaf.LogEvent(renderer.View);

}

...

}
/ 

To complete the operation, the implementation calls the Xamarin wrapper API.

Note:¬†This example assumes the view¬†belongs to¬†MainActivity, but in most scenarios it can belong to any available Activity in the application. Using the current Activity is usually a good estimate of where the clicked view is located, and there are several libraries available for installation on Nuget (Xamarin’s library marketplace) that can provide easy access to it. For example. CurrentActivity.

Instrumenting Xamarin wrappers for Tealeaf

To instrument Xamarin wrappers for Tealeaf, follow these steps: 

Step-by-step

  1. Create separate wrappers (.dll library) for the Tealeaf SDK and the EOCore library and then add them to your Xamarin project.

    Start by obtaining the Android and iOS wrappers for the Tealeaf SDK and the EOCore library. You can obtain the wrappers from the following locations or manually create them from an existing source. The process differs for Android and iOS. 

    Tealeaf SDK

    EOCore

    Manually creating the Android wrappers

    To manually create the Android wrapper, follow these steps:

    1. Open the tealeafMod or EOCore folder in Android Studio and build the project. This step creates a new .aar file in build/outputs/aar.
    2. Create a new Android Bindings Library project in Visual Studio. Under Jars, add the .aar file that was created in step 1. If you are asked to choose how to add the file, choose Copy the file to the directory.
    3. Build the project. This step generates a new .dll file, which wraps the APIs in the .aar file to C# that can then be added to a Xamarin project. You might see an error indicating a DoInBackgroundmethod was left unimplemented for some classes in the output files. You can add the following code to the classes as a workaround and rebuild successfully:

      protected override Java.Lang.Object DoInBackground (params Java.Lang.Object[] @params) { return null; }
      
    4. Open or create a Xamarin project in Visual Studio. Under Dependencies > Assemblies (Xamarin.Forms) or References (Xamarin Native), add the .dll file that you built in step 3. 

      Note: You must create separate wrappers for Tealeaf and EOCore. You cannot map multiple .aar files to the same .dll library. For details, see https://bugzilla.xamarin.com/show_bug.cgi?id=44573.

    Manually creating the iOS wrappers

    TBS

  2. Instrument the wrappers.

    To instrument the wrappers, you follow a process that is similar to the native Tealeaf SDK and the React Native wrapper. See the sample app for an example.

    For a quicker instrumentation, you can clone the sample app repository and copy the ITealeaf.cs and TealeafAndroid.cs files to your project. Next, initialize the library in your Xamarin project in your MainActivity.cs file:

    tealeafAndroid = new TealeafAndroid();
    
    
    

     

    Note The sample app project relies on a plugin to retrieve the current Activity. For details, see the Xamarin wrapper design example.

     

    Sample app
    The Tealeaf sample app uses the FormsGallery sample app from Xamarin to instrument the Xamarin wrapper.

Join The Discussion

Your email address will not be published. Required fields are marked *