zoukankan      html  css  js  c++  java
  • [Windows Phone 7璀璨]北漂1.0在线歌词播放器二.歌曲按艺术家分类实现

    一.实现歌曲按艺术家分类达到快速定位用到了LongListSelector控件

    国外资料:

    http://www.windowsphonegeek.com/articles/wp7-longlistselector-in-depth--part1-visual-structure-and-api

    http://www.windowsphonegeek.com/articles/wp7-longlistselector-in-depth--part2-data-binding-scenarios

    二.编码实现

    2.1页面XAML代码

    View Code
     1  <phone:PhoneApplicationPage.Resources>
    2 <DataTemplate x:Key="GroupHeader">
    3 <Border Background="{StaticResource PhoneAccentBrush}" Margin="3">
    4 <TextBlock Text="{Binding Key}" Height="40" FontSize="30"></TextBlock>
    5 </Border>
    6 </DataTemplate>
    7 <DataTemplate x:Key="GroupSongItem">
    8 <Border Width="480" Height="50" Background="{StaticResource PhoneAccentBrush}" Margin="3">
    9 <TextBlock Text="{Binding Key}" FontSize="30" Foreground="White"></TextBlock>
    10 </Border>
    11 </DataTemplate>
    12 <DataTemplate x:Key="SongItem">
    13 <StackPanel>
    14 <TextBlock Text="{Binding Name}" FontSize="30" ></TextBlock>
    15 <TextBlock Text="{Binding Artist}"></TextBlock>
    16 </StackPanel>
    17 </DataTemplate>
    18 </phone:PhoneApplicationPage.Resources>
    19 <!--LayoutRoot 是包含所有页面内容的根网格-->
    20 <Grid x:Name="LayoutRoot" Background="Transparent">
    21 <toolkit:LongListSelector x:Name="SongList"
    22 ItemTemplate="{StaticResource SongItem}"
    23 GroupHeaderTemplate="{StaticResource GroupHeader}"
    24 GroupItemTemplate="{StaticResource GroupSongItem}" SelectionChanged="SongList_SelectionChanged"/>
    25
    26
    27 </Grid>

    2.2分组实现Group<T>代码

    View Code
    ///<summary>
    /// 分组实现
    ///</summary>
    ///<typeparam name="T"></typeparam>
    public class Group<T> : IEnumerable<T>
    {
    public string Key { get; set; }
    public IList<T> Items { get; set; }
    public Group(string key, IEnumerable<T> Item)
    {
    this.Key = key;
    this.Items = new List<T>(Item);
    }
    public override bool Equals(object obj)
    {
    Group<T> NowGroup = obj as Group<T>;
    return (NowGroup != null) && (this.Key.Equals(NowGroup.Key));
    }
    #region
    public IEnumerator<T> GetEnumerator()
    {
    return this.Items.GetEnumerator();
    }
    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
    return this.Items.GetEnumerator();
    }
    #endregion
    }

    2.3数据绑定代码(用到LINQ分组排序)

    View Code
     1         void get()
    2 {
    3 //装载歌曲集合
    4 songs = libary.Songs;
    5 //根据艺术家分组排序给LISTBOX
    6 var music = from s in songs
    7 group s by s.Artist into newmusic
    8 select new Group<Song>(newmusic.Key.ToString(),newmusic);
    9 //绑定数据
    10 SongList.ItemsSource = music;
    11
    12
    13
    14 }

    2.4歌曲选中播放事件

    View Code
     1 private void SongList_SelectionChanged(object sender, SelectionChangedEventArgs e)
    2 {
    3 if (this.SongList.SelectedItem != null)
    4 {
    5 //获取选中项
    6 string SelectsongName = this.SongList.SelectedItem.ToString();
    7 int i = -1;
    8 //遍历歌曲集合
    9 foreach (var item in songs)
    10 {
    11 i++;
    12 //如果找到歌曲
    13 if (item.Name == SelectsongName)
    14 {
    15 MediaPlayer.Play(songs, i);
    16
    17 }
    18
    19 }
    20 //对歌曲按照歌名排序
    21 var songGobye = from s in songs
    22 orderby s.Name
    23 select songs;
    24
    25
    26 foreach (var item in songGobye)
    27 {
    28 songs = item;
    29
    30 }
    31
    32
    33 }
    34
    35
    36 }






  • 相关阅读:
    Lookup 组件用法全解
    将字符串转换成表
    处于同一域中的两台SQL Server 实例无法连接
    在SSIS中的不同组件间使用局部临时表
    SSIS的CheckPoint用法
    控制流如何处理错误
    SSIS 对数据排序
    SSIS 数据输出列因为字符截断而失败
    SQL Server 的本地时间和UTC时间
    TSQL Merge 用法
  • 原文地址:https://www.cnblogs.com/tubufeng/p/2404769.html
Copyright © 2011-2022 走看看