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 */ }
}
Related
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();
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.
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);
}
When I use a page animation to navigate from one page to another, I navigate fine to the second page but I can't navigate back to the main page (using the hardware back button) .When I try to navigate back I find all controls on the page gone.
Code to go the second page
private void Button1_Click(object sender, RoutedEventArgs e)
{
//Run animation then navigate to second page
myAnimation.Begin();
myAnimation.Completed += (s,ev)=>
{
NavigationService.Navigate(new Uri("/nextPage.xaml?id=Button1",UriKind.Relative));
};
}
Code on second page
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e )
{
base.OnNavigatedTo(e);
myAnimation.Begin(); //Another Animation
}
My guess is that your storyboard moves the controls outside of the view. When the user presses the back button, the previous page is restored in the exact state you left it. So the controls are still be hidden from the view.
To solve the issue, simply reset the storyboard when the user navigates to the page:
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
base.OnNavigatedTo(e);
myAnimation.Stop();
}
In my master page, I'm loading a variable in the session like this:
public partial class TheMasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ViewUserPreferences SessionUserPreferences = new ViewUserPreferences();
SessionUserPreferences = UserPreferences.GetUserPreferencesFromDB(6);
}
}
}
Then, in the code behind of a file, I have this:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
var test = Session["SessionUserPreferences"];
}
}
But when I debug, test is null. What's causing the problem?
Also, if I put a break point in the master page, it doesn't trigger when I run the aspx page; is this normal?
Thanks.
First thing you are missing the assignment part for UserPreferences.GetUserPreferencesFromDB(6) to the Session object. (I read the comments for #Greg's answer and you mentioned that even after that it is not working.)
Second, Master Page's Page_Load Event is triggered after the Current Page's Page_Load Event, hence the value of Session["SessionUserPreferences"] is null in Current Page's Page Load event since it is not set yet.
Check this link for further information on Page Events:
http://msdn.microsoft.com/en-us/library/dct97kc3.aspx
You have to do Session["SessionUserPreferences"] = something; somewhere before you attempt to retrieve that. Are you setting it somewhere else that you didn't show?