Back button controlling for my Windows Phone 8.1 Silverlight App - c#

I am developing a Windows Phone 8.1 Silverlight app. In my app, I need to override the back button code.
So I tried this,
protected override void OnBackKeyPress(CancelEventArgs e)
{
//Navigating to this page when back button is pressed
DisplayPage mynewPage = new DisplayPage();
this.Content = mynewPage;
e.Cancel = true;
}
But this code is not working. What am I doing wrong? Please help me out.
EDIT:
When I place this code on MainPage, it works! But I don't want to place it there. I want to place it on other pages.

Remove "e.Cancel = true" (it cancels the navigation). Simply just navigate to the new page.
EDIT:
For navigating to another page, I would rather suggest using NavigationService. Check out this page for examples.
EDIT:
Code snippet:
protected override void OnBackKeyPress(CancelEventArgs e)
{
//Navigating to this page when back button is pressed
NavigationService.Navigate(new Uri("/DisplayPage.xaml", UriKind.Relative));
}

Related

Windows 10 mobile UWP - slow back button

I have simply application written in c#, with sqlite database. I realized that it's not working fast on my phone. I'm pretty sure that problem is linked with functionality of pressed bulit-in back button. When I repeat several time this process:
Open new page -> return to previous page by back button, the application starting slows down.
When I added my own back button only to test, everything works fine.
I base mostly on this article:
http://www.wintellect.com/devcenter/jprosise/handling-the-back-button-in-windows-10-uwp-apps
Open new page -> return to previous page by back button, the application starting slows down.
After looking into your project, I found out the problem: You are registering SystemNavigationManager.GetForCurrentView().BackRequested +=OnBackRequested on every page. SystemNavigationManager.GetForCurrentView().BackRequested is an application scope event. It won't dispose the eventhandler when you are navigating between pages. You only need to register it once in your whole application.
So, to fix the problem, you can comment out all the BackRequested event registration of your pages's code-behind and keep only the one in your App.xaml.cs.
For example: in ProductsPage.xaml.cs comment out or delete following lines:
//SystemNavigationManager.GetForCurrentView().BackRequested += (s, e) =>
//{
// // TODO: Go back to the previous page
// Frame.Navigate(typeof(main1));
//};
If your back is Phone hard key you may handle the event.
The link is say the pc and add the back button and you should
SystemNavigationManager.GetForCurrentView().BackRequested +=OnBackRequested;
private void OnBackRequested(object sender, BackRequestedEventArgs e)
{
Frame rootFrame = Window.Current.Content as Frame;
if (rootFrame?.CanGoBack==true)
{
e.Handled = true;
rootFrame.GoBack();
}
else
{
Application.Current.Exit();
}
}
}
http://edi.wang/post/2016/2/1/windows-10-uwp-back-button-tricks
http://blog.csdn.net/lindexi_gd/article/details/50618029

Windows phone save form

I'm developing some app for Windows phone 8.1, and I have this problem:
Have 1 form with some checkbox, radio buttons and some text fields. When I fill it up, and move to next form with:
Frame.Navigate(typeof(SOME_FORM));
and I have some things to do, and want to come back to 1st form, it's all cleared. How to remember what i fill up on 1st form, when i come back from 2nd?
And I add all controls programmatically, so I can't save it in static variable.
Any help?
In your first page set property NavigationCacheMode to Required
in XAML
NavigationCacheMode="Required"
or in code behind
this.NavigationCacheMode = NavigationCacheMode.Required;
But the question is: when do you add controls to page? if you add control in OnNavigatedTo you should check NavigationMode and don't reload page on back navigation
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (e.NavigationMode == NavigationMode.Back)
{
//do nothing - filled controls already there
return;
}
//add controls
}

How to go to previous page in windows phone 8 when back button is pressed

I have a home page or landing page in my windows phone c# based app where user enters login details and upon successful login user is redirected to page2 . Here the user will see a list box with few items . Upon selecting an item from this list box a new page called "Threadx" opens.(where x is the each page that opens upon clicking the x item in the list box)
While user is on this Thread page "Threadx" he may receive the toast notifications and the thread gets updated with new replies or answers on that thread.
But When user clicks on back button the "ThreadX" page doesn't get closed and instead it goes to its previous state where it has less number of messages , and so on until the app gets closed.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (e.NavigationMode == NavigationMode.Back)
{
return;
}
}
I would like to know if this "Threadx" page can be closed upon clicking back button without affecting other "Threadx+1","Threadx+2"..."Threadx+n" pages.
Any help would be really appreciated.
Normally windows keeps the pages on it's stack when you leave a page and navigate to another page. If you want to navigate to the previous page on pressing the Back Button you can do following things:
Add following line to OnNavigatedTo method:
Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
Add definition for HardwareButtons_BackPressed method:
private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
{
e.Handled = true;
if (Frame.CanGoBack)
Frame.GoBack();
}
Don't forget to add Windows.Phone.UI.Input to the namespace list.
The other way I got it worked was using the below code in the onnavigateto method in my "thread" page and it worked for me. Let me know if there is an elegant way of doing it or better way of doing it .
if (e.NavigationMode == NavigationMode.Back)
{
NavigationService.Navigate(new Uri("/View/Page2.xaml", UriKind.Relative));
}

how to prevent going back to previous page

For a windows phone 8 app I'm developing, I had to load some data at the starting of the app. For that matter I designed a page called SplashScreen.xaml that loads the data and after all the loading is done I navigate to the MainPage.xaml using:
NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
Now when the user is in the main page and taps the back button on the phone instead of going out of the app(which is the default gesture) goes back to the SplashScreen.xaml, making them unable to go out of the app(except for taping the start button which take's the app to background) and of course giving them a bad impression.
The question is How to prevent going back to the previous page
Thank you all.
Just clear the backstack when landing on MainPage:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
while (NavigationService.RemoveBackEntry() != null);
}

Get a control inside another control

I've been racking my brain over this for a while, its been a while since I had to do this and know its possible I've done it in another project in the past that I don't have a backup of to refer to.
I have a Login View on a page inside the login view is 2 panels one panel with a login control (to login) and one panel with a createuserwizard (to register) and a second button to click to register.
I'm trying to hide the panel with the login control and show the panel with the register control via a button click but all I end up with is a null reference exception.
this is what I have currently.
protected void Register_Click(object sender, EventArgs e)
{
FindControl("LoginView1").FindControl("LoginPanel").Visible = false;
FindControl("LoginView1").FindControl("RegPanel").Visible = true;
}
I appreciate any help thanks.
I figured out what the problem was so I'll leave the question here for anyone who may have the same problem and stumble across this
I was so used to working with controls from a master page but within a page that's inside the master page you don't need the first findcontrol its simply:
protected void Register_Click(object sender, EventArgs e)
{
LoginView1.FindControl("LoginPanel").Visible = false;
LoginView1.FindControl("RegPanel").Visible = true;
}

Categories