[英]Get the display name of the file in the listview in windows store application
I am trying to display the name of the file which is clicked by the user in the listview but the problem is it is displaying the type of item clicked ie Windows.Storage.StorageFile clicked. 我正在尝试显示用户在列表视图中单击的文件名,但问题是它正在显示单击的项的类型,即Windows.Storage.StorageFile。
Code: 码:
<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>
and the code for MainPage.xaml.cs 和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;
//}
}
}
Try changing filesListView_ItemClick
to 尝试将
filesListView_ItemClick
更改为
var name = (sender as StorageFile).Name;
var message = new MessageDialog(name + "clicked", "listview item clicked");
await message.ShowAsync();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.