Xamarin

Last updated:

Prisma Campaigns provides a Xamarin.Forms shared library for use with C#, as well as specific libraries for Android and iOS. Successful integrations require implementing several references correctly (both at a shared level and for Android or iOS projects) as outlined in this document, particularly in the Library References section below.

First steps

To begin, add the PrismaSDK namespace at the top of the application’s main XAML file (usually App.xaml.cs) and invoke the Client.load() static method with the following parameters:

Load(String server, String port, String appToken, String customer, String protocol)

where

  • param String server and param String port: server name and port where the Prisma service resides and is listening.
  • param String appToken: access token for the current application.
  • param String customer: customer ID (if available). Otherwise, pass an empty string ("").
  • param String protocol: Connection protocol (https by default).
using PrismaSDK;
[assembly: XamlCompilation (XamlCompilationOptions.Compile)]
namespace SDKsample
{
    public partial class App: Application
    {
        public App ()
        {
            InitializeComponent();
            Client c = Client.Load(localhost, 8080, 8e2626da-4ee1-45c7-b269-50df530dbc6e, 1234, HTTP);
            MainPage = new MainPage();
        }

Since the Client class is a singleton, it will return the same instance of the object whenever the application calls it through the shared static attribute as var c = Client.shared;.

Banners

This section will walk you through the steps to display dynamic content in a banner.

First, add an SDK assembly reference to the header of the integration page (xmlns:Prisma="clr-namespace:PrismaSDK;assembly=PrismaSDK") and insert the following lines within ContentPage:

<ContentPage xmlns= http://xamarin.com/schemas/2014/forms
             xmlns: x= http://schemas.microsoft.com/winfx/2009/xaml
             xmlns: local=clr-namespace: SDKsample
             xmlns: Prisma=clr-namespace: PrismaSDK; assembly=PrismaSDK
             x: Class=SDKsample.MainPage>

Second, include a Placeholder control within the layout with an ID for local reference (phid in this case) and a PlaceholderName attribute (HomePublicBanner, for example):

<StackLayout>
    <Prisma: Placeholder x: Name=phid PlaceholderName=HomePublicBanner></Prisma: Placeholder>
</StackLayout>

Finally, implement the OnAppearing method in the page where you defined the control:

namespace SDKsample
{
    public partial class MainPage: ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
        }
        protected override void OnAppearing()
        {
            List<Placeholder> l = new List<Placeholder> ();
            l.Add(phid);
            Client.shared.syncView(MyPage, l, null);
        }
    }
}

where MyPage represents the page to sync and l a list of placeholders.

Custom banners

You can also create banners from the application code independently of the SDK. To do this, you can utilize the handleActionparameter of the syncView method. Under this scenario, the interface callback is invoked when requesting the banners for the given page:

Action<Object> handler = r => {
                Debug.WriteLine (Initialized);
                Dictionary<String, PlaceholderContent> result = (Dictionary<string, PlaceholderContent>) r;
                PlaceholderContent content = result[MainGeneral];
                var size = content.banners.Count;
                Debug.WriteLine (Total banners found for placeholder: + size);
};
Client.shared.syncView(MyPage, l, handler);

To retrieve the views from the content of placeholder:

let banner = content.banners[0]
let v = banner.GetView()

or the content of an HTML banner:

HtmlBannerView h = (HtmlBannerView)banner;
var strBanner = h.getHTMLContent();

To initialize the funnel from the banner:

let funnel = banner.GetFunnel()
funnel.start()

or to dismiss a campaign banner:

let funnel = banner.GetFunnel ()
funnel.dismiss()

Library references

The shared project needs to reference the Newtonsoft.Json and PCLStorage NuGet packages, as well as the PrismaSDK.dll library.

The PrismaSDK.Android and PrismaSDK.IOS assemblies contain custom renderers of different controls, so you need to include one or the other in the corresponding application.

  • For Android, the manifest must have an automatic target. When initializing the main Android activity, you will need to invoke the PrismaSDK.Droid.AndroidComponents.init() as shown below:
namespace SDKsample.Droid
{
    [Activity(Label = "SDKsample", Icon = "@mipmap/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
    public class MainActivity global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
    {
        protected override void OnCreate(Bundle bundle)
        {
            PrismaSDK.Droid.AndroidComponents.init();
            ...
  • In iOS, invoke the PrismaSDK.IOS.IOSComponents.init() method when initializing the main iOS activity:
namespace SDKsample.iOS
{
    public class Application
    {
        // This is the main entry point of the application.
        static void Main (string[] args)
        {
            // if you want to uses to different Application Delegate class from AppDelegate
            // you can specify it here.
            PrismaSDK.IOS.IOSComponents.init ();
            UIApplication.Main (args, null, AppDelegate);
        }

In both platforms -for optimization purposes-, the compiler will delete any controls for which there are no previous references within the assembly.