简体   繁体   English

音乐库列表应用-UWP / C#

[英]Music Library List App - UWP / C#

I comment'm developing an application in C # for UWP, I am new to this and I'm willing to show the list of songs with his album and artist, I've done in a ListView with (ListView.items.add) together their properties, but this is not at all aesthetic. 我评论说正在为UWP用C#开发应用程序,对此我是陌生的,我愿意与他的专辑和歌手一起显示歌曲列表,我已经在ListView中完成了(ListView.items.add)它们的特性结合在一起,但这完全不是美学。 Binding is possible with? 绑定有可能吗? Could you help me? 你可以帮帮我吗?

 protected async override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        this.DataContext = this;


        StorageFolder storageFolder = KnownFolders.MusicLibrary;

        var files = await storageFolder.GetFilesAsync();


        foreach (var file in files)
        {
            var musicProperties = await file.Properties.GetMusicPropertiesAsync();

            var Artista = musicProperties.Artist;
            if (Artista == "")
            {
                Artista = "Artista desconocido";
            }
            string fileName = file.DisplayName;

            listView.Items.Add(file.DisplayName + "-" + Artista  );

        }


    }

Binding is possible with? 绑定有可能吗?

Yes, binding is possible and I think binding is a better way than adding items to ListView in the code behind, you should be able to use DataTemplate to build the structure of each item and use ObservableCollection Class to provide notifications when items get added, removed or refreshed. 是的,可以进行绑定,我认为绑定是比在后面的代码中将项目添加到ListView更好的方法,您应该能够使用DataTemplate构建每个项目的结构,并使用ObservableCollection Class在添加,删除项目时提供通知或刷新。

For example here: 例如这里:

<ListView ItemsSource="{x:Bind MusicList}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="File Name: " />
                    <TextBlock Text="{Binding FileName}" />
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="Artist: " />
                    <TextBlock Text="{Binding Artist}" />
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="Album: " />
                    <TextBlock Text="{Binding Album}" />
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

And you need to create a data model class for these "FileName", "Artist", and "Album" properties: 您需要为以下“ FileName”,“ Artist”和“ Album”属性创建数据模型类:

public class MusicLib
{
    public string FileName { get; set; }
    public string Artist { get; set; }
    public string Album { get; set; }
}

code behind: 后面的代码:

private ObservableCollection<MusicLib> MusicList = new ObservableCollection<MusicLib>();

public MainPage()
{
    this.InitializeComponent();
}

protected override async void OnNavigatedTo(NavigationEventArgs e)
{
    StorageFolder musicLib = KnownFolders.MusicLibrary;
    var files = await musicLib.GetFilesAsync();
    foreach (var file in files)
    {
        var musicProperties = await file.Properties.GetMusicPropertiesAsync();
        var artist = musicProperties.Artist;
        if (artist == "")
            artist = "Artista desconocido";
        var album = musicProperties.Album;
        if (album == "")
            album = "Unkown";
        MusicList.Add(new MusicLib { FileName = file.DisplayName, Artist = artist, Album = album });
    }
}

I used your method to set the empty "Artist" and "Album" properties to certain values here, but you can also use Converter to do this work, for the information about using Converter , you can refer to IValueConverter interface , there is sample in this doc. 我使用您的方法在此处将空的“ Artist”和“ Album”属性设置为某些值,但是您也可以使用Converter来完成这项工作,有关使用Converter的信息,您可以参考IValueConverter接口 ,其中包含示例这个文件。

By the way, if you want to access the Music Library in your app, you need to enable the "Music Library" capability in your "Package.appxmanifest" file. 顺便说一句,如果要访问应用程序中的音乐库,则需要在“ Package.appxmanifest”文件中启用“音乐库”功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM