選取器: 自定義文件選取窗口, 自定義文件保存窗口
介紹
重新想象 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>
Picker/MySavePicker.xaml.cs
/*
* 演示如何開發自定義文件保存選取器
*
* 1、在 Package.appxmanifest 中新增一個“文件保存選取器”聲明,並做相關配置
* 2、在 App.xaml.cs 中 override void OnFileSavePickerActivated(FileSavePickerActivatedEventArgs args),如果 app 是由文件保存選取器激活的,則可以在此獲取其相關信息
*
* FileSavePickerActivatedEventArgs - 通過“文件保存選取器”激活應用程序時的事件參數
* FileSavePickerUI - 獲取 FileSavePickerUI 對象
* PreviousExecutionState, Kind, SplashScreen - 各種激活 app 的方式的事件參數基本上都有這些屬性,就不多說了
*
* FileSavePickerUI - 自定義文件保存選取器的幫助類
* AllowedFileTypes - 允許的文件類型,只讀
* Title - 將在“自定義文件保存選取器”上顯示的標題
* FileName - 需要保存的文件名(包括文件名和擴展名),只讀
* TrySetFileName(string value) - 嘗試指定需要保存的文件名(包括文件名和擴展名)
* FileNameChanged - 用戶在文件名文本框中更改文件名或在文件類型下拉框中更改擴展名時觸發的事件
* TargetFileRequested - 用戶提交保存時觸發的事件(事件參數:TargetFileRequestedEventArgs)
*
* TargetFileRequestedEventArgs
* Request - 返回 TargetFileRequest 對象
*
* TargetFileRequest
* TargetFile - 目標文件對象,用於返回給 client
* GetDeferral() - 獲取異步操作對象,同時開始異步操作,之後通過 Complete() 通知完成異步操作
*/
using System;
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 XamlDemo.Picker
{
public sealed partial class MySavePicker : Page
{
private FileSavePickerUI _fileSavePickerUI;
public MySavePicker()
{
this.InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
// 獲取 FileSavePickerUI 對象
var args = (FileSavePickerActivatedEventArgs)e.Parameter;
_fileSavePickerUI = args.FileSavePickerUI;
_fileSavePickerUI.Title = "自定義文件保存選取器";
_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;
});
}
}
}
判斷程序是否是由文件保存選取器激活,在 App.xaml.cs 中 override void OnFileSavePickerActivated(FileSavePickerActivatedEventArgs args)
// 通過文件保存選取器激
活應用程序時所調用的方法
protected override void OnFileSavePickerActivated(FileSavePickerActivatedEventArgs args)
{
var rootFrame = new Frame();
rootFrame.Navigate(typeof(XamlDemo.Picker.MySavePicker), args);
Window.Current.Content = rootFrame;
Window.Current.Activate();
}
OK
[源碼下載]:http://files.cnblogs.com/webabcd/Windows8.rar