问题描述
我想扩展Android应用程序,并且想重写oncreate方法.但是由于某种原因,它非常可怕...仅当我删除[Application]时,我才能运行它,但是根据此链接,我们必须添加[Application]
I want to extend the Android application and I want to override oncreate method.But for some reason its been very terrible...Only if I remove [Application] I am able to run it but according to this link we have to add [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上扩展应用程序类的正确方法是什么
Can anybody clarify me what is the rightway to extend application class in xamarin android
更新
如果我不删除[应用程序],我将得到以下错误
If I don't remove [application] I am exactly getting the below error
如果我的应用程序可以编译,但是会在运行时错误之后抛出
If I application then it compiles but it throws following runtime error
这是我的清单文件
推荐答案
在 AssemblyInfo.cs
In AssemblyInfo.cs
评论
#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 ...
}
}
基本上,您已经在两个地方定义了它,这就是为什么发生此问题的原因.在AssemblyInfo.cs中注释掉并添加扩展类即可解决
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
仅供参考:
清单还可以
这篇关于如何在Xamarin Android中扩展应用程序类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!