终于找到了答案。以下是步骤
I - 在你的 android (UI) 项目中获取 nuget 包“Xamarin.Forms.Labs”,显然现在它是 Scorchio.NinjaCoder.Xamarin.Forms.Labs
II - 在SetUp.cs中使用如下代码如下所示
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();
}
}
}
其中“IViewMethodCallService”是接口,具有方法的签名,例如TestMethod(),在您的 PCL 项目中,“ViewMethodCallService.cs”是 UI 或 Android 项目中该接口的实现。
III - 现在创建接口“IViewMethodCallService”的对象,如下所示
IViewMethodCallService callMethod= Resolver.Resolve<IViewMethodCallService>();
callMethod.TestMethod();
“ViewMethodCallService.cs”看起来像这样
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");
}
}
}
我从this question 得到了这个答案,如果您想进行更多研究,请参考相关链接。希望这对某人有所帮助。