问题
In WP application we need to provide user option to lock app with password.
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?
回答1:
I writed own solution, but may be it is not so elegant as it could be.
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().
So we need to do 6 steps:
You should choose where to save IsAppCurrentlyLocked (bool flag), AppLockPassword (string) and IsUserEnabledAppLockWithPassword (bool flag). I had chosen IsolatedStorageSettings
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 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... } }In App class find Application_Deactivated (to handle app minimizing (windows button)) and Application_Closing (to handle when user closing app) methods, we should mark app as locked if user enabled this option when this events happens
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(); }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
回答2:
You should add the check in the Application_Launching and Application_Activated events.
The launching event for when the app is first opened and the activated one for when the user returns to the app after having left to do something else.
Have these events both set a flag and have the base page that all your pages inherit from check for this flag when navigated to. The check should be for if the flag is set, if it is, show the login prompt and then clear the flag after successful password entry.
This approach will handle FAS, FAR & deep linking, in addition to starting the app normally.
Beware Some choosers will trigger the activated event when they return to the app. Add extra handling for these as appropriate / if necessary.
回答3:
Why not create a start page where the passwords is entered?
For instances you have your MainPage.xaml, create a InsertPasswordPage.xaml reference it on WMAppManifest as the start page:
<DefaultTask Name="_default" NavigationPage="InsertPasswordPage.xaml" />
And insert all the password logic on the InsertPasswordPage.xaml, when the user successfully logins just navigate to your main page ;)
EDIT: As Gambit said if the user pressed the back button he will return to the insert password page, but you can solve this by removing from the backstack the page after the user logged in.
来源:https://stackoverflow.com/questions/15476449/lock-app-with-password