0
votes

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?

`

1

1 Answers

1
votes

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>