简体   繁体   English

在Windows应用商店中的列表视图中获取文件的显示名称

[英]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.

相关问题 如何获取Windows应用商店应用的本地化显示名称 - How to get localized display name for the windows store apps 如何从Windows Store应用程序中的操作获取方法名称 - How to Get Method Name From Action in Windows Store Application 获取Windows 8 Store / Metro应用程序中某些文件扩展名的所有路径 - Get all path of certain file extensions in windows 8 store / metro application 获取 Windows 用户显示名称 - Get Windows User Display Name 如何使用Windows窗体应用程序获取活动进程文件名? - How to get Active process file name using windows forms application? 如何从hWnd获取Windows 10商店应用程序的“应用程序名称”(例如Edge) - How to get the “Application Name” from hWnd for Windows 10 Store Apps (e.g. Edge) 在Windows Store应用程序中保存xml文件 - Save xml file in Windows Store application 在Windows 8 Store App中获取Listview ItemTemplate内部的文本块的值 - Get the value of textblocks inside Listview itemtemplate in windows 8 store app 在Windows窗体中显示当前打开文件的名称? - Display the name of the current open file in a Windows Form? 使用UWP商店应用程序锁定Windows 10工作站的显示 - Lock the display of windows 10 workstation using UWP store application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM