如何在 xamarin android 中扩展应用程序 class

How to extend application class in xamarin android

我想扩展 Android 应用程序并且我想覆盖 oncreate 方法。 但由于某种原因,它非常糟糕......只有我删除 [Application] 我才能 运行 它但是根据这个 link 我们必须添加 [Application]

[Application]
public class MainApp : Application
{
public MainApp(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}

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

谁能告诉我在 xamarin android

中扩展应用程序 class 的正确方法是什么?

更新

如果我不删除 [application],我就会收到以下错误

/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Common.targets: Error: Error executing task GenerateJavaStubs: Application cannot have both a type with an [Application] attribute and an [assembly:Application] attribute. (myapp.Droid)

如果我申请然后它编译但它抛出以下运行时间错误

[AndroidRuntime] Shutting down VM [AndroidRuntime] FATAL EXCEPTION: main [AndroidRuntime] Process: com.test.myapp, PID: 6524 [AndroidRuntime] java.lang.RuntimeException: Unable to instantiate application com.test.myapp.MainApp: java.lang.ClassNotFoundException: Didn't find class "com.test.myapp.MainApp" on path: DexPathList[[zip file "/data/app/com.test.myapp-1/base.apk"],nativeLibraryDirectories=[/data/app/com.test.myapp-1/lib/arm, /data/app/com.test.myapp-1/base.apk!/lib/armeabi-v7a, /system/lib, /vendor/lib]] [AndroidRuntime] at android.app.LoadedApk.makeApplication(LoadedApk.java:802) [AndroidRuntime] at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5377) [AndroidRuntime] at android.app.ActivityThread.-wrap2(ActivityThread.java) [AndroidRuntime] at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1545) [AndroidRuntime] at android.os.Handler.dispatchMessage(Handler.java:102) [AndroidRuntime] at android.os.Looper.loop(Looper.java:154) [AndroidRuntime] at android.app.ActivityThread.main(ActivityThread.java:6119) [AndroidRuntime] at java.lang.reflect.Method.invoke(Native Method) [AndroidRuntime] at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) [AndroidRuntime] at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) [AndroidRuntime] Caused by: java.lang.ClassNotFoundException: Didn't find class "com.test.myapp.MainApp" on path: DexPathList[[zip file "/data/app/com.test.myapp-1/base.apk"],nativeLibraryDirectories=[/data/app/com.test.myapp-1/lib/arm, /data/app/com.test.myapp-1/base.apk!/lib/armeabi-v7a, /system/lib, /vendor/lib]] [AndroidRuntime] at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56) [AndroidRuntime] at java.lang.ClassLoader.loadClass(ClassLoader.java:380) [AndroidRuntime] at java.lang.ClassLoader.loadClass(ClassLoader.java:312) [AndroidRuntime] at android.app.Instrumentation.newApplication(Instrumentation.java:992) [AndroidRuntime] at android.app.LoadedApk.makeApplication(LoadedApk.java:796) [AndroidRuntime] ... 9 more

这是我的清单file

  1. 将 ApplicationAttribute [Application] 应用到您的应用程序子class以在清单

    中注册此class
  2. 添加Java/JNI构造函数

    • .ctor(System.IntPtr, Android.Runtime.JniHandleOwnership)
    • 如果您不添加这个,当运行时尝试实例化 Java 包装器时,您将收到一个错误....
  3. 覆盖 OnCreate 方法

    • 这避免了:[art] JNI RegisterNativeMethods: attempt to register 0 native methods 因此您的应用程序 subclass 没有被实例化。

示例:

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

    public override void OnCreate()
    {
        base.OnCreate();
    }
}

AssemblyInfo.cs

注释掉

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

并将其移到您的应用程序之上

#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 ...
}
}

基本上你已经在两个地方定义了它,这就是为什么会出现这个问题。 在 AssemblyInfo.cs 中注释掉并在扩展 class 中添加将得到

仅供参考:

清单没问题