




已阅读5页,还剩16页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
我们紧接着上篇,这篇将介绍如何使用文件选择器访问和保存文件-我是华丽的分割线-此示例演示用户如何使用文件选择器选择您的应用程序文件和文件夹,根据用户指定的名称,文件类型和文件保存的位置。这个示例使用 Windows.Storage.Pickers API。本篇将介绍如下四个方面:a)让用户选择一个文件b)让用户选择多个文件c)让用户选择一个文件夹d)让用户保存文件和指定的名称,文件类型和/或保存位置我们的创建的步骤如下: 1)为了组织文件方便,我们先建一个文件夹 FilePicker2)向文件夹中添加如下四个文件:PickAFolder.xaml,PickASinglePhoto.xaml,PickMultipleFiles.xaml,SaveAFile.xaml创建方式如图:3)此时的解决方案结构如下:4)向我们的 DataSource 添加导航所需要的信息修改我们的 SampleDataSource.cs 文件中的 SampleDataSource 类中的代码,代码如下: View Code public sealed class SampleDataSourceprivate static SampleDataSource _sampleDataSource = new SampleDataSource(); private ObservableCollection _allGroups = new ObservableCollection();public ObservableCollection AllGroupsget return this._allGroups; public static IEnumerable GetGroups(string uniqueId)if (!uniqueId.Equals(“AllGroups“) throw new ArgumentException(“Only AllGroups is supported as a collection of groups“);return _sampleDataSource.AllGroups;public static SampleDataGroup GetGroup(string uniqueId)/ Simple linear search is acceptable for small data setsvar matches = _sampleDataSource.AllGroups.Where(group) = group.UniqueId.Equals(uniqueId);if (matches.Count() = 1) return matches.First();return null;public static SampleDataItem GetItem(string uniqueId)/ Simple linear search is acceptable for small data setsvar matches = _sampleDataSource.AllGroups.SelectMany(group = group.Items).Where(item) = item.UniqueId.Equals(uniqueId);if (matches.Count() = 1) return matches.First();return null;public SampleDataSource()var group1 = new SampleDataGroup(“FilePicker“,“Use Windows.Storage.Pickers API“,“Access and save files using the file picker“,“Assets/FilePicker.jpg“,“);group1.Items.Add(new SampleDataItem(“FilePicker-PickASinglePhoto“,“Pick a single photo“,“only one file can selected,file type is jpg,jpeg,png“,“Assets/FilePicker.jpg“,“only one file can selected “,“,group1,typeof(PickASinglePhoto);group1.Items.Add(new SampleDataItem(“FilePicker-PickMultipleFiles“,“Pick multiple files“,“you can pick multiple files“,“Assets/FilePicker.jpg“,“pick multiple files“,“,group1,typeof(PickMultipleFiles);group1.Items.Add(new SampleDataItem(“FilePicker-PickAFolder“,“Pick a folder“,“you can pick a folder“,“Assets/FilePicker.jpg“,“Pick a folder“,“,group1,typeof(PickAFolder);group1.Items.Add(new SampleDataItem(“FilePicker-SaveAFile“,“Save a file“,“you can save a file“,“Assets/FilePicker.jpg“,“Save a file“,“,group1,typeof(SaveAFile);this.AllGroups.Add(group1);5)我们的导航这样就做好了,效果图 :点击,出现如下图片:6)我们开始我们的任务:让用户选择一个文件使用的 FileOpenPicker 的 PickSingleFileAsync 方法来调用一个文件选择器窗口,让用户选择一个单一的文件。修改 PickASinglePhoto.xaml 的代码:View Code Prompt the user to pick a single photo.修改后台代码:View Code public sealed partial class PickASinglePhoto : Pagepublic PickASinglePhoto()this.InitializeComponent();PickAFileButton.Click += new RoutedEventHandler(PickAFileButton_Click);/ / Invoked when this page is about to be displayed in a Frame./ / Event data that describes how this page was reached. The Parameter/ property is typically used to configure the tected override void OnNavigatedTo(NavigationEventArgs e)private async void PickAFileButton_Click(object sender, RoutedEventArgs e)FileOpenPicker openPicker = new FileOpenPicker();/设置呈现视图模式为缩略图openPicker.ViewMode = PickerViewMode.Thumbnail;/设置文件选择器打开时的起始位置,这里设置为图片库openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;/添加文件过滤器openPicker.FileTypeFilter.Add(“.jpg“);openPicker.FileTypeFilter.Add(“.jpeg“);openPicker.FileTypeFilter.Add(“.png“);/异步调用 PickSingleFileAsyncStorageFile file = await openPicker.PickSingleFileAsync();if (file != null)OutputTextBlock.Text = “Picked photo: “ + file.Name;elseOutputTextBlock.Text = “Operation cancelled.“;效果图如下:7)让用户选择多个文件使用的 FileOpenPicker 的 PickMultipleFilesAsync 方法来调用一个文件选择器窗口,让用户选择多个文件。修改我们的 PickMultipleFiles.xaml代码如下:View Code Prompt the user to pick one or more files.后台代码:View Code public sealed partial class PickMultipleFiles : Pagepublic PickMultipleFiles()this.InitializeComponent();PickFilesButton.Click += new RoutedEventHandler(PickFilesButton_Click);/ / Invoked when this page is about to be displayed in a Frame./ / Event data that describes how this page was reached. The Parameter/ property is typically used to configure the tected override void OnNavigatedTo(NavigationEventArgs e)private async void PickFilesButton_Click(object sender, RoutedEventArgs e)FileOpenPicker openPicker = new FileOpenPicker();/设置呈现视图模式为列表openPicker.ViewMode = PickerViewMode.List;/设置文件选择器打开时的起始位置,这里设置为文档库openPicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;/不过滤任何类型openPicker.FileTypeFilter.Add(“*“);/调用 PickMultipleFilesAsync 获得选择文件列表IReadOnlyList files = await openPicker.PickMultipleFilesAsync();if (files.Count 0)StringBuilder output = new StringBuilder(“Picked files:n“);foreach (StorageFile file in files)output.Append(file.Name + “n“);OutputTextBlock.Text = output.ToString();elseOutputTextBlock.Text = “Operation cancelled.“; 效果图:8)让用户选择一个文件夹使用的 FolderPicker 的 PickSingleFolderAsync 方法来调用一个文件选择器窗口,让用户选择一个文件夹。修改我们的 PickAFolder.xaml 的 xmal 代码:View Code Prompt the user to pick a folder so its contents can be accessed later.修改后台代码:View Code public sealed partial class PickAFolder : Pagepublic PickAFolder()this.InitializeComponent();PickFolderButton.Click += new RoutedEventHandler(PickFolderButton_Click);/ / Invoked when this page is about to be displayed in a Frame./ / Event data that describes how this page was reached. The Parameter/ property is typically used to configure the tected override void OnNavigatedTo(NavigationEventArgs e)private async void PickFolderButton_Click(object sender, RoutedEventArgs e)FolderPicker folderPicker = new FolderPicker();folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;folderPicker.FileTypeFilter.Add(“.jpg“);/调用 PickSingleFolderAsync 选择文件夹StorageFolder folder = await folderPicker.PickSingleFolderAsync();if (folder != null)/ 提供对列表的访问,使用该列表,应用程序可以跟踪最近访问的文件或位置和将来要存储的文件或位置StorageApplicationPermissions.FutureAccessList.AddOrReplace(“PickedFolderToken“, folder);OutputTextBlock.Text = “Picked folder: “ + folder.Name;elseOutputTextBlock.Text = “Operation cancelled.“;效果图:9)让用户保存文件和指定的名称,文件类型和/或保存位置使用的 FileSavePicker 的 PickSaveFileAsync 方法来调用一个文件选择器窗口,让用户保存文件。修改我们的 SaveAFile.xaml 的代码:View Code Prompt the user to save a file.修改后台代码:View Code public sealed partial class SaveAFile : Pagepublic SaveAFile()this.InitializeComponent();SaveFileButton.Click += new RoutedEventHandler(SaveFileButton_Click);/ / Invoked when this page is about to be displayed in a Frame./ / Event data that describes how this page was reached. The Parameter/ property is typically used to configure the tected override void OnNavigatedTo(NavigationEventArgs e)private async void SaveFileButton_Click(object sender, RoutedEventArgs e)FileSavePicker savePicker = new FileSavePicker();savePicke
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 烘焙店投资加盟合同范本
- 混凝土配料劳务合同范本
- 消防检测合同的补充协议
- 洗车店急需转让合同范本
- 漂流项目运营协议书范本
- 煤气管道转让协议书模板
- 泉州串串香加盟合同范本
- 物业顾问合同协议书范本
- 砂滤池清洗回填合同范本
- 铺面场地出租协议书模板
- 四川省成都市2025年中考语文真题试卷及答案
- 燃气工程规范化管理课件
- 光伏电站安全管理课件
- 编辑校对员笔试试题及答案
- 安吉公司团建十人活动方案
- 农民手机应用培训
- 湖南省邵阳市海谊中学2024-2025学年高一上学期期末考试历史试题(原卷版及答案)
- 煤场考试试题及答案
- 2025年河南省中考数学真题试卷及答案解析
- 2025春季学期国开电大专科《建筑施工技术》一平台在线形考(形考任务1至4)试题及答案
- 2025广东梅州职业技术学院教师招聘考试试题
评论
0/150
提交评论