LongListSelector 歌曲列表 按歌手,按专辑分组的实现

  

 

首先建立一个Song.cs,用于保存歌曲信息

View Code
 1 using System;
2 using System.Net;
3 using System.Windows;
4 using System.Windows.Controls;
5 using System.Windows.Documents;
6 using System.Windows.Ink;
7 using System.Windows.Input;
8 using System.Windows.Media;
9 using System.Windows.Media.Animation;
10 using System.Windows.Shapes;
11
12 namespace PhoneAppMusic
13 {
14 public class Song
15 {
16 ///<summary>
17 /// ID
18 ///</summary>
19 public string GUID { get; set; }
20 ///<summary>
21 /// 歌曲名
22 ///</summary>
23 public string SongName { get; set; }
24 ///<summary>
25 /// 专辑名
26 ///</summary>
27 public string SongCDName { get; set; }
28 ///<summary>
29 /// 歌手
30 ///</summary>
31 public string Songer { get; set; }
32 ///<summary>
33 /// 专辑图片
34 ///</summary>
35 public Uri SongCDPic { get; set; }
36 }
37 }

 

SongHelper.cs类

View Code
 1 using System;
2 using System.Net;
3 using System.Windows;
4 using System.Windows.Controls;
5 using System.Windows.Documents;
6 using System.Windows.Ink;
7 using System.Windows.Input;
8 using System.Windows.Media;
9 using System.Windows.Media.Animation;
10 using System.Windows.Shapes;
11
12 using System.Collections;
13 using System.Collections.Generic;
14
15 namespace PhoneAppMusic
16 {
17 public class SongHelper
18 {
19 private List<Song> songs = new List<Song>();
20 public List<Song> Songs {
21 get { return songs; }
22 }
23 public SongHelper() {
24
25 IntoSong("不浪漫的罪名", "万岁2001", "王杰", new Uri("SongCDImg/150_albumpic_576_0.jpg", UriKind.Relative));
26 IntoSong("情已经失去", "万岁2001", "王杰", new Uri("SongCDImg/150_albumpic_576_0.jpg", UriKind.Relative));
27 IntoSong("伤心1999", "万岁2001", "王杰", new Uri("SongCDImg/150_albumpic_576_0.jpg", UriKind.Relative));
28 IntoSong("万岁", "万岁2001", "王杰", new Uri("SongCDImg/150_albumpic_576_0.jpg", UriKind.Relative));
29 IntoSong("情已经失去", "万岁2001", "王杰", new Uri("SongCDImg/150_albumpic_576_0.jpg", UriKind.Relative));
30
31 IntoSong("知足", "知足", "五月天", new Uri("SongCDImg/150_albumpic_8143_0.jpg", UriKind.Relative));
32 IntoSong("憨人", "知足", "五月天", new Uri("SongCDImg/150_albumpic_8143_0.jpg", UriKind.Relative));
33 IntoSong("而我知道", "知足", "五月天", new Uri("SongCDImg/150_albumpic_8143_0.jpg", UriKind.Relative));
34 IntoSong("乱世浮生", "知足", "五月天", new Uri("SongCDImg/150_albumpic_8143_0.jpg", UriKind.Relative));
35
36 IntoSong("情歌没有告诉你", "情歌没有告诉你", "梁静茹", new Uri("SongCDImg/150_singerpic_44_0.jpg", UriKind.Relative));
37 IntoSong("一家一", "情歌没有告诉你", "梁静茹", new Uri("SongCDImg/150_singerpic_44_0.jpg", UriKind.Relative));
38 IntoSong("直觉", "情歌没有告诉你", "梁静茹", new Uri("SongCDImg/150_singerpic_44_0.jpg", UriKind.Relative));
39
40 IntoSong("我们就到这", "今天是情人节", "梁静茹", new Uri("SongCDImg/150_albumpic_35795_0.jpg", UriKind.Relative));
41 IntoSong("我决定", "今天是情人节", "梁静茹", new Uri("SongCDImg/150_albumpic_35795_0.jpg", UriKind.Relative));
42 IntoSong("如果能在一起", "今天是情人节", "梁静茹", new Uri("SongCDImg/150_albumpic_35795_0.jpg", UriKind.Relative));
43 }
44 ///<summary>
45 /// 新增数据
46 ///</summary>
47 ///<param name="SongName">歌曲名</param>
48 ///<param name="SongCDName">专辑名</param>
49 ///<param name="Songer">歌手</param>
50 ///<param name="SongCDPic">专辑图片</param>
51 private void IntoSong(string SongName, string SongCDName, string Songer, Uri SongCDPic) {
52 this.songs.Add(new Song
53 {
54 GUID = Guid.NewGuid().ToString(),
55 SongName = SongName,
56 SongCDName = SongCDName,
57 SongCDPic = SongCDPic,
58 Songer = Songer
59 });
60 }
61
62 }
63 ///<summary>
64 /// 分组实现
65 ///</summary>
66 ///<typeparam name="T"></typeparam>
67 public class Group<T> : IEnumerable<T> {
68 public string Key { get; set; }
69 public IList<T> Items { get; set; }
70 public Group(string key, IEnumerable<T> Item) {
71 this.Key = key;
72 this.Items = new List<T>(Item);
73 }
74 public override bool Equals(object obj)
75 {
76 Group<T> NowGroup = obj as Group<T>;
77 return (NowGroup != null) && (this.Key.Equals(NowGroup.Key));
78 }
79 #region
80 public IEnumerator<T> GetEnumerator() {
81 return this.Items.GetEnumerator();
82 }
83 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
84 return this.Items.GetEnumerator();
85 }
86 #endregion
87 }
88 }


界面设计

 

View Code
  1 <phone:PhoneApplicationPage 
2 x:Class="PhoneAppMusic.MainPage"
3 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
4 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5 xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
6 xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
7 xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
8 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
9 mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="696"
10 FontFamily="{StaticResource PhoneFontFamilyNormal}"
11 FontSize="{StaticResource PhoneFontSizeNormal}"
12 Foreground="{StaticResource PhoneForegroundBrush}"
13 SupportedOrientations="Portrait" Orientation="Portrait"
14 shell:SystemTray.IsVisible="True" xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit">
15
16 <phone:PhoneApplicationPage.Resources>
17 <DataTemplate x:Key="GroupSongHeader">
18 <Border Background="{StaticResource PhoneAccentBrush}"
19 Height="50"
20 >
21 <TextBlock Text="{Binding Key}"
22 FontSize="{StaticResource PhoneFontSizeLarge}"
23 ></TextBlock>
24 </Border>
25 </DataTemplate>
26 <DataTemplate x:Key="GroupSongItem">
27 <Border Background="{StaticResource PhoneAccentBrush}"
28 Margin="3"
29 Height="50"
30 Width="300"
31 >
32 <TextBlock Text="{Binding Key}"
33 FontSize="{StaticResource PhoneFontSizeLarge}"
34 Foreground="White"
35 ></TextBlock>
36 </Border>
37 </DataTemplate>
38 <DataTemplate x:Key="SongItem">
39 <Border
40 Margin="3"
41 >
42 <StackPanel Orientation="Vertical">
43 <StackPanel Orientation="Horizontal">
44 <TextBlock Text="歌曲:"
45 Margin="20,0,0,0"
46 FontSize="{StaticResource PhoneFontSizeMediumLarge}"
47 ></TextBlock>
48 <TextBlock Text="{Binding SongName}"
49 Margin="20,0,0,0"
50 FontSize="{StaticResource PhoneFontSizeLarge}"
51 ></TextBlock>
52 </StackPanel>
53
54 <StackPanel Orientation="Horizontal">
55 <TextBlock Text="歌手:"
56 Margin="20,0,0,0"
57 FontSize="{StaticResource PhoneFontSizeMediumLarge}"
58 ></TextBlock>
59 <TextBlock Text="{Binding Songer}"
60 Margin="20,0,0,0"
61 FontSize="{StaticResource PhoneFontSizeMediumLarge}"
62 ></TextBlock>
63 </StackPanel>
64 </StackPanel>
65 </Border>
66 </DataTemplate>
67 </phone:PhoneApplicationPage.Resources>
68 <!--LayoutRoot 是放置所有頁面的根資料格-->
69 <Grid x:Name="LayoutRoot" Background="Transparent">
70 <Grid.RowDefinitions>
71 <RowDefinition Height="Auto"/>
72 <RowDefinition Height="*"/>
73 </Grid.RowDefinitions>
74
75 <!--TitlePanel 包含應用程式的名稱和頁面標題-->
76 <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
77 <TextBlock x:Name="ApplicationTitle" Text="我的應用程式" Style="{StaticResource PhoneTextNormalStyle}"/>
78 <TextBlock x:Name="PageTitle" Text="歌曲列表" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
79 </StackPanel>
80
81 <!--ContentPanel - 其他內容置於此-->
82 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
83 <toolkit:LongListSelector
84 x:Name="MusicList"
85 ItemTemplate="{StaticResource SongItem}"
86 GroupItemTemplate="{StaticResource GroupSongItem}"
87 GroupHeaderTemplate="{StaticResource GroupSongHeader}"
88 >
89
90 </toolkit:LongListSelector>
91
92 </Grid>
93
94 </Grid>
95
96 <!--顯示 ApplicationBar 使用方式的程式碼範例-->
97 <phone:PhoneApplicationPage.ApplicationBar>
98 <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True">
99
100 <shell:ApplicationBar.MenuItems>
101 <shell:ApplicationBarMenuItem Text="按专辑" x:Name="SoftCDName" Click="SoftCDName_Click"/>
102 <shell:ApplicationBarMenuItem Text="按歌手" x:Name="SoftSonger" Click="SoftSonger_Click"/>
103 </shell:ApplicationBar.MenuItems>
104 </shell:ApplicationBar>
105 </phone:PhoneApplicationPage.ApplicationBar>
106
107 </phone:PhoneApplicationPage>

数据查询:

View Code
 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Net;
5 using System.Windows;
6 using System.Windows.Controls;
7 using System.Windows.Documents;
8 using System.Windows.Input;
9 using System.Windows.Media;
10 using System.Windows.Media.Animation;
11 using System.Windows.Shapes;
12 using Microsoft.Phone.Controls;
13
14 namespace PhoneAppMusic
15 {
16 public partial class MainPage : PhoneApplicationPage
17 {
18 SongHelper SongHelper = null;
19 // 建構函式
20 public MainPage()
21 {
22 SongHelper = new PhoneAppMusic.SongHelper();
23 InitializeComponent();
24 this.Loaded+=new RoutedEventHandler(MainPage_Loaded);
25 }
26 void MainPage_Loaded(object sender, RoutedEventArgs e) {
27 SongCDSoft();
28 }
29 ///<summary>
30 /// 按专辑
31 ///</summary>
32 void SongCDSoft()
33 {
34 var Music = from S in SongHelper.Songs
35 group S by S.SongCDName into NewMusic
36 select new Group<Song>(NewMusic.Key, NewMusic);
37
38 this.MusicList.ItemsSource = Music;
39 }
40 ///<summary>
41 /// 按歌手
42 ///</summary>
43 void SongerSoft()
44 {
45 var Music = from S in SongHelper.Songs
46 group S by S.Songer into NewMusic
47 select new Group<Song>(NewMusic.Key, NewMusic);
48
49 this.MusicList.ItemsSource = Music;
50 }
51
52 private void SoftCDName_Click(object sender, EventArgs e)
53 {
54 SongCDSoft();
55 }
56
57 private void SoftSonger_Click(object sender, EventArgs e)
58 {
59 SongerSoft();
60 }
61
62 }
63 }




转载于:https://www.cnblogs.com/lsmayday/archive/2011/11/10/2244513.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值