在windows store应用程序的listview中获取文件的显示名称

时间:2015-08-07 08:43:47

标签: c# windows listview windows-store-apps

我正在尝试显示用户在列表视图中单击的文件的名称,但问题是它显示的是单击的项目类型,即单击了Windows.Storage.StorageFile。

代码:

<Page
    x:Class="Notes.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Notes"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <GridView Background="White">


        <Button x:Name="Button_Create" Content="Create" Background="Black" Height="104" Width="188" FontSize="32" Click="ButtonCreate_Click"/>
        <ListView IsItemClickEnabled="True" x:Name="filesListView" Foreground="Gray" HorizontalAlignment="Left" SelectionMode="None" ItemClick="filesListView_ItemClick">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding DisplayName}" />
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </GridView>

</Page>

和MainPage.xaml.cs的代码

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;

using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.Storage.BulkAccess;
using Windows.Storage.Streams;
using Windows.UI.Popups;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;
//using System.Deployment.Application;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace Notes
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
            //getFolders();
            getFoldersFrommCurrentDirectory();
        }

        private async void ButtonCreate_Click(object sender, RoutedEventArgs e)
        {
            StorageFolder folder = await KnownFolders.MusicLibrary.CreateFolderAsync("Notes", CreationCollisionOption.OpenIfExists);

            //create a file in that folder

            StorageFile file = await folder.CreateFileAsync("MyNotes.txt", CreationCollisionOption.GenerateUniqueName);

            //write to that file

            await Windows.Storage.FileIO.WriteTextAsync(file, "My notes file");

            getFoldersFrommCurrentDirectory();
        }

        private async void getFoldersFrommCurrentDirectory()
        {
            //get folders from current application folder
            StorageFolder folder = await KnownFolders.MusicLibrary.CreateFolderAsync("Notes", CreationCollisionOption.OpenIfExists);
            IReadOnlyList<StorageFile> files = await folder.GetFilesAsync();
            filesListView.ItemsSource = files;
        }




        private async void filesListView_ItemClick(object sender, ItemClickEventArgs e)
        {
            var message = new MessageDialog(e.ClickedItem+ "clicked", "listview item clicked");
            await message.ShowAsync();
        }


        //private async void getFolders()
        //{
        //    //get a folder from known folders
        //    IReadOnlyList<StorageFolder> folders = await KnownFolders.MusicLibrary.GetFoldersAsync();
        //    filesListView.ItemsSource = folders;
        //}


    }


}

1 个答案:

答案 0 :(得分:0)

尝试将filesListView_ItemClick更改为

var name = (sender as StorageFile).Name;
var message = new MessageDialog(name + "clicked", "listview item clicked");
await message.ShowAsync();