Windows 8 Store Apps學習(25) 選取器: 檔案選取視窗等

來源:互聯網
上載者:User

選取器: 檔案選取視窗, 檔案夾選取視窗, 檔案儲存視窗

介紹

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.