使用密码锁定应用程序 [英] Lock app with password

查看:23
本文介绍了使用密码锁定应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 WP 应用程序中,我们需要提供用户选项以使用密码锁定应用程序.

In WP application we need to provide user option to lock app with password.

据我了解 WP 应用生命周期,我需要将导航放在 App.Application_Activated、App.Application_Deactivated 和起始页中的 LockPage,但我不能在 App 类中使用 NavigationService...

As I understand WP app lifecycle, I need to put navigation to LockPage in App.Application_Activated, App.Application_Deactivated and start page, but I can not use NavigationService in App class...

我不想在其他页面中放置导航代码锁定页面,或者没有其他选项?

I do not want to put navigation code to lock page in each other pages, or there is no other options?

推荐答案

我写了自己的解决方案,但可能不是那么优雅.

I writed own solution, but may be it is not so elegant as it could be.

应用锁定逻辑:用户使用密码启用应用锁定,我们处理应用类中的 Application_Deactivated 和 Application_Closing 事件,如果用户启用此选项,则将应用标记为锁定.然后,在每个页面上,我们应该检查:应用程序当前是否被锁定,如果是,我们应该导航到 AppLockedWithPasswordPage.在 AppLockedWithPasswordPage 上,我们需要检查用户的密码,如果正确则调用 NavigationService.GoBack().

App locking logic: User enable app locking with password, we handling Application_Deactivated and Application_Closing events in App class and marking app as locked if user enabled this option. Then, on each page we should put check: is app currently locked and if it is, we should navigate to AppLockedWithPasswordPage. On AppLockedWithPasswordPage we need to check user`s password, if it is correct call NavigationService.GoBack().

所以我们需要做 6 个步骤:

So we need to do 6 steps:

  1. 您应该选择保存IsAppCurrentlyLocked(布尔标记)、AppLockPassword(字符串)和IsUserEnabledAppLockWithPassword(布尔标记)的位置.我选择了IsolatedStorageSettings

  1. You should choose where to save IsAppCurrentlyLocked (bool flag), AppLockPassword (string) and IsUserEnabledAppLockWithPassword (bool flag). I had chosen IsolatedStorageSettings

创建 AppLockedWithPassword 页面,您需要在其中显示 TextBox 和 Button,当然不要忘记为用户提供选项以通过删除应用程序数据来重置 AppLock

Create AppLockedWithPassword page, where you need to show TextBox and Button, do not forget to provide option for user to reset AppLock of course with deleting app data

AppLockedWithPasswordPage 应该阻止 BackButton 导航,因此阻止它:

AppLockedWithPasswordPage should prevent BackButton navigation, so preventing it:

// AppLockedWithPasswordPage
protected override void OnBackKeyPress(CancelEventArgs e)
{
    // Preventing back key navigation
    e.Cancel = true;
}

  • 点击按钮检查密码

  • Check password on button click

    // AppLockedWithPasswordPage
    private void UnlockAppButton_Click(object sender, RoutedEventArgs e)
    {
        if (PasswordBox.Password.Equals(IsolatedStorageSettings["AppLockPassword"]))
        {
            NavigationService.GoBack();
        }
        else
        {
            // Say user, that password incorrect, etc...
        }
    }
    

  • 在 App 类中找到 Application_Deactivated(处理应用最小化(窗口按钮))和 Application_Closing(当用户关闭应用时处理)方法,如果用户在此事件发生时启用了此选项,我们应该将应用标记为锁定

    private void SetIsAppCurrentlyLockedFlagIfUserEnabledAppLocking()
    {
        if ((bool)IsolatedStorageSettings["IsUserEnabledAppLockWithPassword"])
        {
            IsolatedStorageSettings["IsAppCurrentlyLocked"] = true;
        }
    }
    
    private void Application_Deactivated(object sender, DeactivatedEventArgs e)
    {
        SetIsAppCurrentlyLockedFlagIfUserEnabledAppLocking();
    }
    
    private void Application_Closing(object sender, ClosingEventArgs e)
    {
        SetIsAppCurrentlyLockedFlagIfUserEnabledAppLocking();
    }
    

  • 最后一步,在你想要锁定的所有页面上,你应该添加检查 OnNavigatedTo 方法,如果应用程序当前被锁定,它将导航到 AppLockedWithPasswordPage

  • And final step, on all pages you want to lock you should add check in OnNavigatedTo method which will navigate to AppLockedWithPasswordPage if app is currently locked

    // Create some class, like PagesUtils or so on with check method
    private static Uri uriToAppLockedWithPasswordPage = new Uri("pathToAppLockedWithPasswordPage", UriKind.Relative);
    
    public static void NavigateToAppLockedWithPasswordPageIfAppLocked(PhoneApplicationPage page)
    {
        if ((bool)IsolatedStorageSettings["IsAppCurrentlyLocked"])
        {
            page.NavigationService.Navigate(uriToAppLockedWithPasswordPage); 
        }
    }
    
    // In each page you want to lock add
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        PagesUtils.NavigateToAppLockedWithPasswordPageIfAppLocked();
        base.OnNavigatedTo();
    }
    

  • 附言当然真正的代码要好得多,这只是简单的例子,希望对你有帮助

    P.S. of course real code is much better, this is just simple example, I hope it will help you

    这篇关于使用密码锁定应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆