选取器: 文件选取窗口, 文件夹选取窗口, 文件保存窗口
介绍
重新想象 Windows 8 Store Apps 之 选取器
FileOpenPicker - 选择一个文件或多个文件
FolderPicker - 选择一个文件夹
FileSavePicker - 保存文件到指定路径
示例
1、演示如何通过 FileOpenPicker 选择一个文件或多 个文件
Picker/FileOpenPickerDemo.xaml
<Page x:Class="XamlDemo.Picker.FileOpenPickerDemo" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.Picker" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="120 0 0 0"> <TextBlock Name="lblMsg" FontSize="14.667" /> <Button Name="btnPickFile" Content="pick a file" Click="btnPickFile_Click_1" Margin="0 10 0 0" /> <Button Name="btnPickFiles" Content="pick multiple files" Click="btnPickFiles_Click_1" Margin="0 10 0 0" /> </StackPanel> </Grid> </Page>
Picker/FileOpenPickerDemo.xaml.cs
/* * 演示如何通过 FileOpenPicker 选择一个文件或多个文件 * * FileOpenPicker - 文件选择窗口 * ViewMode - 文件选择窗口的视图模式,Windows.Storage.Pickers.PickerViewMode 枚举(List 或 Thumbnail) * SuggestedStartLocation - 文件选择窗口所显示的初始路径,Windows.Storage.Pickers.PickerLocationId 枚举 * DocumentsLibrary, ComputerFolder, Desktop,, Downloads, HomeGroup, MusicLibrary, PicturesLibrary,VideosLibrary * FileTypeFilter - 允许显示在文件选择窗口的文件类型集合 * CommitButtonText - 文件选择窗口的提交按钮的显示文本,此按钮默认显示的文本为“打开” * PickSingleFileAsync() - 弹出文件选择窗口,以让用户选择一个文件 * PickMultipleFilesAsync() - 弹出文件选择窗口,以让用户选择多个文件 */ using System; using System.Collections.Generic; using Windows.Storage; using Windows.Storage.AccessCache; using Windows.Storage.Pickers; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; namespace XamlDemo.Picker { public sealed partial class FileOpenPickerDemo : Page { public FileOpenPickerDemo() { this.InitializeComponent(); } private async void btnPickFile_Click_1(object sender, RoutedEventArgs e) { if (XamlDemo.Common.Helper.EnsureUnsnapped()) { // 选择一个文件 FileOpenPicker openPicker = new FileOpenPicker(); openPicker.CommitButtonText = "选中此文件"; openPicker.ViewMode = PickerViewMode.Thumbnail; openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary; openPicker.FileTypeFilter.Add(".jpg"); openPicker.FileTypeFilter.Add(".gif"); openPicker.FileTypeFilter.Add(".png"); // 弹出文件选择窗口 StorageFile file = await openPicker.PickSingleFileAsync(); // 用户在“文件选择窗口”中完成操作后,会返回对应的 StorageFile 对象 if (file != null) { lblMsg.Text = "选中文件: " + file.Name; } else { lblMsg.Text = "取消了"; } } } private async void btnPickFiles_Click_1(object sender, RoutedEventArgs e) { if (XamlDemo.Common.Helper.EnsureUnsnapped()) { // 选择多个文件 FileOpenPicker openPicker = new FileOpenPicker(); openPicker.ViewMode = PickerViewMode.List; openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary; openPicker.FileTypeFilter.Add("*"); // 弹出文件选择窗口 IReadOnlyList<StorageFile> files = await openPicker.PickMultipleFilesAsync(); // 用户在“文件选择窗口”中完成操作后,会返回对应的 StorageFile 对象 if (files.Count > 0) { lblMsg.Text = "选中文件: "; lblMsg.Text += Environment.NewLine; foreach (StorageFile file in files) { lblMsg.Text += (file.Name); lblMsg.Text += Environment.NewLine; } } else { lblMsg.Text = "取消了"; } } } } }
以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索windows
, 文件
, storage class
, using
, 窗口
, 选择
, picker
文件窗口
,以便于您获取更多的相关知识。
时间: 2025-01-30 05:09:42