简体   繁体   中英

How to implement lockscreen in Xamarin forms application?

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?

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
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM