zoukankan      html  css  js  c++  java
  • 27、FilePicker

    1、Pick a single photo :

         提示用户选择一张单独的图片。

    操作截图:

    点击按钮,打开 win8 的文件选择器 :

    显示结果 :

    相应的 xaml :

    <Button Grid.Row="1" x:Name="PickAFileButton" Content="Pick photo" Click="PickAFileButton_Click"/>
     <TextBlock x:Name="OutputTextBlock" TextWrapping="Wrap" />

    相应的 C# 代码 :

           private async void PickAFileButton_Click(object sender, RoutedEventArgs e)
            {
                   //  表示允许用户选择和打开文件的 UI 元素。
                      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 对象。 StorageFile file = await openPicker.PickSingleFileAsync(); if (file != null) { // Application now has read/write access to the picked file OutputTextBlock.Text = "Picked photo: " + file.Name; } else { OutputTextBlock.Text = "Operation cancelled."; } }


    2、 Pick multiple files :

         选择多个文件 :

    单击按钮,打开文件选择框 :

    选择两个文件,打印结果 :

    页面的 xaml :

     <Button Grid.Row="1" x:Name="PickFilesButton" Content="Pick files"  Click="PickFilesButton_Click"/>
    
    //输出结果
     <TextBlock x:Name="OutputTextBlock" TextWrapping="Wrap" />


    相应的单击事件 :

     private async void PickFilesButton_Click(object sender, RoutedEventArgs e)
            {
                    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");
                        }
                        OutputTextBlock.Text = output.ToString();
                    }
    else
           
             {
                      
      OutputTextBlock.Text
    = "Operation cancelled.";
      
                  }
    }

    3、Pick a folder :

      提示用户选择一个文件夹,所以其内容可以在之后访问。

    操作 :

    单击按钮,调用系统的选择文件夹的选择器 :

    结果 :

    页面的 xaml :

     <Button Grid.Row="1" x:Name="PickFolderButton" Content="Pick folder"  Click="PickFolderButton_Click"/>
    <TextBlock x:Name="OutputTextBlock"  TextWrapping="Wrap" />

    相应的 C# 代码 :

           private async void PickFolderButton_Click(object sender, RoutedEventArgs e)
            {
                    FolderPicker folderPicker = new FolderPicker();
                  
                   // 获取或设置文件夹选取器在其中查找要呈现给用户的文件夹的初始位置。
                      folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;//Windows 桌面。
                       folderPicker.FileTypeFilter.Add(".docx");
                    folderPicker.FileTypeFilter.Add(".xlsx");
                    folderPicker.FileTypeFilter.Add(".pptx");
    
                  // 显示 folderPicker 对象,以便用户可以选择文件夹。
                    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 : 提供对列表的访问,使用该列表,
                         //应用程序可跟踪最近访问的文件和/或位置或存储将来要访问的文件和/或位置。                    
                         // 将新的存储项添加到访问列表上,或替换指定的项(如果它已存在于列表中)。
    // token : 新存储项关联的标记。如果访问列表仍包含含有此标记的存储项,则新建项将替换现有项。
    StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", folder);
    OutputTextBlock.Text
    = "Picked folder: " + folder.Name; } else { OutputTextBlock.Text = "Operation cancelled."; } } }

    4、Save a file :

         提示用户保存一个文件。

         操作截图 :

    点击按钮 :

    点击保存按钮,输出结果 :

    页面的 xaml :

     <Button x:Name="SaveFileButton" Content="Save file" Click="SaveFileButton_Click"/>
    
     <TextBlock x:Name="OutputTextBlock"  />

    相应的 C# :

            private async void SaveFileButton_Click(object sender, RoutedEventArgs e)
            {
                    FileSavePicker savePicker = new FileSavePicker();
                    
                    // 获取或设置文件保存选取器向用户建议的位置,作为要保存文件的位置。
                       savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
               
    // 下拉文件类型用户可以保存文件
    savePicker.FileTypeChoices.Add("Plain Text", new List<string>() { ".txt" });
    // 获取或设置文件保存选取器向用户建议的文件名。 //用作默认文件名,如果用户没有输入文件名或选择一个文件的 savePicker.SuggestedFileName = "New Document"; // 显示文件选取器,以便用户可以保存文件和设置要保存的文件的文件名、扩展和位置。 StorageFile file = await savePicker.PickSaveFileAsync(); if (file != null) { // 防止更新远程版本的文件,直到我们完成 CompleteUpdatesAsync 进行更改和调用。 CachedFileManager.DeferUpdates(file);
    //  WriteTextAsync(IStorageFile file, string contents) :  将文本写入指定文件。 await FileIO.WriteTextAsync(file, file.Name);
    // 让 Windows 知道我们完成更改文件,其他应用程序可以更新远程版本的文件。 // 完成更新可能需要 Windows 要求用户输入。 // 启动对指定的文件的更新。 FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file); if (status == FileUpdateStatus.Complete) { OutputTextBlock.Text = "File " + file.Name + " was saved."; } else { OutputTextBlock.Text = "File " + file.Name + " couldn't be saved."; } } else { OutputTextBlock.Text = "Operation cancelled."; } }


    5、Open a cached file :

         这个示例讲述了如何在一个应用中用这个 Cached File Updater 协议给你的应用提供一个文件的最新版本。

          这个示例要求你有 File picker contracts sample 工程(下一讲会介绍)。

        步骤 :

             1)点击 "Pick cached file" 按钮以加载 file picker

             2) 选择 “File picker contracts sample”  工程。(代码下载后会看到,和本工程同级)

             3)选择列表中的第三个场景“3) Pick cached file”

             4)单击 “Add file to basket” 然后 在这个 file picker 中 单击 “Open”

             5)单击 “Output lastest version” 按钮。

     操作截图 :

    单击 “Pick cached file” :

    点击打开 ,结果输出:

    页面的 xaml :

     <Button x:Name="PickFileButton" Content="Pick cached file"  Click="PickFileButton_Click"/>
              
    
     <Button x:Name="OutputFileButton" Content="Output latest version" IsEnabled="False"  Click="OutputFileButton_Click"/>
    
    
     <TextBlock x:Name="OutputFileName" TextWrapping="Wrap"  />
             
     <TextBlock x:Name="OutputFileContent"  TextWrapping="Wrap" />

    相应的 C# :

    string fileToken = string.Empty;
    
      private async void PickFileButton_Click(object sender, RoutedEventArgs e)
            {
                //FutureAccessList : 获取一个对象,该对象代表应用程序维护的一个列表,
                //该列表使应用程序可以存储文件和/或位置 (文件夹) 并在未来轻松地访问这些项。
                //删除访问列表中的所有存储项。            
                Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Clear();
                fileToken = string.Empty;
            
                    FileOpenPicker openPicker = new FileOpenPicker();
                    openPicker.FileTypeFilter.Add(".txt");
                    StorageFile file = await openPicker.PickSingleFileAsync();
                    if (file != null)
                    {
                        fileToken = Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(file);
                        OutputFileButton.IsEnabled = true;
                        OutputFileAsync(file);
                    }
                    else
                    {
                        //"Operation cancelled
                    }
                
            }
             private async void OutputFileAsync(StorageFile file)
            {
                string fileContent = await FileIO.ReadTextAsync(file);
                OutputFileName.Text = String.Format(@"Received file: {0}", file.Name);
                OutputFileContent.Text = String.Format(@"File content:{0}{1}", System.Environment.NewLine, fileContent);
            }
            private async void OutputFileButton_Click(object sender, RoutedEventArgs e)
            {
                if (!String.IsNullOrEmpty(fileToken))
                {
                    // Windows 将调用服务器程序来更新本地版本的文件
                    try
                    {
    
                     // 检索列表中的指定 StorageFile。此方法成功完成时,它返回与指定的 token 关联的 StorageFolder。
                        StorageFile file = await Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.GetFileAsync(fileToken);
                        OutputFileAsync(file);
                    }
                    catch (UnauthorizedAccessException)
                    {
                       //访问被拒绝
                    }
                }
            }

    6、Update a cached file :

         本示例演示了如何通知你的应用程序当继承的 Cached File Updater 协议你已经更改了一个文件。

         步骤 :

         1)单击 “Get save file” 按钮,加载 file save picker

         2)  选择 “File picker contracts sample” 应用从这个位置列表中。

         3) 选择场景 " 3) Save to a  cached file"

         4) 单击 Picker 中的 Save 按钮

         5)单击 "Write to file" 按钮

    操作截图 :

    点击 "Get save file" :

    点击保存按钮,结果 :

    点击 “Write to file” 按钮 :

    页面的 xaml :

     <Button x:Name="SaveFileButton" Content="Get save file"  Click="SaveFileButton_Click"/>
           
    
     <Button x:Name="WriteFileButton" Content="Write to file" IsEnabled="False"  Click="WriteFileButton_Click"/>
    <TextBlock x:Name="OutputFileName" TextWrapping="Wrap"  />
         
    <TextBlock x:Name="OutputFileContent" TextWrapping="Wrap"  />

    相应的  C# :

     string fileToken = string.Empty;
    
    private async void SaveFileButton_Click(object sender, RoutedEventArgs e)
            {
                  FileSavePicker savePicker = new FileSavePicker();                
                  
    // 下拉文件类型用户可以保存文件 savePicker.FileTypeChoices.Add("Plain Text", new List<string>() { ".txt" });
    // 默认文件名,如果用户不类型或选择一个文件的一个替代 savePicker.SuggestedFileName = "New Document"; // 成功完成对此方法的调用时,它会返回为表示已保存文件而创建的 storageFile 对象。 //此 storageFile 匹配用户指定的那些的文件名、扩展和位置,但该文件不含内容。 //若要保存文件的内容,应用程序必须将该内容写入此 StorageFile file = await savePicker.PickSaveFileAsync(); if (file != null) { //向访问列表添加新的存储项。返回结果:
     应用程序稍后可用以检索存储项的标记。
    fileToken
    = Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(file); OutputFileName.Text = file.Name;
    WriteFileButton.IsEnabled = true; } else { //Operation cancelled } }
       private async void WriteFileButton_Click(object sender, RoutedEventArgs e)
            {
                if (!String.IsNullOrEmpty(fileToken))
                {
                    StorageFile file = await Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.GetFileAsync(fileToken);
                   
                   // 防止更新远程版本的文件,直到我们完成CompleteUpdatesAsync进行更改和调用。
                     CachedFileManager.DeferUpdates(file);
                    
                   
                 // AppendTextAsync(IStorageFile file, string contents) : 将文本追加到指定文件。
                    await FileIO.AppendTextAsync(file, String.Format(@"{0}Text Added @ {1}.", System.Environment.NewLine, DateTime.Now.ToString()));
    
                 // 让 Windows 知道我们完成改变该文件,以便服务器应用程序可以更新远程版本的文件。
                   //完成更新可能需要Windows要求用户输入。   
    FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
    switch (status) { case FileUpdateStatus.Complete: OutputFileAsync(file); break; case FileUpdateStatus.CompleteAndRenamed:
                           
    // 将新的存储项添加到访问列表上,或替换指定的项(如果它已存在于列表中)。
    Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.AddOrReplace(fileToken, file); OutputFileAsync(file); break; default: break; } } }
           private async void OutputFileAsync(StorageFile file)
            {
                string fileContent = await FileIO.ReadTextAsync(file);
                OutputFileName.Text = String.Format(@"Received file: {0}", file.Name);
                OutputFileContent.Text = String.Format(@"File content:{0}{1}", System.Environment.NewLine, fileContent);
            }
  • 相关阅读:
    React Native-安卓环境的搭建
    python爬虫学习之日志记录模块
    Python爬虫学习之正则表达式爬取个人博客
    eclipse运行spark程序时日志颜色为黑色的解决办法
    python爬虫学习之爬取全国各省市县级城市邮政编码
    python 字典详细使用
    python爬虫学习之查询IP地址对应的归属地
    python jieba库的基本使用
    Eclipse环境搭建并且运行wordcount程序
    Hadoop2.0伪分布式平台环境搭建
  • 原文地址:https://www.cnblogs.com/hebeiDGL/p/2704434.html
Copyright © 2011-2022 走看看