可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I know this has been discussed ad nauseum, but I want to present my case....
My start page in my app is login.xaml. The user logs in, the username and password are authenticated through my WCF service, the username is saved in isolated storage, and then the user is redirected to mainpage.xaml.
When a user starts my app, and they already have a saved username in isolated storage, they are redirected to mainpage.xaml
If the user hits "back" hard button from mainpage.xaml, this redirects to the login screen, which in turn redirects them back to the mainpage.xaml since they already have a saved local username. This is causing my app to fail certification currently since the user cannot hit the "back" button to exit the app from mainpage.xaml.
My instinct here is to override the BackKeyPress in mainpage.xaml and exit the app somehow. By reading the other posts, I can see that this method is not available. My second idea was to somehow store a property in the app.xaml.cs page that would tell the app to exit when the login page is loaded and that property is set to true, but that seems a bit hacky as well....
Any ideas here?
UPDATE SOLUTION USED:
What ended up working was to make the login page a pivot item inside mainpage.xaml. When a user opened the app without being logged in, the currently selected pivot item would change to the login pivot item. If the user opened the app and WAS logged in, the normal pivot item would be selected.
回答1:
I do this by combining the login page and the main page into one page with two grids in a stackpanel and manipulating the visibility of the grids to switch between the UI required for anonymous and authenticated users. This makes the problem go away with no need to diddle the history or fiddle with flags.
With a suitable converter and appropriate bindings of Visibility properties I could probably run the whole show without explicit code, but that would be a good example of the difference between clever and smart - it works so well that sooner or later I'd forget how it works (ok I confess, I did this and forgot how it worked).
回答2:
You should probably change your flow to automatically launch MainPage.xaml, which will redirect to a login page the first time it loads if the user has not logged in yet. Then you set a flag to not auto-redirect to the login page, so if the user hits back to abort logging in, they end up at MainPage.xaml, which can just be all "Please sign in for this to be useful." And you never prevent back button from MainPage.xaml.
回答3:
So your issue (as I understand it) is:
- LOGIN.XAML - > LoggedInUser.XAML
- ---->HIT BACK BUTTON<----
- -> Which then returns you back to LOGIN.XAML
- -> Which then returns you right back to LoggedInUser.XAML...
You could set a flag after the user logs in and then on the OnNavigated event could you check that flag to determine if you want to redirect? Without some sort of flag I'm not sure how you are going to do this and have it pass certification. With all that being said, I personally I wouldn't do it this way. I'm pretty sure that auto-redirecting isn't something that looked highly upon (mainly because of the certification process and UX). You might want to consider having a main screen with a login button. Then the login screen will do what you need it to and then if it passes then go to the next step for your app.
Just my 2 cents.
回答4:
If you are using Mango, on the mainpage.xaml you can do this. This will pop the login.xaml of the stack. Hope that helps.
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
PhoneApplicationFrame RootFrame = Application.Current.RootVisual as PhoneApplicationFrame;
if (RootFrame != null)
{
if (RootFrame.BackStack.Count() > 0)
{
RootFrame.RemoveBackEntry();
}
}
}
回答5:
Well I think the problem here is, that login screen, shouldn't really be a page (I mean PhoneApplicationPage or any class that extends it). In my opinion login screen should be just a child windows of any page, that requires user to log in some how.
So the approach here, would be to make a login screen as a user control and add it to Popup control on a page that requires user to log in. Then on Loaded event of the page or before any action that requires authentication, you just check if the user is logged in, if he is not, you just change visual state (open the popup with login, with ubercool animation of course :)) and thats it. User logs in popup closes and you are back on page, no need to redirect or any other hacks.
P.S.: I encourage you and all people reading this post to read Peter Torr's post on concept of places, here >> http://blogs.msdn.com/b/ptorr/archive/2010/08/28/introducing-the-concept-of-places.aspx
回答6:
I haven't submitted my app for approval yet but I have no reason to believe it won't pass based on my understanding of back navigation rules and experience with app submissions so far.
I went with an approach similar to Peter Torr's: http://blogs.msdn.com/b/ptorr/archive/2010/08/28/redirecting-an-initial-navigation.aspx?wa=wsignin1.0
The problem I had initially was that redirection from the login page to MainPage wasn't working. That required (a) updating the UriMapper again after login and (b) adding to the query string of MainPage.xaml when coming from a login just to change the Uri so the OS considered it a different page to navigate to.
More at http://www.geoffhudik.com/tech/2012/2/19/windows-phone-login-navigation.html. I'll update if for some reason it doesn't get approved or something else better shows up.
回答7:
I solved it like in my question here.
Basically, start at whatever page you want and throw a specific Exception (AuthFailedException
in my case) that you catch in Application's UnhandledException handler.
Then redirect to the login page and perform your usual authentication.
On success, GoBack
to the page you came from. If the back button is pressed at the login screen, listen for it in a BackKeyPressed
handler, clear the back stack (optional: until you hit a page which doesn't require authentication) and let the event pass through. This should quit your app or at least send you back where you began.
Not really modal, but opaque regarding UX.
回答8:
A more logical alternative to Amer Gill's answer would be to override OnNavigatedFrom in your Login page and clear the backstack upon navigating to the Main page:
protected override void OnNavigatedFrom(NavigationEventArgs e)
{
while (NavigationService.BackStack.Any())
{
NavigationService.RemoveBackEntry();
}
}
回答9:
Well even though you created a working solution, let me give you another suggestion :)
The best way I've found was working with the UriMapper
!
Not to reinvent the wheel, there is a nice example submitted to StackOverflow here: How to Show a Page if Application is Launched for the First Time