How to go back mainpage when press back button on device?

Discussion How to go back mainpage when press back button on device?

  • Sunday, April 29, 2012 3:55 PM
     
     

    i have an small windows phone application.

    my application have a button and a listbox. my code below:

    namespace PhoneApp1
    {
        public partial class MainPage : PhoneApplicationPage
        {
            // Constructor
            public MainPage()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                listBox1.Items.Add("http://google.com");
            }
    
            private void listBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                WebBrowserTask wbt = new WebBrowserTask();
                wbt.URL = listBox1.SelectedItem.ToString();
                wbt.Show();
            }
        }
    }
    My problem is when i press back button then item in my listbox no longer appear in my mainpage.
    Can i keep item of listbox in mainpage when i press back button ?

All Replies

  • Monday, April 30, 2012 12:00 AM
     
     

    Hi,

        You have the OnNavigatedTo  and OnNavigatedFrom events for your page.  In the OnNavigatedFrom , write logic to store the contents of listbox in a collection , and also store another variable (a boolean variable ) to indicate whether value exists.  

    In the OnNavigatedTo , first check whether the variable is set , and if it is, then retrieve the contents of the listbox from the other stored collection.

    Hope it Helps Laughing

     

  • Monday, April 30, 2012 12:10 PM
     
     

    Hi,

        You have the OnNavigatedTo  and OnNavigatedFrom events for your page.  In the OnNavigatedFrom , write logic to store the contents of listbox in a collection , and also store another variable (a boolean variable ) to indicate whether value exists.  

    In the OnNavigatedTo , first check whether the variable is set , and if it is, then retrieve the contents of the listbox from the other stored collection.

    Hope it Helps Laughing

     

    Thanks your reply, it helped me! Smile