UWP C# Select 多个项目并将名称显示为列表视图中的单个项目
UWP C# Select Multiple items and display name as single items on Listview
大家好希望你们都度过了愉快的一周;
enter image description here我是 UWP 的新手,正在尝试创建一个非常简单的应用程序,首先我希望能够 select 任何一个或多个文件并在列表视图中列出名称,我非常接近,但是作为一个普通的 foreach 循环,它正确地添加了项目但是添加了名称请看下面的结果和我的代码。
Mainpage.xaml
<Grid x:Name="Output" Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Top">
<ListView x:Name="ListViewtouse" >
</ListView>
</Grid>
MainPage.xaml.cs
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.List;
openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
openPicker.FileTypeFilter.Add("*");
IReadOnlyList<StorageFile> files = await openPicker.PickMultipleFilesAsync();
if (files.Count > 0)
{
StringBuilder output = new StringBuilder();
// The StorageFiles have read/write access to the picked files.
// See the FileAccess sample for code that uses a StorageFile to read and write.
foreach (StorageFile file in files)
{
output.Append(file.Name + "\n");
ListViewtouse.Items.Add(output.ToString());
}
}
else
{
Console.WriteLine("Operation cancelled.");
}
结果:
file1 <<--- 列表视图项目 1
文件1
file2 <<--- 列表视图项目 2
文件1
file2 <<--- 列表视图项目 3
文件 3
对我来说这确实有道理,但是有没有办法让每个列表视图项目只有 1 个名称而不是将它们相加?我附上了一张图片以供进一步参考。
提前致谢。
此行为的原因是您在所有循环中都使用了 StringBuilder
对象,而没有重置 StringBuilder
对象。当您向 StringBuilder
对象添加新项目名称时,该对象将包含最后一个项目的名称。
您可以将该行代码移到 foreach 循环中。
像这样:
foreach (StorageFile file in files)
{
StringBuilder output = new StringBuilder();
output.Append(file.Name + "\n");
ListViewtouse.Items.Add(output.ToString());
}
大家好希望你们都度过了愉快的一周;
enter image description here我是 UWP 的新手,正在尝试创建一个非常简单的应用程序,首先我希望能够 select 任何一个或多个文件并在列表视图中列出名称,我非常接近,但是作为一个普通的 foreach 循环,它正确地添加了项目但是添加了名称请看下面的结果和我的代码。
Mainpage.xaml
<Grid x:Name="Output" Grid.Row="1" HorizontalAlignment="Left" VerticalAlignment="Top">
<ListView x:Name="ListViewtouse" >
</ListView>
</Grid>
MainPage.xaml.cs
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.List;
openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
openPicker.FileTypeFilter.Add("*");
IReadOnlyList<StorageFile> files = await openPicker.PickMultipleFilesAsync();
if (files.Count > 0)
{
StringBuilder output = new StringBuilder();
// The StorageFiles have read/write access to the picked files.
// See the FileAccess sample for code that uses a StorageFile to read and write.
foreach (StorageFile file in files)
{
output.Append(file.Name + "\n");
ListViewtouse.Items.Add(output.ToString());
}
}
else
{
Console.WriteLine("Operation cancelled.");
}
结果: file1 <<--- 列表视图项目 1
文件1 file2 <<--- 列表视图项目 2
文件1 file2 <<--- 列表视图项目 3 文件 3
对我来说这确实有道理,但是有没有办法让每个列表视图项目只有 1 个名称而不是将它们相加?我附上了一张图片以供进一步参考。
提前致谢。
此行为的原因是您在所有循环中都使用了 StringBuilder
对象,而没有重置 StringBuilder
对象。当您向 StringBuilder
对象添加新项目名称时,该对象将包含最后一个项目的名称。
您可以将该行代码移到 foreach 循环中。
像这样:
foreach (StorageFile file in files)
{
StringBuilder output = new StringBuilder();
output.Append(file.Name + "\n");
ListViewtouse.Items.Add(output.ToString());
}