Search code examples
xamarin.formsprismmobile-centervisual-studio-app-center

Mobile Center issue with Xamarin Forms


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,


Solution

  • Move the call into the OnInitialized method.