简体   繁体   English

如何在wp8中滚动到列表框的SelectedIndex

[英]How to scroll to the SelectedIndex of listbox in wp8

I am having problem scrolling to selected index of listbox in 1st page load event. 我在第一页加载事件中滚动到列表框的选定索引时遇到问题。 When the page loads for the 1st time it cannot scroll to the selected index while the selected index is changed and highlighted. 当页面第一次加载时,在所选索引更改并突出显示时,它无法滚动到所选索引。 When I navigate to another page and come back to this page then it scroll to the selected index. 当我导航到另一个页面并返回该页面时,它将滚动到所选索引。 I didn't understand why it could not scroll at 1st page load. 我不明白为什么它无法在第一页加载时滚动。

void MainPage_Loaded(object sender, RoutedEventArgs e)
{
    try
    {
      ApplicationBarIconButton btn = (ApplicationBarIconButton)ApplicationBar.Buttons[1];
      if (PlayState.Playing == BackgroundAudioPlayer.Instance.PlayerState)
      {
         btn.IconUri = new Uri("/Images/pause.png", UriKind.Relative);
         BackgroundAudioPlayer player = BackgroundAudioPlayer.Instance;
         if (player.Track != null)
         {
            currentChapter = ds.getChapter(Convert.ToByte(player.Track.Artist.Split(DataSource.TRACKARTISTDELIMTER)[0])); 
            lsbReadingChapter.ItemsSource = ds.getArabicTextWithTranslation(currentChapter);

           //Get Position and Start timer
           fillRecitation();
           double position = player.Position.TotalSeconds;
           setSelectedIndex(position);
           recitationTimer.Start();
         }
      }
      else
      {
         lsbReadingChapter.ItemsSource = ds.getArabicTextWithTranslation(currentChapter);
         recitationTimer.Stop();
         btn.IconUri = new Uri("/Images/play.png", UriKind.Relative);
         scrollIntoSelectedItem(Convert.ToByte(App.Recent.AyaID));

       }

       txtTitle.Text = HCI.convertToArabicIndicString(currentChapter.ChapterID) + " - " + currentChapter.SuraName + " - " + HCI.convertToArabicIndicString(currentChapter.Ayas);

       if (currentChapter != null)
       {
         if (ds.chapterAvailable(currentChapter))
            txtAvailable.Text = "available";
         else
            txtAvailable.Text = string.Empty;
       }

       if (lsbReadingChapter.SelectedIndex != -1)
         scrollIntoSelectedItem(lsbReadingChapter.SelectedIndex); //scroll to index
  }
  catch (Exception) { }
}

void scrollIntoSelectedItem(int index)
{
   lsbReadingChapter.SelectedIndex = lsbReadingChapter.Items.Count - 1;
   lsbReadingChapter.UpdateLayout();
   lsbReadingChapter.ScrollIntoView(lsbReadingChapter.SelectedIndex);

   lsbReadingChapter.SelectedIndex = index;
   lsbReadingChapter.UpdateLayout();
   lsbReadingChapter.ScrollIntoView(lsbReadingChapter.SelectedIndex);

}

This doesn't work only in 1st page load event. 这仅在第一页加载事件中不起作用。 I didn't understand what causes it not scroll? 我不明白是什么原因导致无法滚动?

Thanks! 谢谢!

在页面加载中使用它。

this.Page.MaintainScrollPositionOnPostBack = true;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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