C# 如何滚动到wp8中列表框的SelectedIndex
在第一页加载事件中,我无法滚动到列表框的选定索引。当页面第一次加载时,当所选索引被更改并突出显示时,它无法滚动到所选索引。当我导航到另一个页面并返回到此页面时,它会滚动到所选索引。我不明白为什么它不能在第一页加载时滚动C# 如何滚动到wp8中列表框的SelectedIndex,c#,windows-phone-8,C#,Windows Phone 8,在第一页加载事件中,我无法滚动到列表框的选定索引。当页面第一次加载时,当所选索引被更改并突出显示时,它无法滚动到所选索引。当我导航到另一个页面并返回到此页面时,它会滚动到所选索引。我不明白为什么它不能在第一页加载时滚动 void MainPage_Loaded(object sender, RoutedEventArgs e) { try { ApplicationBarIconButton btn = (ApplicationBarIconButton)Applic
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.Page.MaintainScrollPositionOnPostBack = true;
这不仅在第一页加载事件中有效。我不明白是什么原因导致它不能滚动
谢谢 在页面加载中使用此选项
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.Page.MaintainScrollPositionOnPostBack = true;
我们在windows phone 8中有这样的属性吗?windows phone 8没有在平台中列出。。。所以可能不是,但肯定有类似的东西。你可以通过document.documentElement.scrollTop填充一个隐藏字段,然后在pageload上滚动到那个位置来解决这个问题。