[源碼下載]
html
做者:webabcd
介紹
背水一戰 Windows 10 之 選取器html5
示例
一、演示如何開發自定義文件保存選取器
App.xaml.csc++
// 經過文件保存選取器激活應用程序時所調用的方法 protected override void OnFileSavePickerActivated(FileSavePickerActivatedEventArgs args) { var rootFrame = new Frame(); rootFrame.Navigate(typeof(Windows10.Picker.MySavePicker), args); Window.Current.Content = rootFrame; Window.Current.Activate(); }
Picker/MySavePicker.xamlweb
<Page x:Class="Windows10.Picker.MySavePicker" 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" /> </StackPanel> </Grid> </Page>
Picker/MySavePicker.xaml.csexpress
/* * 演示如何開發自定義文件保存選取器 * * 一、在 Package.appxmanifest 中新增一個「文件保存選取器」聲明,並作相關配置 * 二、在 App.xaml.cs 中 override void OnFileSavePickerActivated(FileSavePickerActivatedEventArgs args),若是 app 是由文件保存選取器激活的,則會調用此方法 * * FileSavePickerActivatedEventArgs - 經過「文件保存選取器」激活應用程序時的事件參數 * FileSavePickerUI - 獲取 FileSavePickerUI 對象 * Kind - 此 app 被激活的類型(ActivationKind 枚舉) * 好比,若是是經過「文件打開選取器」激活的話,則此值爲 FileOpenPicker * PreviousExecutionState - 此 app 被激活前的狀態(ApplicationExecutionState 枚舉) * 好比,若是此 app 被激活前就是運行狀態的或,則此值爲 Running * SplashScreen - 獲取此 app 的 SplashScreen 對象 * CallerPackageFamilyName - 獲取激活了此 app 的應用的包名(可是實際測試發現,獲取到的倒是此 app 的包名) * User - 獲取激活了此 app 的 User 對象 * * FileSavePickerUI - 自定義文件保存選取器的幫助類 * AllowedFileTypes - 容許的文件類型,只讀 * Title - 將在「自定義文件保存選取器」上顯示的標題 * FileName - 須要保存的文件名(包括文件名和擴展名),只讀 * TrySetFileName(string value) - 嘗試指定須要保存的文件名(包括文件名和擴展名) * FileNameChanged - 用戶在文件名文本框中更改文件名或在文件類型下拉框中更改擴展名時觸發的事件 * TargetFileRequested - 用戶提交保存時觸發的事件(事件參數:TargetFileRequestedEventArgs) * * TargetFileRequestedEventArgs * Request - 返回 TargetFileRequest 對象 * * TargetFileRequest * TargetFile - 目標文件對象,用於返回給 client * GetDeferral() - 獲取異步操做對象,同時開始異步操做,以後經過 Complete() 通知完成異步操做 */ using System; using System.Collections.Generic; using Windows.ApplicationModel.Activation; using Windows.Storage; using Windows.Storage.Pickers.Provider; using Windows.UI.Core; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; namespace Windows10.Picker { public sealed partial class MySavePicker : Page { private FileSavePickerUI _fileSavePickerUI; public MySavePicker() { this.InitializeComponent(); } protected override void OnNavigatedTo(NavigationEventArgs e) { // 獲取 FileSavePickerUI 對象(從 App.xaml.cs 傳來的) FileSavePickerActivatedEventArgs args = (FileSavePickerActivatedEventArgs)e.Parameter; _fileSavePickerUI = args.FileSavePickerUI; _fileSavePickerUI.Title = "自定義文件保存選取器"; // 經過 AllowedFileTypes 獲取到的容許的擴展名是在調用端的 FileSavePicker.FileTypeChoices 中配置的,實際保存擴展名能夠不與其匹配 IReadOnlyList<string> allowedFileTypes = _fileSavePickerUI.AllowedFileTypes; lblMsg.Text = "allowedFileTypes: " + string.Join(",", allowedFileTypes); lblMsg.Text += Environment.NewLine; lblMsg.Text += "Kind: " + args.Kind; lblMsg.Text += Environment.NewLine; lblMsg.Text += "SplashScreen.ImageLocation: " + args.SplashScreen.ImageLocation; lblMsg.Text += Environment.NewLine; lblMsg.Text += "PreviousExecutionState: " + args.PreviousExecutionState; lblMsg.Text += Environment.NewLine; lblMsg.Text += "CallerPackageFamilyName: " + args.CallerPackageFamilyName; lblMsg.Text += Environment.NewLine; lblMsg.Text += "User.NonRoamableId: " + args.User.NonRoamableId; lblMsg.Text += Environment.NewLine; _fileSavePickerUI.TargetFileRequested += _fileSavePickerUI_TargetFileRequested; } protected override void OnNavigatedFrom(NavigationEventArgs e) { _fileSavePickerUI.TargetFileRequested -= _fileSavePickerUI_TargetFileRequested; } private async void _fileSavePickerUI_TargetFileRequested(FileSavePickerUI sender, TargetFileRequestedEventArgs args) { // 異步操做 var deferral = args.Request.GetDeferral(); try { // 在指定的地址新建一個沒有任何內容的空白文件 StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(sender.FileName, CreationCollisionOption.GenerateUniqueName); // 設置 TargetFile,「自定義文件保存選取器」的調用端會收到此對象 args.Request.TargetFile = file; } catch (Exception ex) { // 輸出異常信息 OutputMessage(ex.ToString()); } finally { // 完成異步操做 deferral.Complete(); } } private async void OutputMessage(string msg) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { lblMsg.Text = msg; }); } } }
二、演示如何調用自定義文件保存選取器
Picker/MySavePickerDemo.xamlwindows
<Page x:Class="Windows10.Picker.MySavePickerDemo" 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"> <Run> 若是須要激活自定義的文件保存窗口,請在彈出的選取器窗口的左側的導航列表中選擇相應的 app </Run> </TextBlock> <Button Name="btnMySavePicker" Content="彈出文件保存窗口" Click="btnMySavePicker_Click" Margin="5" /> </StackPanel> </Grid> </Page>
Picker/MySavePickerDemo.xaml.csapp
/* * 演示如何調用自定義文件保存選取器 * * 自定義文件保存選取器參見 MySavePicker.xaml */ using System; using System.Collections.Generic; using Windows.Storage; using Windows.Storage.Pickers; using Windows.Storage.Provider; using Windows.UI.Xaml.Controls; namespace Windows10.Picker { public sealed partial class MySavePickerDemo : Page { public MySavePickerDemo() { this.InitializeComponent(); } private async void btnMySavePicker_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e) { FileSavePicker savePicker = new FileSavePicker(); savePicker.FileTypeChoices.Add("文本", new List<string>() { ".txt" }); // 彈出文件保存窗口 StorageFile file = await savePicker.PickSaveFileAsync(); 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
[源碼下載]asp.net