android - 如何创建默认应用程序实例的自定义实例?

标签 android xamarin.android xamarin

我想创建自己的应用程序对象,类似于以下内容:

namespace MyNameSpace.Android
{
    public class MyApplication : Application
    {
        public override void OnCreate()
        {
            base.OnCreate();

            // do application specific things here
        }
    }
}

如何让应用使用 MyApplication 对象?我的理解是我必须在 list 中指定这一点,但我无法让它发挥作用。该应用程序部署正常,但之后无法运行。

这是我的 list :

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
          android:installLocation="preferExternal" 
          package="abc.mypackage">
    <uses-sdk android:targetSdkVersion="14" />
    <application android:label="MyMobileApp" 
               android:theme="@style/Theme.FullScreen" 
               android:screenOrientation="portrait" 
               android:icon="@drawable/Logo" 
               android:name=".MyNameSpace.Android.MyApplication"/>
</manifest>

请注意,我已经尝试了 android:name=".MyNameSpace.Android.MyApplication" 属性中大小写字母的所有变体,包括带或不带前导 字符。

如果我删除该行,应用程序将正常运行。

最佳答案

解决方案是将以下类属性和构造函数添加到我的自定义应用程序类中:

namespace MyNameSpace.Android
{
    [Application(Debuggable = true, ManageSpaceActivity = typeof(MyMainActivity))]
    public class MyApplication : Application
    {

        public MyApplication(IntPtr javaReference, JniHandleOwnership transfer)
            : base(javaReference, transfer)
        {
        }

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

            // do application specific things here
        }

    }
}

关于android - 如何创建默认应用程序实例的自定义实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18957144/

相关文章:

android - 如何在 libgdx 中根据不同的屏幕分辨率缩放图像

java - Android - 应用程序不断因错误而崩溃 - 我认为这与首选项 fragment 有关

java - Android 非静态 block 中的 init 方法

android - 如何在 Android 中将代码执行延迟 X 时间

java - Xamarin Studio 中的 Android - 错误 : package com. google.android.gms.tagmanager 不存在

visual-studio - Xamarin.Android.Common.targets : error : Could not find a part of the path '/platforms'

android - 如何在 Chromecast 接收器上流式传输 SHOUTcast radio 流

Xamarin 更新后 Xamarin.Android 调试构建在 Visual Studio 2015 中失败

event-handling - 如何在不实例化事件中每个实例的情况下处理子类 View 事件?

C# 向 .net 枚举添加额外值