wp8.1 Study14 FilePicker简单介绍

一、FileOpenPicker/FileSavePicker介绍

  这个在使用手机中是十分经常的,如在朋友圈中你要发图片,首先点击添加图片,而后你会进入手机图片区,选择图片后自动返回朋友圈准备发图界面。简单的步骤演示如下图所示:

  它允许App获取手机中任何类型的文件,可以不通过KnownFoldersAPI从Pictures库及Vedio库获得相应文件。

二、简单例子

  在页面中,放入一个Image控件及一个叫Pick的Button控件,假设想点击pick,然后进入选择图片库,选择一个图片,然后Image控件显示相应选择的图片。

pick点击事件C#代码如下:

private void pick_Click(object sender, RoutedEventArgs e)
        {
            //创造一个picker对象
            var file = new FileOpenPicker();
            //picker打开文件类型
            file.FileTypeFilter.Add(".jpg");
            file.FileTypeFilter.Add(".jpeg");
            file.FileTypeFilter.Add(".png");
            //打开picker获取一个文件
            file.ContinuationData["Operation"] = "UpdateProfilePicture";//这个为了后面保存图片的操作
            file.PickSingleFileAndContinue();
        }

Pick 一个文件后,还需要在App.xaml.cs重写Activate方法,代码如下:

protected override void OnActivated(IActivatedEventArgs args)
{
    if (args is FileOpenPickerContinuationEventArgs)
    {
        Frame rootFrame = Window.Current.Content as Frame;
        if (rootFrame == null)
        {
            // 创建要充当导航上下文的框架,并导航到第一页
            rootFrame = new Frame();

            // TODO: 将此值更改为适合您的应用程序的缓存大小
            rootFrame.CacheSize = 1;
            Window.Current.Content = rootFrame;
        }

         // Standard Frame initialization code ...

        if (!rootFrame.Navigate(typeof(Page1)))
        {
            throw new Exception("Failed to create target page");
        }

        var p = rootFrame.Content as Page1;
        p.FilePickerEvent = (FileOpenPickerContinuationEventArgs)args;

        // Ensure the current window is active
        Window.Current.Activate();
    }
}

最后还需要在页面中加入一下代码

private FileOpenPickerContinuationEventArgs _filePickerEventArgs = null;
        public FileOpenPickerContinuationEventArgs FilePickerEvent
        {
            get { return _filePickerEventArgs; }
            set
            {
                _filePickerEventArgs = value;
                ContinueFileOpenPicker(_filePickerEventArgs);
            }
        }

        private async void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs _filePickerEventArgs)
        {
            if ((_filePickerEventArgs.ContinuationData["Operation"] as string) == "UpdateProfilePicture" && _filePickerEventArgs.Files != null && _filePickerEventArgs.Files.Count > 0)
            {
                StorageFile file = _filePickerEventArgs.Files[0];
                string mrutoken = StorageApplicationPermissions.MostRecentlyUsedList.Add(file, "20141201");//mrucache保存选择图片记录
                IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.SetSource(fileStream);
                imageshow.Source = bitmapImage;
                
            }

注:还可以使用SavePicker ,使用如上,代码如下:

//Create the picker object
FileSavePicker savePicker = new FileSavePicker();

// Dropdown of file types the user can save the file as   
savePicker.FileTypeChoices.Add(
    "Plain Text"new List<string>() { ".txt" });
// Default file name if the user does not type one in or select // a file to replace
savePicker.SuggestedFileName = "New Document";

// Open the picker for the user to pick a file
savePicker.ContinuationData["Operation"] = "SomeDataOrOther";
savePicker.PickSingleFileAndContinue();

三、Windows.Storage.AccessCache

  这是让使用者能快速重新打开之前打开过的文件的缓存,我们可以使用FutureAccessList 和 MostRecentlyUsedList,其中MostRecentlyUsedList是一个记录着经常使用的文件的列表,而FutureAccessList是一个可以保存文件以便以后的使用的列表。当使用者pick一个文件等,App需要把其加入以上两个列表记录。

  在 ContinueFileOpenPicker()方法添加

// Add to MRU with metadata (For example, a string that represents the date)
        string mruToken = StorageApplicationPermissions.MostRecentlyUsedList.Add(file, "20120716");
        // Add to FA without metadata
        string faToken = StorageApplicationPermissions.FutureAccessList.Add(file);

而每当需要使用列表时,可以这样调用代码:

//  get the token for the first item in our MRU and use it to retrieve a StorageFile that represents that file
String mruFirstToken = StorageApplicationPermissions.MostRecentlyUsedList.Entries.First().Token;
StorageFile retrievedFile = await StorageApplicationPermissions.MostRecentlyUsedList.GetFileAsync(mruFirstToken);
...

// Retrieve tokens for all items in the MRU
AccessListEntryView mruEntries = StorageApplicationPermissions.MostRecentlyUsedList.Entries;
if (mruEntries.Count > 0)
{
    foreach (AccessListEntry entry in mruEntries)
    {
        String mruToken = entry.Token;
        // Continue processing the MRU entry
        ...
    }
}
原文地址:https://www.cnblogs.com/NEIL-X/p/4191908.html