Xamarin Forms - Disable auto-lock when the app is open

后端 未结 5 655
走了就别回头了
走了就别回头了 2020-12-18 17:46

I want to disable auto-lock when my app is open. How can I do that?

5条回答
  •  失恋的感觉
    2020-12-18 18:07

    We can achieve it by using Xamarin.Essentials plugin. Install it on solution level(While installing select include prerelease checkbox).

    In your MainPage write this code

    public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            ToggleScreenLock();
        }
        public void ToggleScreenLock()
        {
            if (!ScreenLock.IsActive)
                ScreenLock.RequestActive();
            else
                ScreenLock.RequestRelease();
        }
    }
    

    In Android project MainActivity add this line

    Xamarin.Essentials.Platform.Init(this, savedInstanceState);
    

    before calling LoadApplication(new App());. For more information visit Microsoft docs.

    This is working throughout the app. To install plugin refer below screenshot -

提交回复
热议问题