Windows 8 Store Apps學習(26) 選取器: 自訂檔案選取視窗和儲存視窗

來源:互聯網
上載者:User

選取器: 自訂檔案選取視窗, 自訂檔案儲存視窗

介紹

重新想象 Windows 8 Store Apps 之 選取器

FileOpenPickerUI - 自訂檔案開啟選取器

FileSavePickerUI - 自訂儲存選擇器

樣本

1、 開發一個自訂檔案選取視窗。註:如果需要 啟用自訂的檔案選取視窗,請在彈出的選取器視窗的左上方選擇對應 Provider

Picker/MyOpenPicker.xaml

<Page    x:Class="XamlDemo.Picker.MyOpenPicker"    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="btnPickLocalFile" Content="選擇一個本地檔案" Click="btnPickLocalFile_Click_1" Margin="0 10 0 0" />                <Button Name="btnPickRemoteFile" Content="選擇一個遠程檔案" Click="btnPickRemoteFile_Click_1" Margin="0 10 0 0" />            </StackPanel>    </Grid></Page>

Picker/MyOpenPicker.xaml.cs

/* * 示範如何開發自訂檔案開啟選取器 *  * 1、在 Package.appxmanifest 中新增一個“檔案開啟選取器”聲明,並做相關配置 * 2、在 App.xaml.cs 中 override void OnFileOpenPickerActivated(FileOpenPickerActivatedEventArgs args),如果 app 是由檔案開啟選取器啟用的,則可以在此擷取其相關資訊 *  * FileOpenPickerActivatedEventArgs - 通過“檔案開啟選取器”啟用應用程式時的事件參數 *     FileOpenPickerUI - 擷取 FileOpenPickerUI 對象 *     PreviousExecutionState, Kind, SplashScreen - 各種啟用 app 的方式的事件參數基本上都有這些屬性,就不多說了 *      * FileOpenPickerUI - 自訂檔案開啟選取器的協助類 *     AllowedFileTypes - 允許的檔案類型,唯讀 *     SelectionMode - 選擇模式(FileSelectionMode.Single 或 FileSelectionMode.Multiple) *     Title - 將在“自訂檔案開啟選取器”上顯示的標題 *     CanAddFile(IStorageFile file) - 是否可以將指定的檔案添加進選中檔案清單 *     AddFile(string id, IStorageFile file) - 將檔案添加進選中檔案清單,並指定 id *     ContainsFile(string id) - 選中檔案清單中是否包含指定的 id *     RemoveFile(string id) - 根據 id 從選中檔案清單中刪除對應的檔案 *     FileRemoved - 從選中檔案清單中刪除檔案時觸發的事件 *     Closing - 使用者關閉“自訂檔案開啟選取器”時觸發的事件 */    using System;using Windows.ApplicationModel;using Windows.ApplicationModel.Activation;using Windows.Storage;using Windows.Storage.Pickers.Provider;using Windows.Storage.Provider;using Windows.Storage.Streams;using Windows.UI.Xaml.Controls;using Windows.UI.Xaml.Navigation;    namespace XamlDemo.Picker{    public sealed partial class MyOpenPicker : Page    {        private FileOpenPickerUI _fileOpenPickerUI;            public MyOpenPicker()        {            this.InitializeComponent();        }            protected override void OnNavigatedTo(NavigationEventArgs e)        {            // 擷取 FileOpenPickerUI 對象            var args = (FileOpenPickerActivatedEventArgs)e.Parameter;            _fileOpenPickerUI = args.FileOpenPickerUI;                _fileOpenPickerUI.Title = "自訂檔案開啟選取器";                // _fileOpenPickerUI.Closing += _fileOpenPickerUI_Closing;            // _fileOpenPickerUI.FileRemoved += _fileOpenPickerUI_FileRemoved;        }            protected override void OnNavigatedFrom(NavigationEventArgs e)        {            // _fileOpenPickerUI.Closing -= _fileOpenPickerUI_Closing;            // _fileOpenPickerUI.FileRemoved -= _fileOpenPickerUI_FileRemoved;        }            // 選擇一個本地檔案        private async void btnPickLocalFile_Click_1(object sender, Windows.UI.Xaml.RoutedEventArgs e)        {            /*             * 注意:             * 1、選擇的檔案的副檔名必須匹配 FileOpenPicker.FileTypeFilter 中的定義             * 2、如果通過 KnownFolders.DocumentsLibrary 等選擇檔案,除了要在 Package.appxmanifest 選擇對應的“庫”功能外,還必須在 Package.appxmanifest 中的“檔案類型關聯”聲明中增加對相應的的副檔名的支援             */                StorageFile file = await Package.Current.InstalledLocation.GetFileAsync(@"Assets\Logo.png");            AddFileResult result = _fileOpenPickerUI.AddFile("myFile", file);                lblMsg.Text = "選擇的檔案: " + file.Name;            lblMsg.Text += Environment.NewLine;            lblMsg.Text += "AddFileResult: " + result.ToString();        }            // 選擇一個遠程檔案        private async void btnPickRemoteFile_Click_1(object sender, Windows.UI.Xaml.RoutedEventArgs e)        {            Uri uri = new Uri("http://images.cnblogs.com/mvpteam.gif", UriKind.Absolute);                // 副檔名必須匹配 FileOpenPicker.FileTypeFilter 中的定義            StorageFile file = await StorageFile.CreateStreamedFileFromUriAsync("mvp.gif", uri, RandomAccessStreamReference.CreateFromUri(uri));            AddFileResult result = _fileOpenPickerUI.AddFile("myFile", file);                lblMsg.Text = "選擇的檔案: " + file.Name;            lblMsg.Text += Environment.NewLine;            lblMsg.Text += "AddFileResult: " + result.ToString();        }    }}

判斷程式是否是由檔案開啟選取器啟用,在 App.xaml.cs 中 override void OnFileOpenPickerActivated(FileOpenPickerActivatedEventArgs args)

// 通過檔案開啟選取器啟用應用程式時所調用的方法protected override void OnFileOpenPickerActivated(FileOpenPickerActivatedEventArgs args){    var rootFrame = new Frame();    rootFrame.Navigate(typeof(XamlDemo.Picker.MyOpenPicker), args);    Window.Current.Content = rootFrame;        Window.Current.Activate();}

2、 開發一個自訂檔案儲存視窗。註:如果需要啟用自訂的檔案儲存視窗,請在彈出的選取器 視窗的左上方選擇對應 Provider
Picker/MySavePicker.xaml

<Page    x:Class="XamlDemo.Picker.MySavePicker"    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" />            </StackPanel>    </Grid></Page>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.