Metro style app 文件、文件夹的选择、文件的保存。

简介:

 

 Metro style app 文件、文件夹的选择、文件的保存。

 

1、选择单个文件

public  async void  PickAFile()
{
     FileOpenPicker openPicker = new  FileOpenPicker();
     openPicker.ViewMode = PickerViewMode.Thumbnail;
     openPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
     openPicker.FileTypeFilter.Add( ".jpg" );
     openPicker.FileTypeFilter.Add( ".jpeg" );
     openPicker.FileTypeFilter.Add( ".png" );
     StorageFile file = await openPicker.PickSingleFileAsync();
     if  (file != null )
     {
         // Application now has read/write access to the picked file
         msg = "Picked photo: "  + file.Name;
     }
     else
     {
         msg = "Operation cancelled." ;
     }
}

 

 

 

2、选择多个文件

public  async void  PickFiles()
{
     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( "Picked files:\n" );
         // Application now has read/write access to the picked file(s)
         foreach  (StorageFile file in  files)
         {
             output.Append(file.Name + "\n" );
         }
         msg = output.ToString();
     }
}

 

3、选择文件夹

public  async void  PickFolder()
{
     FolderPicker folderPicker = new  FolderPicker();
     folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;
     folderPicker.FileTypeFilter.Add( ".docx" );
     folderPicker.FileTypeFilter.Add( ".xlsx" );
     folderPicker.FileTypeFilter.Add( ".pptx" );
     folderPicker.FileTypeFilter.Add( ".jpg" );
     folderPicker.FileTypeFilter.Add( ".jpeg" );
     StorageFolder folder = await folderPicker.PickSingleFolderAsync();
     if  (folder != null )
     {
         // Application now has read/write access to all contents in the picked folder (including other sub-folder contents)
         StorageApplicationPermissions.FutureAccessList.AddOrReplace( "PickedFolderToken" , folder);
         msg = "Picked folder: "  + folder.Name;
     }
     else
     {
         msg = "Operation cancelled." ;
     }
 
}

 

 

4、保存文件

public  async void  SaveFile()
{
     FileSavePicker savePicker = new  FileSavePicker();
     savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
     // Dropdown of file types the user can save the file as
     savePicker.FileTypeChoices.Add( "Plain Text" , new  List< string >() { ".txt"  });
     // Default file name if the user does not type one in or select a file to replace
     savePicker.SuggestedFileName = "New Document" ;
     StorageFile file = await savePicker.PickSaveFileAsync();
     if  (file != null )
     {
         // Prevent updates to the remote version of the file until we finish making changes and call CompleteUpdatesAsync.
         CachedFileManager.DeferUpdates(file);
         // write to file
         await FileIO.WriteTextAsync(file, file.Name);
         // Let Windows know that we're finished changing the file so the other app can update the remote version of the file.
         // Completing updates may require Windows to ask for user input.
         FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
         if  (status == FileUpdateStatus.Complete)
         {
             msg = "File "  + file.Name + " was saved." ;
         }
         else
         {
             msg = "File "  + file.Name + " couldn't be saved." ;
         }
     }
}

 

下面是C++版本

1、选择单个文件 

void  BasicHandleCPP::MainPage::PickerAFile_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
     FileOpenPicker^ openPicker = ref  new  FileOpenPicker();
     openPicker->ViewMode = PickerViewMode::Thumbnail;
     openPicker->SuggestedStartLocation = PickerLocationId::PicturesLibrary;
     openPicker->FileTypeFilter->Append( ".jpg" );
     openPicker->FileTypeFilter->Append( ".jpeg" );
     openPicker->FileTypeFilter->Append( ".png" );
 
     create_task(openPicker->PickSingleFileAsync()).then([ this ](StorageFile^ file)
     {
         if  (file)
         {
             Display->Text = "Picked photo: "  + file->Name;
         }
         else
         {
             Display->Text = "Operation cancelled." ;
         }
     });
 
}

 

 

2、选择多个文件

void  BasicHandleCPP::MainPage::PickerFiles_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
     FileOpenPicker^ openPicker = ref  new  FileOpenPicker();
     openPicker->ViewMode = PickerViewMode::List;
     openPicker->SuggestedStartLocation = PickerLocationId::DocumentsLibrary;
     openPicker->FileTypeFilter->Append( "*" );
 
     create_task(openPicker->PickMultipleFilesAsync()).then([ this ](IVectorView<StorageFile^>^ files)
     {
         if  (files->Size > 0)
         {
             String^ output = "Picked files:\n" ;
             std::for_each(begin(files), end(files), [ this , &output](StorageFile ^file)
             {
                 output += file->Name + "\n" ;
             });
             Display->Text = output;
         }
         else
         {
             Display->Text = "Operation cancelled." ;
         }
     });
}

 

 

3、选择文件夹

void  BasicHandleCPP::MainPage::PickerFolder_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
     FolderPicker^ folderPicker = ref  new  FolderPicker();
     folderPicker->SuggestedStartLocation = PickerLocationId::Desktop;
 
     // Users expect to have a filtered view of their folders depending on the scenario.
     // For example, when choosing a documents folder, restrict the filetypes to documents for your application.
     folderPicker->FileTypeFilter->Append( ".docx" );
     folderPicker->FileTypeFilter->Append( ".xlsx" );
     folderPicker->FileTypeFilter->Append( ".pptx" );
 
     create_task(folderPicker->PickSingleFolderAsync()).then([ this ](StorageFolder^ folder)
     {
         if  (folder)
         {
             Display->Text = "Picked folder: "  + folder->Name;
         }
         else
         {
             Display->Text = "Operation cancelled." ;
         }
     });
}

 

4、保存文件

void  BasicHandleCPP::MainPage::SaveFile_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
     FileSavePicker^ savePicker = ref  new  FileSavePicker();
     savePicker->SuggestedStartLocation = PickerLocationId::DocumentsLibrary;
 
     auto plainTextExtensions = ref  new  Platform::Collections::Vector<String^>();
     plainTextExtensions->Append( ".txt" );
     savePicker->FileTypeChoices->Insert( "Plain Text" , plainTextExtensions);
     savePicker->SuggestedFileName = "New Document" ;
 
     create_task(savePicker->PickSaveFileAsync()).then([ this ](StorageFile^ file)
     {
         if  (file != nullptr)
         {
 
             // Prevent updates to the remote version of the file until we finish making changes and call CompleteUpdatesAsync.
             CachedFileManager::DeferUpdates(file);
             // write to file
             create_task(FileIO::WriteTextAsync(file, file->Name)).then([ this , file]()
             {
                 // Let Windows know that we're finished changing the file so the other app can update the remote version of the file.
                 // Completing updates may require Windows to ask for user input.
                 create_task(CachedFileManager::CompleteUpdatesAsync(file)).then([ this , file](FileUpdateStatus status)
                 {
                     if  (status == FileUpdateStatus::Complete)
                     {
                         Display->Text = "File "  + file->Name + " was saved." ;
                     }
                     else
                     {
                         Display->Text = "File "  + file->Name + " couldn't be saved." ;
                     }
                 });
             });
         }
         else
         {
             Display->Text = "Operation cancelled." ;
         }
     });
}

 

 

 

 

 

 

 本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/archive/2012/06/28/2567009.html,如需转载请自行联系原作者

 


目录
相关文章
|
2月前
|
移动开发 开发框架 小程序
uni-app:demo&媒体文件&配置全局的变量(三)
uni-app 是一个使用 Vue.js 构建多平台应用的框架,支持微信小程序、支付宝小程序、H5 和 App 等平台。本文档介绍了 uni-app 的基本用法,包括登录示例、媒体文件处理、全局变量配置和 Vuex 状态管理的实现。通过这些示例,开发者可以快速上手并高效开发多平台应用。
100 0
|
2月前
|
Linux 开发工具 数据安全/隐私保护
linux异常一:feng 不在 sudoers 文件中,此事将被报告。yum提示Another app is currently holding the yum lock; waiting for
这篇文章介绍了在CentOS 7系统中安装Docker时遇到的两个常见问题及其解决方法:用户不在sudoers文件中导致权限不足,以及yum被锁定的问题。
48 2
linux异常一:feng 不在 sudoers 文件中,此事将被报告。yum提示Another app is currently holding the yum lock; waiting for
|
2月前
|
Java Linux Apache
jar 解压app.jar到指定文件夹
要将 JAR 文件(如 `app.jar`)解压到指定文件夹,可使用 Java 自带的 `jar` 工具、Apache Ant、7-Zip 或 Python 脚本。方法包括命令行操作(如 `jar xf app.jar -C /path/to/destination/folder`)、Ant 构建文件、7-Zip 图形界面或命令行,以及 Python 的 `zipfile` 模块。选择适合的方法即可轻松完成解压。
159 3
|
4月前
|
JSON Linux 网络安全
【Azure 应用服务】如何从App Service for Linux 的环境中下载Container中非Home目录下的文件呢?
【Azure 应用服务】如何从App Service for Linux 的环境中下载Container中非Home目录下的文件呢?
|
4月前
|
API 网络架构
【Azure Logic App】在中国区的微软云服务上,使用逻辑应用是否可以下载SharePoint上的文件呢?
【Azure Logic App】在中国区的微软云服务上,使用逻辑应用是否可以下载SharePoint上的文件呢?
【Azure Logic App】在中国区的微软云服务上,使用逻辑应用是否可以下载SharePoint上的文件呢?
|
4月前
|
前端开发 JavaScript Linux
【Azure 应用服务】在Azure App Service for Linux环境中,部署的Django应用,出现加载css、js等静态资源文件失败
【Azure 应用服务】在Azure App Service for Linux环境中,部署的Django应用,出现加载css、js等静态资源文件失败
|
4月前
|
Java Linux 网络安全
【Azure 应用服务】App Service for Linux环境中,如何解决字体文件缺失的情况
【Azure 应用服务】App Service for Linux环境中,如何解决字体文件缺失的情况
|
4月前
|
Java Linux C++
【Azure 应用服务】App Service For Linux 部署Java Spring Boot应用后,查看日志文件时的疑惑
【Azure 应用服务】App Service For Linux 部署Java Spring Boot应用后,查看日志文件时的疑惑
|
2月前
|
小程序 JavaScript 前端开发
uni-app开发微信小程序:四大解决方案,轻松应对主包与vendor.js过大打包难题
uni-app开发微信小程序:四大解决方案,轻松应对主包与vendor.js过大打包难题
761 1
|
3天前
|
JSON 缓存 前端开发
HarmonyOS NEXT 5.0鸿蒙开发一套影院APP(附带源码)
本项目基于HarmonyOS NEXT 5.0开发了一款影院应用程序,主要实现了电影和影院信息的展示功能。应用包括首页、电影列表、影院列表等模块。首页包含轮播图与正在热映及即将上映的电影切换显示;电影列表模块通过API获取电影数据并以网格形式展示,用户可以查看电影详情;影院列表则允许用户选择城市后查看对应影院信息,并支持城市选择弹窗。此外,项目中还集成了Axios用于网络请求,并进行了二次封装以简化接口调用流程,同时添加了请求和响应拦截器来处理通用逻辑。整体代码结构清晰,使用了组件化开发方式,便于维护和扩展。 该简介概括了提供的内容,但请注意实际开发中还需考虑UI优化、性能提升等方面的工作。
41 11