android - 为 list 中的 OnCreate 不匹配扩展 Android 应用程序类

标签 android xamarin xamarin.android

Android 应用程序使用 Xamarin,但我认为这适用于非 Xamarin。我在做研究时阅读了这篇文章。

In an Android application, you will notice that the AndroidManifest.xml file contains an entry for which is used to configure various properties of your application: icon, label, theme, etc. Depending on your application's needs, this is sufficient. The application when instantiated will perform the necessary initializations based on the values configured in the manifest, and it will then load the Activity that is defined as the view to load on startup.

In some cases, you may wish to extend the Application class in order to perform custom initialization on application startup, or to maintain global application state (for example, in the case of a game that is tracking score across multiple levels). The example that you have shown above is the case where someone has decided to subclass the Application class and override it's OnCreate method, although they haven't included any custom code in the override method. It looks like their purpose was to create a global static property to always have access to the Activity that is currently loaded.

以前,我不需要扩展应用程序类,所以我只是让我的 list 处理它,就像本文所说的那样。但是,我最近需要扩展应用程序类,以便我可以为应用程序使用 OnCreate 事件。

我添加了一个扩展 Application 的新类,它看起来像这样:

namespace MyApp.Droid
{
     public class MyApp : Application
     {
        public MyApp(IntPtr handle, global::Android.Runtime.JniHandleOwnership transfer)
        :base(handle, transfer)
        {

        }

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

            //Do OnCreate things here.
        }
    }
}

看来仅仅添加这个类是不完整的。第一个问题,我现在还必须修改我的 list 吗?第二个问题,该类可以具有主题、图标等属性……这些属性已经在我的 list 中定义。它们可以保留在 list 中还是我需要将它们作为属性移动到这个新类?

这是 list :

    <application android:allowBackup="true" android:icon="@drawable/mainapplogo" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme">

谢谢!

最佳答案

First question, do I also have to modify my manifest now?

在这个问题中,如果您的意思是仅仅因为您添加了新的应用程序类,是否需要对现有 list 进行任何更改,那么没有这样的要求。

Second question, the class can have attributes for the theme, icon, etc... which are and have been defined in my manifest. Can they stay in the manifest or do I need to move them to this new class as attributes?

Android Manifest 的主题和其他所有内容都将保留在那里,Android Application 类和 AndroidManifest.xml 独立工作,因此添加扩展应用程序类不需要对您的 Manifest 文件进行任何更改,反之亦然。

祝你好运

如有疑问,请随时回复

更新

确保您的应用程序类在类名顶部具有 [Application] 属性

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

public class MyApp: Application
 {

关于android - 为 list 中的 OnCreate 不匹配扩展 Android 应用程序类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54812637/

相关文章:

android - 在库 list 中使用 ${applicationId}

java - Android - EditText-可选择,不可编辑

Android 位图工厂内存不足第二张照片

ios - iOS 中使用 Xamarin 的 "Sign in with Twitter"按钮

Xamarin Android 将 Java.Lang.Enum 映射到 C# Enum

xamarin - 相机预览和 OCR

android - RecyclerView OnClickListener(在整个房车上)

c# - 如何在 ListView 中将网格嵌套到网格中

Android ViewPager非标题页指示器

android - 对于华为设备,isPowerSaveMode() 始终返回 false