背水一戰 Windows 10 (93) - 選取器: FileOpenPicker, FolderPicker, FileSavePicker

[源碼下載]


html

背水一戰 Windows 10 (93) - 選取器: FileOpenPicker, FolderPicker, FileSavePicker



做者:webabcd


介紹
背水一戰 Windows 10 之 選取器html5

  • FileOpenPicker(文件選取窗口)
  • FolderPicker(文件夾選取窗口)
  • FileSavePicker(文件保存窗口)



示例
一、演示如何經過 FileOpenPicker 選擇一個文件或多個文件
Picker/FileOpenPickerDemo.xamlc++

<Page
    x:Class="Windows10.Picker.FileOpenPickerDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.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="10 0 10 10">

            <TextBlock Name="lblMsg" Margin="5" />

            <Button Name="btnPickFile" Content="pick a file" Click="btnPickFile_Click" Margin="5" />

            <Button Name="btnPickFiles" Content="pick multiple files" Click="btnPickFiles_Click" Margin="5" />

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

Picker/FileOpenPickerDemo.xaml.csweb

/*
 * 演示如何經過 FileOpenPicker 選擇一個文件或多個文件
 * 
 * FileOpenPicker - 文件選擇窗口
 *     ViewMode - 文件選擇窗口的視圖模式,Windows.Storage.Pickers.PickerViewMode 枚舉(List 或 Thumbnail)
 *     SuggestedStartLocation - 文件選擇窗口所顯示的初始路徑,Windows.Storage.Pickers.PickerLocationId 枚舉
 *         DocumentsLibrary, ComputerFolder, Desktop,, Downloads, HomeGroup, MusicLibrary, PicturesLibrary, VideosLibrary, Objects3D, Unspecified
 *     FileTypeFilter - 容許顯示在文件選擇窗口的文件類型集合(* 表明所有)
 *     CommitButtonText - 文件選擇窗口的提交按鈕的顯示文本,此按鈕默認顯示的文本爲「打開」
 *     PickSingleFileAsync() -  彈出文件選擇窗口,以讓用戶選擇一個文件
 *     PickMultipleFilesAsync() - 彈出文件選擇窗口,以讓用戶選擇多個文件
 */

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

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

        private async void btnPickFile_Click(object sender, RoutedEventArgs e)
        {
            // 選擇一個文件
            FileOpenPicker openPicker = new FileOpenPicker();
            openPicker.CommitButtonText = "選中此文件";
            openPicker.ViewMode = PickerViewMode.Thumbnail;
            openPicker.SuggestedStartLocation = PickerLocationId.ComputerFolder;
            openPicker.FileTypeFilter.Add(".jpg");
            openPicker.FileTypeFilter.Add(".gif");
            openPicker.FileTypeFilter.Add(".png");
            // * 表明所有
            // openPicker.FileTypeFilter.Add("*");

            // 彈出文件選擇窗口
            StorageFile file = await openPicker.PickSingleFileAsync(); // 用戶在「文件選擇窗口」中完成操做後,會返回對應的 StorageFile 對象
            if (file != null)
            {
                lblMsg.Text = "選中文件: " + file.Name;
            }
            else
            {
                lblMsg.Text = "取消了";
            }
        }

        private async void btnPickFiles_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(); // 用戶在「文件選擇窗口」中完成操做後,會返回對應的 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 = "取消了";
            }
        }
    }
}


二、演示如何經過 FolderPicker 選擇一個文件夾
Picker/FolderPickerDemo.xamlexpress

<Page
    x:Class="Windows10.Picker.FolderPickerDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.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="10 0 10 10">

            <TextBlock Name="lblMsg" Margin="5" />

            <Button Name="btnPickFolder" Content="pick a folder" Click="btnPickFolder_Click" Margin="5" />

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

Picker/FolderPickerDemo.xaml.cswindows

/*
 * 演示如何經過 FolderPicker 選擇一個文件夾
 * 
 * FolderPicker - 文件夾選擇窗口
 *     ViewMode - 文件夾選擇窗口的視圖模式,Windows.Storage.Pickers.PickerViewMode 枚舉(List 或 Thumbnail)
 *     SuggestedStartLocation - 文件夾選擇窗口所顯示的初始路徑,Windows.Storage.Pickers.PickerLocationId 枚舉
 *         DocumentsLibrary, ComputerFolder, Desktop,, Downloads, HomeGroup, MusicLibrary, PicturesLibrary, VideosLibrary, Objects3D, Unspecified
 *     FileTypeFilter - 容許顯示在文件夾選擇窗口的文件類型集合(只能顯示符合要求的文件,可是沒法選中)
 *     CommitButtonText - 文件夾選擇窗口的提交按鈕的顯示文本,此按鈕默認顯示的文本爲「選擇這個文件夾」
 *     PickSingleFolderAsync() - 彈出文件夾選擇窗口,以讓用戶選擇一個文件夾
 */

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

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

        private async void btnPickFolder_Click(object sender, RoutedEventArgs e)
        {
            // 選擇一個文件夾
            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 = "取消了";
            }
        }
    }
}


三、演示如何經過 FileSavePicker 保存文件到指定路徑
Picker/FileSavePickerDemo.xamlasp.net

<Page
    x:Class="Windows10.Picker.FileSavePickerDemo"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:Windows10.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="10 0 10 10">

            <TextBlock Name="lblMsg" Margin="5" />

            <Button Name="btnSaveFile" Content="save a file" Click="btnSaveFile_Click" Margin="5" />

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

Picker/FileSavePickerDemo.xaml.csasync

/*
 * 演示如何經過 FileSavePicker 保存文件到指定路徑
 * 
 * FileSavePicker - 文件保存窗口
 *     SuggestedStartLocation - 文件保存窗口所顯示的初始路徑,Windows.Storage.Pickers.PickerLocationId 枚舉
 *         DocumentsLibrary, ComputerFolder, Desktop,, Downloads, HomeGroup, MusicLibrary, PicturesLibrary, VideosLibrary, Objects3D, Unspecified
 *     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 Windows10.Picker
{
    public sealed partial class FileSavePickerDemo : Page
    {
        public FileSavePickerDemo()
        {
            this.InitializeComponent();
        }

        private async void btnSaveFile_Click(object sender, RoutedEventArgs e)
        {
            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
[源碼下載]ide

相關文章
相關標籤/搜索