Skip the page in wp7 navigation stack - c#

Let's say I have a code
protected override void OnNavigatedTo(NavigationEventArgs e)
{
var userInfo = SettingsManager.Read<User>(SettingsManager.UserInfoSetting);
if (e.NavigationMode == NavigationMode.Back && userInfo == null)
{
_mainViewModel.NavigationService.GoBack();
}
if (e.NavigationMode == NavigationMode.New && userInfo == null)
{
_mainViewModel.NavigationService.NavigateTo(new Uri(ViewModelLocator.SettingPageUrl, UriKind.Relative));
}
base.OnNavigatedTo(e);
}
When the user runs application for the first time he will be redirected to the settings page and it works pretty fine right now. If user doesn't want to provide his information he can press back-button in that case I want to skip main page of the application and exit the application. If I run the code I received InvalidOperationException Cannot go back when CanGoBack is false.
The GoBack() method calls PhoneApplicationFrame.GoBack() method to navigate back.

Warning - Removing backstack entries is not recommended by Windows Developer guidelines. Because you have to adhere to the natural behaviour of the back button.
Link - https://learn.microsoft.com/en-us/windows/uwp/layout/navigation-history-and-backwards-navigation
Try removing back stack entries (Main page in your case) when the user goes to the settings page for the first time.
You can remove any remaining backstack entries by the following code:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
while (this.NavigationService.BackStack.Any())
{
this.NavigationService.RemoveBackEntry();
}
}
So when the user presses the back button the application should exit as there are no remaining backstack entries.

You cannot forcibly exit this way.
What you could do, is handle the back button press on the settings page and clear the stack (which is allowed), then let the back button be handled -- thus the user will exit because of the back button, not because of your call.

protected override void OnBackKeyPress(CancelEventArgs e)
{
while (NavigationService.CanGoBack)
{
NavigationService.RemoveBackEntry();
}
base.OnBackKeyPress(e);
}

Related

Remove Session Value If I Delete Value From Field And Re-Submit Form

I have an asp.net webform that stores details entered in the session and then displays all the details on a confirmation page later on. All these works are fine.
If the user navigates back to the page, the details are still displayed but the issue i'm having is that:
User Scenario
User enter's details in a none mandatory field and continues. This value is displayed on the confirmation page as expected. User then goes back to the page and deletes the value in the none mandatory field and continues BUT the value deleted is still deleted on my confirmation page.
I have debugged it and at first I can see that the field is empty. But when the code drops back into my submitbutton_click, it re-adds it.
The current code that I have is displayed below:
Code
protected void Page_Load(object sender, EventArgs e)
{
if (Step01AddressBuildingname.Text == string.Empty && Session["Step01AddressBuildingname"] != null)
{
Step01AddressBuildingname.Text = Session["Step01AddressBuildingname"].ToString();
}
}
protected void Step01SubmitButton_Click(object sender, EventArgs e)
{
Session["Step01AddressBuildingname"] = Step01AddressBuildingname.Text;
}
From what I can discern from your post it seems that when you update the Step01AddressBuildingname field to be blank and click on submit, this does not clear down the session value as expected. If so it's because you're not handling the post back in you page_load event. In asp.net webforms postbacks occur when a form is submitted to the server, in this scenario you just want to handle the form submission and not load the page as standard. You do this by checking the IsPostBack property on the page.
In your code, because you're not checking this property, tep01AddressBuildingname.Text = Session["Step01AddressBuildingname"].ToString(); is getting executed before the submit button's click event handler - hence the value doesn't get deleted. Try this
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) {
if (Step01AddressBuildingname.Text == string.Empty && Session["Step01AddressBuildingname"] != null)
{
Step01AddressBuildingname.Text = Session["Step01AddressBuildingname"].ToString();
}
}
}
protected void Step01SubmitButton_Click(object sender, EventArgs e)
{
Session["Step01AddressBuildingname"] = Step01AddressBuildingname.Text;
}

How to get info about previous page on Frame.GoBack()

Say we have some Page PageA and I have a button that, when clicked, does the following:
Frame.NavigateTo(typeof(PageB));
After the user is done doing stuff, he navigates back from PageB to PageA calling Frame.GoBack()
I want be able to determine that I'm navigating back from PageB
I could use:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
e.NavigationMode
}
But this only tells me that I'm navigating back, not that I'm navigating back from PageB.
Is this even a good windows-phone-guidelines approach (did not find this particular case in the docs)?
I think you should be able to do it by using Frame.ForwardStack property which holds forward navigation history.
A short sample which should work:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
var lastPage = Frame.ForwardStack.LastOrDefault();
if (lastPage != null && lastPage.SourcePageType.Equals(typeof(desiredPage)))
{ /* do something */ }
}

How to remove Frame.BackStackDepth?

I am developing a windows 8.1 app which includes several pages. I want to know how can I remove Frame.BackStackDepth so that when user press the back button the app will navigate to the first page.
I tried this but it remove the previous Frame only.
private void backButton_Click(object sender, RoutedEventArgs e)
{
this.Frame.BackStack.RemoveAt(this.Frame.BackStackDepth-1);
this.Frame.GoBack();
}
You can use this extension method:
public static void ResetBackStack(this Frame frame)
{
PageStackEntry mainPage = frame.BackStack.Where(b => b.SourcePageType == typeof(YourPageType)).FirstOrDefault();
frame.BackStack.Clear();
if (mainPage != null)
{
frame.BackStack.Add(mainPage);
}
}
Just override the BackPressed event inside your NavigationHelper class: call that extension method with your frame and then navigate back.
Or just put it inside your EventHandler:
private void backButton_Click(object sender, RoutedEventArgs e)
{
this.Frame.ResetBackStack();
this.Frame.GoBack();
}
As I know from the Windows Phone, you cannot remove backstack with a command or method. it is not such easy. You have to use loops till CanGoBack returns false. This is only way to do this.
I imagined your logic so, remove all pages in backstack except the first opened page. The loop continues up to exception.
Do not forget to define your first page name instead FirstPage.
Also please check the msdn link for more information.
https://social.msdn.microsoft.com/Forums/windowsapps/en-US/3819f389-3bfc-4c59-a919-272927fc9229/navigation-in-a-metro-application?forum=winappswithcsharp
Try this solution.
do
{
this.Frame.BackStack.RemoveAt(this.Frame.BackStackDepth - 1);
} while (Frame.CanGoBack && Frame.BackStack.Last(entry => entry.SourcePageType != typeof(FirstPage)) != null);
this.Frame.GoBack();

Navigation Source in Windows Phone

I have 2 pages in my application, A and B.
If I'm navigation from the outside of the application to A, I want to display a message box. If I'm navigation from B to A, I don't want to display anything.
Is there any way to identify in A the page which initiated navigation? i.e in A.Loaded (or any other event) I need something like
if(pageFromWhichIAmComingFrom == B)
OnNavigatedTo, OnNavigationFrom and OnNavigatedFrom don't seem to help me.
You could use the PhoneApplicationService class to store information about what page you were on last. For example, use OnNavigatedFrom on Page A:
void OnNavigatedFrom(object sender, Eventargs e)
{
PhoneApplicationService.Current.State["LastPage"] = "PageA";
}
And then check for that on the next page:
void OnNavigatedTo(object sender, Eventargs e)
{
if(PhoneApplicationService.Current.State["LastPage"].ToString() == "PageA")
{
// came from page A
}
else
{
// came from a different page
}
}
Hope this helps!
UPDATE:
One more thing I just saw that might be worth trying is using the NavigationService.BackStack property. I haven't tried this, but it seems like it should work. In your OnNavigatedTo event handler, you should be able to get the last entry from the stack to see your last page. This would be simpler and wouldn't require you to set any properties manually. Example:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
var lastPage = NavigationService.BackStack.FirstOrDefault();
}
Found here.

How to hide webbrowser navigations?

I have a form that on load show login form on webbrowser. When user login and then exit without loging out, it automatically logging him out on next start up and showing him again the login form. To logout from site 2 navigations needed but I don't user to see that navigations, I want the browser to be white until all navigations complete or something similar.
bool f = true;
private void Form1_Load(object sender, EventArgs e)
{
wbLoad.Navigate("http://login.uid.me/?site=dmysite&ref=http://mysite.ucoz.com/");
}
private void wbLoad_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (f == true)
{
if (wbLoad.DocumentText.Contains("Sign up"))
{
wbLoad.Navigate("http://login.uid.me/?site=dmysite&ref=http://mysite.ucoz.com/");
f = false;
}
else if (wbLoad.DocumentText.Contains("Log out"))
{
wbLoad.Navigate("http://mysite.ucoz.com/index/10");
f = true;
}
else
{
wbLoad.Navigate("http://uid.me/logout/?mode=1&noajax=1");
f = true;
}
}
}
Logging out means in most cases just removing cookies for current host. If it's your case, you can do it by using javascript. Note, that this JS will remove cookies only for current host. You can call it in FormClosing event
wbLoad.Navigate("javascript:void((function(){var a,b,c,e,f;f=0;a=document.cookie.split('; ');for(e=0;e<a.length&&a[e];e++){f++;for(b='.'+location.host;b;b=b.replace(/^(?:%5C.|[^%5C.]+)/,'')){for(c=location.pathname;c;c=c.replace(/.$/,'')){document.cookie=(a[e]+'; domain='+b+'; path='+c+'; expires='+new Date((new Date()).getTime()-1e11).toGMTString());}}}})())");
You need to write the logout concept in Form Close event or some where else. So next time when user opens that time it will automatically redirects to login page. And need to refresh all your objects after logout.

Categories