Mobile Center issue with Xamarin Forms

583 views Asked by At

I've started to use Azure Mobile Center for a Xamarin.Forms app , for the Android piece.

I've added the required Mobile Center SDK calls, but I still cannot see anything in Analytics. As note, I can build and distribute the app properly.

This is how the App() constructor in App.xams.cs looks like :

public App()
{            
     InitializeComponent();            
     MobileCenter.Start(typeof(Analytics), typeof(Crashes));
     MobileCenter.LogLevel = LogLevel.Verbose;
}

and I've also added the configure call in the OnCreate event in MainActivity.cs

protected override void OnCreate(Bundle bundle)
{
     TabLayoutResource = Resource.Layout.tabs;
     ToolbarResource = Resource.Layout.toolbar;

     base.OnCreate(bundle);

     global::Xamarin.Forms.Forms.Init(this, bundle);
     MobileCenter.Configure("my_app_id");
     LoadApplication(new App(new AndroidInitializer()));
}

After a few tests, it seems that Prism affects in a way the MobileCenter class. The App() constructor is not being called, so I've added this to the existing constructor :

public App(IPlatformInitializer initializer = null) : base(initializer) {
        MobileCenter.Start(typeof(Analytics), typeof(Crashes));
    }

but I get an "System.NullReferenceException: Object reference not set to an instance of an object." It seems Crashes and Analytics are not initialized properly. The MobileCenter is a static class, so maybe this affects its initialization.

Any help is appreciated. Thank you,

3

There are 3 answers

2
AudioBubble On BEST ANSWER

Move the call into the OnInitialized method.

0
Isaack Rasmussen On

In my case, it was because I had only installed the Nuget packages(Analytics & Crashes) for the shared project. Installing it for the iOS project as well fixed it.

3
Marius B. On

I'm using in the app Prism to implement MVVM. I had a hunch that this might affects things; I've created a new project, without using Prism, and voila...I can see Analytics...finally!

The issue is in what way Prism affects the MobileCenter. I've updated the original question.