这个完全相同的代码在另一个解决方案中工作正常,但在这个解决方案中没有,所以我怀疑它是一个配置问题。
我的解决方案是: - XamForms PCL - Android项目 - PCL库(与INativeMethods合作) - Android库(NativeMethods实现INativeMethods)
这是我在PCL库中的界面:
namespace DreamzLibs
{
public interface INativeMethods
{
}
}
这是Android库中的实现:
[assembly: Dependency(typeof(NativeMethods))]
namespace DreamzLibs.Droid
{
public class NativeMethods : Java.Lang.Object, INativeMethods
{
}
}
这是他们要求的地方:
public static Lazy<INativeMethods> NativeMethods = new Lazy<INativeMethods>(() => DependencyService.Get<INativeMethods> ());
这是我得到的错误:
[MonoDroid] UNHANDLED EXCEPTION:
[MonoDroid] System.TypeInitializationException: An exception was thrown by the type initializer for Boats.App ---> System.TypeLoadException: Could not load type 'DreamzLibs.Droid.NativeMethods' from assembly 'DreamzLibs.Droid, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'.
[MonoDroid] at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00023] in <filename unknown>:0
[MonoDroid] at System.Activator.CreateInstance (System.Type type, Boolean nonPublic) [0x000af] in <filename unknown>:0
[MonoDroid] at System.Activator.CreateInstance (System.Type type) [0x00000] in <filename unknown>:0
[MonoDroid] at Xamarin.Forms.DependencyService.Get[INativeMethods] (DependencyFetchTarget fetchTarget) [0x00076] in <filename unknown>:0
[MonoDroid] at DreamzLibs.Utils.<.cctor>b__2 () [0x00000] in e:\Code\Boats\DreamzLibs\Utils.cs:25
[MonoDroid] at System.Lazy`1[DreamzLibs.INativeMethods].CreateValue () [0x00075] in <filename unknown>:0
[MonoDroid] --- End of inner exception stack trace ---
[MonoDroid] at Boats.Droid.MainActivity.OnCreate (Android.OS.Bundle) [0x0005d] in e:\Code\Boats\Droid\MainActivity.cs:37
[MonoDroid] at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) <IL 0x00013, 0x0008e>
[MonoDroid] at (wrapper dynamic-method) object.57e490e4-6d7f-42bb-8d74-c204b5bfcb8e (intptr,intptr,intptr) <IL 0x00017, 0x00027>
[art] JNI RegisterNativeMethods: attempt to register 0 native methods for md52ce486a14f4bcd95899665e9d932190b.JavaProxyThrowable
答案 0 :(得分:1)
必须重新安装软件包“Microsoft.Bcl”来解决这个问题..