Reputation:
I'm trying to get access to the device lockscreen in a xamarin forms pcl project. I have heard about third-party components such as Lockscreen and Passcode but i dont know how to go about it since this is a xamarin forms app not xamarin.android. How would i implement let's say Lockscreen in the android project of my xamarin forms application?
Upvotes: 2
Views: 4470
Reputation: 1961
One way to call Locker.Activate()
within Xamarin Forms is to use a dependency service. In your Xamarin.Forms Project, you can add the following code to make the call to the dependency service:
switch (Device.RuntimePlatform)
{
case Device.Android:
DependencyService.Get<IAndroidMethods>().activateLock();
break;
case Device.iOS:
DependencyService.Get<IAppleMethods>().activateLock();
break;
}
Then in your Xamarin.Forms project, create two interfaces: IAppleMethods
and IAndroidMethods
that contain the activateLock()
method.
I'll show the implementation for IAndroidMethods
:
public interface IAndroidMethods
{
void activateLock();
}
Then, in your iOS and Android projects, create classes that implement the interfaces you just created. This would be your android class:
//add appropriate using declarations
[assembly: Xamarin.Forms.Dependency(typeof(AndroidMethods))]
namespace <YourApp>.Droid
{
public class AndroidMethods : IAndroidMethods
{
public void activateLock()
{
Locker.Activate(Xamarin.Forms.Forms.Context); //Not sure about any other parameters you need to pass in
}
}
}
Upvotes: 0