work hard work smart

专注于Java后端开发。 不断总结,举一反三。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Metro Style App开发快速入门 之文件选择总结

Posted on 2012-03-27 13:16  work hard work smart  阅读(522)  评论(0编辑  收藏  举报

1、选择单个文件,FileTypeFilter.Add是添加能访问的文件类型。

        private async void filePickerBtn_Click(object sender, RoutedEventArgs e)
        {
            FileOpenPicker fileOpenPicker = new FileOpenPicker();
            fileOpenPicker.FileTypeFilter.Add(".jpg");
            fileOpenPicker.FileTypeFilter.Add(".jpeg");
            fileOpenPicker.FileTypeFilter.Add(".png");

            fileOpenPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            StorageFile file = await fileOpenPicker.PickSingleFileAsync();
            if (file != null)
            {
                displayTextBlock.Text = file.Name;
            }
        }

 2、选择多个文件,并且是任意文件类型

        private async void filePickerBtn_Click(object sender, RoutedEventArgs e)
        {
            FileOpenPicker fileOpenPicker = new FileOpenPicker();
            fileOpenPicker.FileTypeFilter.Add("*");
            fileOpenPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            IReadOnlyList<StorageFile> files = await fileOpenPicker.PickMultipleFilesAsync();
            if (files.Count > 0)
            {
                foreach (StorageFile file in files)
                {
                    displayTextBlock.Text += file.Name + "\n";
                }
            }
        }

 

 3、 选择文件夹

        private async void folderPickerBtn_Click(object sender, RoutedEventArgs e)
        {
            if (EnsureUnsnapped())
            {
                FolderPicker folderPicker = new FolderPicker();
                folderPicker.SuggestedStartLocation = PickerLocationId.Desktop;
                folderPicker.FileTypeFilter.Add("*");
                StorageFolder storageFolder = await folderPicker.PickSingleFolderAsync();
                if (null != storageFolder)
                {
                    displayTextBlock.Text = storageFolder.Name;
                }
            }
        }

 FilePicker APIs是不能在snapped状态下使用

        bool EnsureUnsnapped()
        { 
            //FilePicker APIs不能再snapped状态下使用。
            return ((ApplicationView.Value != ApplicationViewState.Snapped) || ApplicationView.TryUnsnap());
        }

 

4、保存文件

        private async void SaveFileBtn_Click(object sender, RoutedEventArgs e)
        {
            if (EnsureUnsnapped())
            {
                FileSavePicker savePicker = new FileSavePicker();
                savePicker.SuggestedStartLocation = PickerLocationId.Desktop;
                savePicker.FileTypeChoices.Add("Text", new List<string>() { ".txt" });
                savePicker.FileTypeChoices.Add("Word Docment", new List<string>() { ".docx", ".doc" });
                savePicker.DefaultFileExtension = ".txt";
                savePicker.SuggestedFileName = "New Document";
                StorageFile storageFile = await savePicker.PickSaveFileAsync();
                if (null != storageFile)
                {
                    displayTextBlock.Text = storageFile.Name;
                }
            }
        }

 选择保存的文件保存自己要求的文件。自定义文件类型和文件名。