Reputation: 1156
I've seen some old solutions to getting the Android Context using Xamarin.Forms. These older solution suggest the use of Xamarin.Forms.Forms.Context
but due to the ever changing Xamarin.Forms platform this no longer works.
In my example I have used the DependencyService.Get<Interface>()
from my Shared Code project to use a platform specific implementation of the Interface
class.
The use of Android Context is needed a ton when using platform specific functionality using Xamarin.Andriod.
So how do we get the global information interface for an Android Context?
Upvotes: 19
Views: 27815
Reputation: 1156
using Android.Content;
included at the top.Android.App.Application.Context
.That's it!
using Android.Content;
[assembly: Xamarin.Forms.Dependency(typeof(DroidClass))]
namespace App.Droid
{
public class DroidClass : Interface
{
Context context = Android.App.Application.Context;
public DroidClass()
{
Toast.MakeText(context, "Grabbed Context!", ToastLength.Long).Show();
}
}
}
Upvotes: 52