How to extend application class in xamarin android

。_饼干妹妹 提交于 2019-12-05 06:58:09

In AssemblyInfo.cs

comment out

     #if DEBUG
    [Application(Debuggable=true)]
    #else
    [Application(Debuggable = false)]
    #endif

and move that on top of your application

#if DEBUG
[Application(Debuggable=true)]
#else
[Application(Debuggable = false)]
#endif
public class MainApp : Application
{
public MainApp(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}

public override void OnCreate()
{
    base.OnCreate();
    //app init ...
}
}

Basically you have defined it two places that's why this problem occurs. Commenting out in AssemblyInfo.cs and adding in the extended class will work out

FYI:

Manifest is ok

  1. Apply the ApplicationAttribute, [Application], to your Application subclass to register this class in the manifest

  2. Add the Java/JNI constructor

    • .ctor(System.IntPtr, Android.Runtime.JniHandleOwnership)
    • You will receive a fault when the runtime tries to instance the Java wrapper if you do not add this....
  3. Override the OnCreate method

    • This avoids: [art] JNI RegisterNativeMethods: attempt to register 0 native methods and thus your application subclass is not instanced.

Example:

[Application]
public class MainApplication : Application
{
    public MainApplication(IntPtr javaReference, Android.Runtime.JniHandleOwnership transfer) : base(javaReference, transfer)
    {
    }

    public override void OnCreate()
    {
        base.OnCreate();
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!