gpt4 book ai didi

android - 如何在 Xamarin.Android 中注册我自己的应用程序子类?

转载 作者:IT老高 更新时间:2023-10-28 23:02:57 26 4
gpt4 key购买 nike

我有

public class MyApp : Application

在 Java 中,我会在 list 中添加一行并将我的应用程序的 namespace 和名称传递给它:

<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

最佳答案

找到了。文档已过时。您将需要一个带有 两个 参数的特殊 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 hooks into this by adding a mono.MonoRuntimeProvider ContentProvider to AndroidManifest.xml during the build process. The mono.MonoRuntimeProvider.attachInfo() method is responsible for loading the Mono runtime into the process. Any attempts to use Mono prior to this point will fail. ( Note: This is why types which subclass Android.App.Application need to provide an (IntPtr, JniHandleOwnership) constructor, as the Application instance is created before Mono can be initialized.)

关于android - 如何在 Xamarin.Android 中注册我自己的应用程序子类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21427981/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com