What is [Application] in Mono for Android?

2019-07-23 14:12发布

问题:

I'm new to mono and working through the Mono For Android tutorials. Can anyone point me to some documentation which explains the meaning of the [Application] tag above my classes?... Here's my issue:

I have come accross an issue where I have created two seperate classes App1.cs & App2.cs, both of these extend the Application Class (Public Class App1 : Application).. etc.

There is a tag above this declaration [Application] which seems to be causing deployment issues, although it builds fine. If I comment one of these out, the application builds and deploys fine.

I have searched for info on these tags, but as yet cannot find anything in the search, FAQ's or on google... I guess the square brackes are causing me search problems. Any help would be great...

Thanks

C

回答1:

In Android applications you can subclass the Application class in order to maintain global state in your app. An app can have at most one Application class, and it is shared throughout the app. In order to register your Application class it needs to be registered in AndroidManifest.xml (this applies to any Android component). Mono for Android uses attributes to generate the manifest during compilation, so any class decorated with ApplicationAttribute will generate configuration at build time.

Since only one Application class is allowed, this would explain the problems you are seeing when you try to register two of them. My guess (without having context into what you're trying to do here) is that you probably want to subclass Activity for these classes instead of Application.