我有
public class MyApp : Application
在Java中,我会在清单中添加一行,并将其命名空间和名称传递给我:
<application android:icon="@drawable/icon" android:label="@string/app_name"
android:name="com.devahead.extendingandroidapplication.MyApplication">
在Xamarin中,有[Application]
属性,但文档指出Name
成员是not supported。那么如何让我的子类注册?在哪里应用属性?
如果我将属性添加到我的子类,我得到:
System.NotSupportedException: Unable to activate instance of type TestClient_Android.MyApplication from native handle 14d00019
答案 0 :(得分:53)
找到它。文档已过时。您将需要一个带有两个参数和的特殊c'tor,您必须添加[Application]
属性:
[Application]
public class MyApplication : Application
{
public MyApplication(IntPtr handle, JniHandleOwnership ownerShip) : base(handle, ownerShip)
{
}
}
编辑:此外,似乎必须覆盖OnCreate()。如果您只有 构造函数,则不会调用它。
public override void OnCreate()
{
// If OnCreate is overridden, the overridden c'tor will also be called.
base.OnCreate();
}
2015年11月编辑:这里是Xamarin文档的链接,其中解释了why this behavior exists。
... Xamarin.Android通过添加一个来挂钩 mono.MonoRuntimeProvider ContentProvider到AndroidManifest.xml期间 构建过程。 mono.MonoRuntimeProvider.attachInfo()方法是 负责将Mono运行时加载到进程中。任何 在此之前尝试使用Mono将失败。 (注意:这是 为什么子类Android.App.Application需要提供的类型 (IntPtr,JniHandleOwnership)构造函数,作为Application实例 在Mono可以初始化之前创建。)