简体   繁体   中英

How to make Windows phone 8.1 listview to open list last selecteditem after pressed windows phone back button?

I Have list on images that i display in listview. When i click listview item it opens another page where it shows bigger version of current picture. When i press Windows phone back button i would like listview to scroll to the item that was selected earlier.

Here is how I save lsitview.selectedindex:

 private void NavigationHelper_SaveState(object sender, SaveStateEventArgs e)
    {

        e.PageState.Add("id", listview.SelectedIndex);
    }

This is how i am trying set listview:

  private void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
    {

        if (null != e.PageState && e.PageState.ContainsKey("id"))
        {

            int i = (int)e.PageState["id"];
            listview.ScrollIntoView(listview.Items[i]);
            System.Diagnostics.Debug.WriteLine(listview.Items.Count);

        }
    }

I also tried like this but this is not working ?

 private void listview_Loaded(object sender, RoutedEventArgs e)
 {

      listview.ScrollIntoView(listview.Items[i])
  }

But when i add normal button to my lisview page like this:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        listview.ScrollIntoView(listview.Items[i]);
    }

It scroll to selected item after button click. How i can make it work without button so that lisview scroll automatically last selected item?

`

If I understand correctly, when the users goes to a picture and comes back, the ListView is scrolled to the top, right? In this case, this is because the page instance is not preserved in cache so it loads again when you press the back button. You can stop this by setting NavigationCacheMode to Enabled:

<Page x:Class="App.YourClass"
    NavigationCacheMode="Enabled"
</Page>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM