0

I am using MvvmCross framework and I want to invoke a method defined in Android project from Core project. I tried This solution but I am getting the following error

Unhandled Exception: System.InvalidOperationException: You MUST call Xamarin.Forms.Init(); prior to using it. occurred

As i am not using Xamarin Forms so I know this will not work. Is there any workaround or any other way to accomplish this?

Brandon Minnick
  • 13,342
  • 15
  • 65
  • 123
Manoj Pathak
  • 209
  • 1
  • 5
  • 15

2 Answers2

0

DependencyService is a feature in Xamarin Forms. If you are using MvvmCross, you should look into dependency injection from MvvmCross. https://www.mvvmcross.com/documentation/fundamentals/dependency-injection

lowleetak
  • 1,362
  • 1
  • 10
  • 19
  • Thank you **lowleetak** for your answer but if I use dependency injection then I get into another issue i.e. how to reference a UI class in App.cs of PCL? Because this would result in circular dependency and VS won't allow it. It would be great if you could provide a small example as a reference. – Manoj Pathak Sep 03 '17 at 23:56
  • You will need to be more specific about what you try to achieve in order for us to provide more detail answer – lowleetak Sep 04 '17 at 14:56
0

Finally, found the answer. Here are the steps

I - Get the nuget package "Xamarin.Forms.Labs" in your android (UI) project, apparently now it is Scorchio.NinjaCoder.Xamarin.Forms.Labs

II - Use the following code in SetUp.cs as shown below

using Android.Content;
using MvvmCross.Core.ViewModels;
using MvvmCross.Droid.Platform;
using Xamarin.Forms.Labs.Services;

namespace SomeProject.UI.Droid
{
    public class Setup : MvxAndroidSetup
    {

        public Setup(Context applicationContext) : base(applicationContext)
        {
            var resolverContainer = new SimpleContainer();
            resolverContainer.Register<IViewMethodCallService>(t => new ViewMethodCallService());
            Resolver.SetResolver(resolverContainer.GetResolver());
        }

        protected override IMvxApplication CreateApp()
        {
            return new App();
        }
    }
}

Where "IViewMethodCallService" is the interface, having the signature of the method e.g. TestMethod(), in your PCL project and "ViewMethodCallService.cs" is implementation of that interface in UI or Android project.

III - Now create an object of the interface "IViewMethodCallService" as shown below

IViewMethodCallService callMethod= Resolver.Resolve<IViewMethodCallService>();
callMethod.TestMethod();

The "ViewMethodCallService.cs" looks like this

using Android.Util;

[assembly: Xamarin.Forms.Dependency(typeof(ViewMethodCallService))]
namespace SomeProject.UI.Droid
{    
    public class ViewMethodCallService : Java.Lang.Object, IViewMethodCallService
    {
        public ViewMethodCallService()
        {

        }

        public void TestMethod()
        {
            Log.Info("Hurrayyyyyyyyyyyyyyyyyyyyyyyyyy", "And I am calling this service");
        }
    }
}

I got this answer from this question and the link mentioned in question if you wish to do more research. Hope this help someone.

Manoj Pathak
  • 209
  • 1
  • 5
  • 15