尝试点击LongListSelector时出错

时间:2014-06-01 15:29:56

标签: windows-phone-8 longlistselector

我已经创建了一个与LongListSelector链接的JumpList(通过跟随this tutorial),但是当他点击列表中的某个项目时,我无法重定向用户。我曾经用一个简单的LongListSelector做好这个:

<phone:LongListSelector x:Name="lls_Songs" SelectionChanged=lls_Songs_SelectionChanged>
    <phone:LongListSelector.ItemTemplate>
        <DataTemplate>
            <StackPanel Margin="5,5,5,5">
                <TextBlock Text="{Binding Name}" FontSize="30"/>
                <TextBlock Text="{Binding Duration}" FontSize="20" Opacity="0.75"/>
            </StackPanel>
        </DataTemplate>
    </phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>

但现在我使用这段代码......

<phone:LongListSelector
                  x:Name="lls_songs"
                  Margin="12,35,12,0"
                  Visibility="Visible"
                  JumpListStyle="{StaticResource JumpListStyle}"
                  Background="Transparent"
                  GroupHeaderTemplate="{StaticResource lls_SongsHeaderTemplate}"
                  ItemTemplate="{StaticResource lls_SongsTemplate}"
                  LayoutMode="List"
                  IsGroupingEnabled="true"
                  SelectionChanged="lls_songs_SelectionChanged"
                  HideEmptyGroups ="true"/>

...与此代码相关联:

<DataTemplate x:Key="lls_SongsTemplate">
    <StackPanel VerticalAlignment="Top" Orientation="Horizontal" Margin="5,5,5,5">
        <StackPanel Orientation="Vertical">
            <toolkit:ContextMenuService.ContextMenu>
                <toolkit:ContextMenu>
                    <toolkit:MenuItem Header="détails"/>
                    <toolkit:MenuItem Header="ajouter à la lecture"/>
                </toolkit:ContextMenu>
            </toolkit:ContextMenuService.ContextMenu>
            <TextBlock Text="{Binding songName}" FontSize="30" />
            <TextBlock Text="{Binding songArtist}" FontSize="20" Opacity="0.75"/>
        </StackPanel>
    </StackPanel>
</DataTemplate> 

这是lls_Songs_SelectionChanged方法:

private void lls_songs_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        Song _selectedSong = lls_songs.SelectedItem as Song;
        MediaPlayer.Play(_selectedSong);
    }

最后我是如何制作我的歌曲列表的:

MediaLibrary _library = new MediaLibrary();
List<MusicInfo> MusicInfoList = new List<MusicInfo>();

int so = _library.Songs.Count;
int _so = 0;
while(_so < so)
    {
        Song _song = null;
        _song = _library.Songs[_so];
        MusicInfoList.Add(new MusicInfo(_song.Name, _song.Artist.ToString()));
        _so = _so + 1;
    }

与此课程相关联:

    public string songName { get; set; }
    public string songArtist { get; set; }

    public MusicInfo(string _songName, string _songArtist)
    {
        this.songName = _songName;
        this.songArtist = _songArtist;
    }

和跳转列表:

private void SortingSongsListsAZ()
    {
        List<AlphaKeyGroup<MusicInfo>> DataSource = AlphaKeyGroup<MusicInfo>.CreateGroups(MusicInfoList,
            System.Threading.Thread.CurrentThread.CurrentUICulture,
            (MusicInfo s) => { return s.songName; }, true);

        lls_songs.ItemsSource = DataSource;

    }

当我点击列表中的某个项目时,我收到此错误:&#34;此方法不接受此参数的空值&#34;。我不明白为什么,有人会帮助我吗?

2 个答案:

答案 0 :(得分:0)

分享或检查您的lls_songs项目源绑定和代码。从您的代码看来

Song _selectedSong = lls_songs.SelectedItem as Song;

_selectedSong为空。

在调试模式下,查看SelectedItem的值是什么。是宋类型还是别的什么?

答案 1 :(得分:0)

根据您的更新,似乎lls_songs.SelectedItem的类型为MusicInfo,而不是Song。修复你的类型转换应该删除我认为的错误:

MusicInfo _selectedSong = lls_songs.SelectedItem as MusicInfo;

better yet这样:

MusicInfo _selectedSong = (MusicInfo)lls_songs.SelectedItem;