Windows Phone 8.1找到LongListSelector选择的项目索引

时间:2016-11-06 14:19:33

标签: c# xaml windows-runtime windows-phone-8.1 windows-phone

我正在使用Windows Phone 8.1音乐播放器应用程序,我将歌曲添加到LongListSelector。我的代码:

public async void initializeMusic()
    {
        await Task.Delay(50);

        collection = library.Songs;
        foreach (Song s in collection)
        {

            songobj.Add(new Songs(s.Name, s.Album.Name));
        }


        List<AlphaKeyGroup<Songs>> datasource = AlphaKeyGroup<Songs>.CreateGroups(songobj,
                System.Threading.Thread.CurrentThread.CurrentCulture,
                (Songs s) => { return s.songname; }, true);

        NowPlayingList.ItemsSource = datasource;
    }

在LongListSelector中选择更改事件:

  private void MainListbox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        // int index=0;
        if (Title_TracksGrid.Text == "Artists")
        {
            try
            {
                songobj = new SongList();
                var item = MainListbox1.SelectedItem;
                int index = MainListbox1.ItemsSource.IndexOf(item); 

                //foreach(var a in e.AddedItems)
                //{
                //   if(a.ToString() == item.ToString())
                //    {
                //        index = e.AddedItems.IndexOf(a);
                //    }
                //}


                System.Diagnostics.Debug.WriteLine("Song index : " + index.ToString());
                // Title_TracksGrid.Text = myIndex.ToString();
                // int index =library.Artists.ElementAt(1).Songs;
                collection = library.Artists.ElementAt(1).Songs;

                foreach (Song s in collection)
                {
                    songobj.Add(new Songs(s.Name, s.Album.Name));
                }
                MainListbox1.Visibility = Visibility.Collapsed;
                TrackListbox.ItemsSource = songobj;
                TrackListGrid.Visibility = Visibility.Visible;
                VIsibilityStoryboard.Begin();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
}
}

LongListSelector xaml:

<phone:LongListSelector x:Name="MainListbox1" Visibility="Visible" Grid.RowSpan="2" 
                            JumpListStyle="{StaticResource AddrBookJumpListStyle}" GroupHeaderTemplate="{StaticResource AddrBookGroupHeaderTemplate}"
  ItemTemplate="{StaticResource AddrBookItemTemplatePlayList}"
  LayoutMode="List"
  IsGroupingEnabled="true"
  HideEmptyGroups ="true" Background="{x:Null}" Margin="10,105,0,0"             SelectionChanged="MainListbox1_SelectionChanged"/>

和itemTemplate:

 <DataTemplate x:Key="AddrBookItemTemplatePlayList">
        <StackPanel Height="auto" Margin="5,0,0,5" Grid.RowSpan="2" >
            <TextBlock FontWeight="Normal"  Text="{Binding Name}" FontFamily="Segoe WP" FontSize="30" VerticalAlignment="Top" Margin="5,0,0,0" HorizontalAlignment="Left" Foreground="White" />
            <TextBlock Width="294" FontSize="20" Text="{Binding Count}" FontFamily="Segoe WP Light"  Margin="5,0,0,0" HorizontalAlignment="Left" VerticalAlignment="Top" Height="40" Foreground="White" />
        </StackPanel>
    </DataTemplate>

index始终返回-1。有什么建议吗?

0 个答案:

没有答案