Luis Rodriguez Luis Rodriguez - 26 days ago 26
iOS Question

Xamarin.Forms - Context?

I don't know to explain this, I hope you understand. I need to know is if there is something similar to ApplicationContext (Xamarin.Android) but in iOS, I am using Xamarin.Forms, and I have a class called MyApp (this in portable) where you have some classes that obtain data needed to run the application, I do this in Xamarin.Android:


variable var = ((MyApp) ApplicationContext);


and with this I get the data.

Answer

Android itself uses "Context" for activities and applications and the Application Context is from Android.Content.Context. The difference is that Application Context ties itself to the lifecycle of the Application. The Context of an Activity ties itself to the lifecycle of the Activity.

iOS itself uses the AppDelegate and would be similar to Android's ApplicationContext. One main difference is that Android's Context contains information that is sometimes required to complete Actions. This is one reason the Forms.Context is exposed in Xamarin.Forms Android custom renderers because Xamarin.Forms manages the Android activity.

I don't know what you capture from the Application Context or what you are trying to accomplish with that data. In iOS, most of the application level data lives in the UIApplication class. The UIApplication encapsulates the central administrative properties of an application running on iOS and is most likely what you are looking for. I would recommend reviewing the Xamarin.iOS.UIKit.UIApplication documentation to see if it contains the data you need.

If you are looking for the lifecycle events of the iOS application, than you would want to use the available override in the AppDelegate. Here is Xamarin's documentation on the lifecycle events available.

I hope this helps!

Disclosure: I work for Xamarin/Microsoft