[英]How to perform some actions before letting user to do anything?
I need to perform some actions before application actually started. 在应用程序实际启动之前,我需要执行一些操作。
I tried this but can't reach breakpoint: 我尝试了这个,但无法达到断点:
public class MyApplication : Android.App.Application {
public MyApplication(IntPtr javaReference, JniHandleOwnership transfer)
: base(javaReference, transfer) { }
public override void OnCreate() {
base.OnCreate();
int test = 1; //breakpoint
}
}
Am I doing something wrong? 难道我做错了什么? Or here is some debugger problem? 还是这里有一些调试器问题?
In Java you'd have to set the android:name
attribute of the application
tag in the AndroidManifest.xml
file to .MyApplication
. 在Java中,您必须将AndroidManifest.xml
文件中application
标记的android:name
属性设置为.MyApplication
。 Presume it's similar in Xaramin: 假设它与Xaramin类似:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:name=".MyApplication"
>
The Xamarin.Android
way of add your own Android Application class: Xamarin.Android
添加您自己的Android Application类的方式:
Name
: 1)具体的班级Name
: [Application(Name = "com.sushihangover.MyAndroidAppClass")]
public class MyApplication : Application
{
public MyApplication(IntPtr handle, JniHandleOwnership transfer) : base(handle,transfer) { }
public override void OnCreate()
{
base.OnCreate();
Log.Debug("SO", "App OnCreate");
}
}
Add this fully qualified name as a android:name=
attribute: 将此完全限定名称添加为android:name=
属性:
~~~
<application android:name="com.sushihangover.MyAndroidAppClass" android:allowBackup="true" android:icon="@mipmap/icon" android:label="@string/app_name">
</application>
~~~
The key item here is to "Register" your custom Application class by using the [Android.App.Application]
attribute. 此处的关键项是使用[Android.App.Application]
属性“注册”您的自定义Application类。 Secondly you need to provide an OnCreate
override to ensure the custom application class's constructor is invoked. 其次,您需要提供一个OnCreate
重写,以确保调用自定义应用程序类的构造函数。 Otherwise without the [Application]
attribute, we do not register the custom <application>
element in the AndroidManifest.xml
. 否则,如果没有[Application]
属性,则不会在AndroidManifest.xml
注册自定义<application>
元素。 Rather we use the default android.app.Application
instead: 而是使用默认的android.app.Application
代替:
<application android:label="App6" android:name="android.app.Application" android:allowBackup="true" android:icon="@drawable/icon" android:debuggable="true">
Thus if we register using the [Application]
attribute, we then will see our custom application class used instead: 因此,如果我们使用[Application]
属性进行注册,则会看到我们使用的自定义应用程序类:
<application android:label="App6" android:name="md50033386ba710bcf156abf7e9c48d30ef.MyApplication" android:allowBackup="true" android:icon="@drawable/icon" android:debuggable="true">
Here's a complete working example of this: 这是一个完整的工作示例:
[Application] //Name is typically a good idea as well
public class MyApplication : Application
{
public MyApplication(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{ }
public override void OnCreate()
{
base.OnCreate();
int myInt = 1;
}
}
Without a name defined in the comment above, you'll get a md5 hashed named instead. 如果没有在上面的注释中定义名称,您将得到一个md5哈希命名的名称。
There's a few notes in our Architecture docs that might be beneficial here as well: 我们的体系结构文档中有一些注释可能在这里也很有用:
https://developer.xamarin.com/guides/android/under_the_hood/architecture/#Application_Startup https://developer.xamarin.com/guides/android/under_the_hood/architecture/#Application_Startup
It may be the debugger problem. 可能是调试器问题。 In onCreate(), try doing something that you can see in your activity, for example setting text in TextView. 在onCreate()中,尝试做一些您可以在活动中看到的操作,例如在TextView中设置文本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.