我在第一页加载事件中滚动到列表框的选定索引时遇到问题。当页面第一次加载时,在更改和突出显示所选索引时,它无法滚动到所选索引。当我导航到另一个页面并返回到此页面时,它会滚动到所选索引。我不明白为什么它无法在第一页加载时滚动。
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);
}
这不仅适用于第一页加载事件。我不明白是什么导致它不滚动?
谢谢!
答案 0 :(得分:0)
在页面加载中使用此功能。
this.Page.MaintainScrollPositionOnPostBack = true;