重新想象 Windows 8 Store Apps (25) - 选取器: 文件选取窗口, 文件夹选取窗口, 文件保存窗口

简介: 原文:重新想象 Windows 8 Store Apps (25) - 选取器: 文件选取窗口, 文件夹选取窗口, 文件保存窗口[源码下载] 重新想象 Windows 8 Store Apps (25) - 选取器: 文件选取窗口, 文件夹选取窗口, 文件保存窗口 作者:webabcd介绍重新...
原文: 重新想象 Windows 8 Store Apps (25) - 选取器: 文件选取窗口, 文件夹选取窗口, 文件保存窗口

[源码下载]


重新想象 Windows 8 Store Apps (25) - 选取器: 文件选取窗口, 文件夹选取窗口, 文件保存窗口



作者:webabcd


介绍
重新想象 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 = "取消了";
                }
            }
        }
    }
}


2、演示如何通过 FolderPicker 选择一个文件夹
Picker/FolderPickerDemo.xaml

<Page
    x:Class="XamlDemo.Picker.FolderPickerDemo"
    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="btnPickFolder" Content="pick a folder" Click="btnPickFolder_Click_1" Margin="0 10 0 0 " />

        </StackPanel>
    </Grid>
</Page>

Picker/FolderPickerDemo.xaml.cs

/*
 * 演示如何通过 FolderPicker 选择一个文件夹
 * 
 * FolderPicker - 文件夹选择窗口
 *     ViewMode - 文件夹选择窗口的视图模式,Windows.Storage.Pickers.PickerViewMode 枚举(List 或 Thumbnail)
 *     SuggestedStartLocation - 文件夹选择窗口所显示的初始路径,Windows.Storage.Pickers.PickerLocationId 枚举
 *         DocumentsLibrary, ComputerFolder, Desktop,, Downloads, HomeGroup, MusicLibrary, PicturesLibrary,VideosLibrary
 *     FileTypeFilter - 允许显示在文件夹选择窗口的文件类型集合(只能显示符合要求的文件,但是无法选中)
 *     CommitButtonText - 文件夹选择窗口的提交按钮的显示文本,此按钮默认显示的文本为“选择这个文件夹”
 *     PickSingleFolderAsync() - 弹出文件夹选择窗口,以让用户选择一个文件夹
 */

using System;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace XamlDemo.Picker
{
    public sealed partial class FolderPickerDemo : Page
    {
        public FolderPickerDemo()
        {
            this.InitializeComponent();
        }

        private async void btnPickFolder_Click_1(object sender, RoutedEventArgs e)
        {
            if (XamlDemo.Common.Helper.EnsureUnsnapped())
            {
                // 选择一个文件夹
                FolderPicker folderPicker = new FolderPicker();
                folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;
                folderPicker.FileTypeFilter.Add(".docx");
                folderPicker.FileTypeFilter.Add(".xlsx");
                folderPicker.FileTypeFilter.Add(".pptx");

                // 弹出文件夹选择窗口
                StorageFolder folder = await folderPicker.PickSingleFolderAsync(); // 用户在“文件夹选择窗口”中完成操作后,会返回对应的 StorageFolder 对象
                if (folder != null)
                {
                    lblMsg.Text = "选中文件夹: " + folder.Name;
                }
                else
                {
                    lblMsg.Text = "取消了";
                }
            }
        }
    }
}


3、演示如何通过 FileSavePicker 保存文件到指定路径
Picker/FileSavePickerDemo.xaml

<Page
    x:Class="XamlDemo.Picker.FileSavePickerDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:XamlDemo.Pikcer"
    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="btnSaveFile" Content="save a file" Click="btnSaveFile_Click_1" Margin="0 10 0 0" />

        </StackPanel>
    </Grid>
</Page>

Picker/FileSavePickerDemo.xaml.cs

/*
 * 演示如何通过 FileSavePicker 保存文件到指定路径
 * 
 * FileSavePicker - 文件保存窗口
 *     SuggestedStartLocation - 文件保存窗口所显示的初始路径,Windows.Storage.Pickers.PickerLocationId 枚举
 *         DocumentsLibrary, ComputerFolder, Desktop,, Downloads, HomeGroup, MusicLibrary, PicturesLibrary,VideosLibrary
 *     SuggestedFileName - 需要保存的文件的默认文件名
 *     SuggestedSaveFile - 需要保存的文件的默认 StorageFile 对象
 *     FileTypeChoices - 可保存的扩展名集合
 *     DefaultFileExtension - 默认扩展名
 *     CommitButtonText - 文件保存窗口的提交按钮的显示文本,此按钮默认显示的文本为“保存”
 *     PickSaveFileAsync() - 弹出文件保存窗口,以让用户保存文件
 */

using System;
using System.Collections.Generic;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Provider;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;

namespace XamlDemo.Picker
{
    public sealed partial class FileSavePickerDemo : Page
    {
        public FileSavePickerDemo()
        {
            this.InitializeComponent();
        }

        private async void btnSaveFile_Click_1(object sender, RoutedEventArgs e)
        {
            if (XamlDemo.Common.Helper.EnsureUnsnapped())
            {
                FileSavePicker savePicker = new FileSavePicker();
                savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;

                // 在扩展名选择框中将会显示:文本(.txt)
                savePicker.FileTypeChoices.Add("文本", new List<string>() { ".txt" });
                savePicker.SuggestedFileName = "webabcdFileSavePicker";

                // 弹出文件保存窗口
                StorageFile file = await savePicker.PickSaveFileAsync(); // 用户在“文件保存窗口”中完成操作后,会返回对应的 StorageFile 对象
                if (file != null)
                {
                    /*
                     * 运行到此,只是在目标地址创建了一个没有任何内容的空白文件而已,接下来开始向文件写入内容
                     */

                    // 告诉 Windows ,从此时开始要防止其它程序更新指定的文件
                    CachedFileManager.DeferUpdates(file);

                    // 将指定的内容保存到指定的文件
                    string textContent = "I am webabcd";
                    await FileIO.WriteTextAsync(file, textContent);

                    // 告诉 Windows ,从此时开始允许其它程序更新指定的文件
                    FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
                    if (status == FileUpdateStatus.Complete)
                    {
                        lblMsg.Text = "文件 " + file.Name + " 保存成功";
                    }

                    lblMsg.Text += Environment.NewLine;
                    lblMsg.Text += "FileUpdateStatus: " + status.ToString();
                }
                else
                {
                    lblMsg.Text = "取消了";
                }
            }
        }
    }
}



OK
[源码下载]

目录
相关文章
|
10月前
|
安全 Windows
“由于启动计算机时出现了页面文件配置问题,Windows在你的计算机上创建了一个临时页面文件。。。”的问题解决
本文主要介绍了因清理电脑垃圾文件时误删虚拟内存导致的Windows页面文件配置问题,并提供了详细的解决步骤。问题表现为开机后出现临时页面文件创建的提示弹窗。解决方法包括通过控制面板或快捷键进入高级系统设置,进而调整虚拟内存设置:进入性能选项中的虚拟内存栏,选择自动管理所有驱动器的分页文件大小,最后确认并重启计算机以恢复正常运行。
7929 5
“由于启动计算机时出现了页面文件配置问题,Windows在你的计算机上创建了一个临时页面文件。。。”的问题解决
|
存储 UED Windows
Windows服务器上大量文件迁移方案
Windows服务器上大量文件迁移方案
1232 1
|
iOS开发 MacOS Windows
Mac air使用Boot Camp安装win10 ,拷贝 Windows 文件时出错
Mac air使用Boot Camp安装win10 ,拷贝 Windows 文件时出错
|
C# Windows
【Azure App Service】在App Service for Windows上验证能占用的内存最大值
根据以上测验,当使用App Service内存没有达到预期的值,且应用异常日志出现OutOfMemory时,就需要检查Platform的设置是否位64bit。
262 11
|
存储 开发框架 .NET
Windows IIS中asp的global.asa全局配置文件使用说明
Windows IIS中asp的global.asa全局配置文件使用说明
328 1
|
Java Windows
如何在windows上运行jar包/JAR文件 如何在cmd上运行 jar包 保姆级教程 超详细
本文提供了一个详细的教程,解释了如何在Windows操作系统的命令提示符(cmd)中运行JAR文件。
8856 1
|
Linux Windows
Windows系统批量创建文件夹的技巧
Windows系统批量创建文件夹的技巧
546 1
|
Windows
windows 文件夹视图全局生效
【8月更文挑战第31天】在 Windows 中,要使文件夹视图设置全局生效,请先在一个文件夹中设置视图模式和排序方式等,然后点击“查看”选项卡中的“选项”按钮,打开“文件夹选项”,切换到“查看”选项卡,点击“应用到文件夹”按钮以确认设置。这样,大多数文件夹将采用相同视图。不过,部分特殊文件夹可能不遵循此设置。
437 3
|
Java 应用服务中间件 开发工具
[App Service for Windows]通过 KUDU 查看 Tomcat 配置信息
[App Service for Windows]通过 KUDU 查看 Tomcat 配置信息
212 2
|
Java 应用服务中间件 Windows
【App Service for Windows】为 App Service 配置自定义 Tomcat 环境
【App Service for Windows】为 App Service 配置自定义 Tomcat 环境
170 2

热门文章

最新文章