Windows 10开发基础

知道91 | ASP.NET | 2015-10-12 | 阅读:4178

在正式版本发布后的一年内,所有符合条件的Windows7、Windows 8.1以及用户都将可以免费升级到Windows 10,Windows Phone 8.1则可以免费升级到Windows 10 Mobile版。所有升级到Windows 10的设备,微软都将提供永久生命周期的支持。

win10 logo

主要内容:

使用选取器打开和保存文件

关于文件、文件夹和库,如果深究其实还是有比较多的内容,我们这一次来学习一下选取器就收了。还有上篇博文中读写文本文件的三种方式可以细细体会一下。

文件选取器包含文件打开选取器(FileOpenPicker、FolderPicker)和文件保存选取器(FileSavePicker),分别用来打开文件和保存文件,这两个选取器的使用方法大致相同。

FileOpenPicker类:

  1. ViewMode ,获取或设置文件选取器用来展示文件或文件夹的视图模式,属性值由PickerViewMode枚举来指定,该枚举有两个枚举值,List表示列表模式,Thumbnail表示缩略图模式
  2. SuggestedStartLocation ,获取或设置文件选取器要呈现给用户的文件的初始位置。属性值由PickerLocationId枚举指定,此枚举一共有10个枚举值,自行转到定义进行查看。
  3. FileTypeFilter,获取文件选择器显示的文件类型的集合,文件类型可以是".txt",".jpg"等,使用Add来进行添加。

注意我们设置的ViewMode和SuggestedStartLocation值在应用程序第一次运行之后,再切换其他值运行,有时候并不会起作用,应该是操作系统自己有记录,然后这个FileTypeFilter属性是必须指定的。

    FileOpenPicker picker = new FileOpenPicker();      //创建文件打开选取器
    picker.ViewMode = PickerViewMode.Thumbnail;       //将ViewMode的值设置为Thumbnail
    picker.SuggestedStartLocation =PickerLocationId.PicturesLibrary;  
    //将文件选取器打开文件的开始位置设置为图片库

    picker.FileTypeFilter.Add(".jpg");
    picker.FileTypeFilter.Add(".jpeg");
    picker.FileTypeFilter.Add(".png");

FolderPicker类:

该类的属性和用法与FileOpenPicker类似。

FolderPicker folderPicker = new FolderPicker();

folderPicker.ViewMode = PickerViewMode.List;

folderPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;

folderPicker.FileTypeFilter.Add("*");

FileSavePicker类:

该类我们经常使用到的3个属性是SuggestedStartLocation(同上)、FileTypeChoices、SuggestedFileName。FileTypeChoices是一个字典类型(IDictionary>),获取用户可选择分配到文件的有效文件类型的集合,SuggestedFileName 则是获取或设置文件保存选取器向用户建议的文件名。

 FileSavePicker picker = new FileSavePicker();

     picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;

     picker.FileTypeChoices.Add("Image", new List() { ".jpg", ".jpeg", ".png", ".bmp", ".gif" });

     picker.SuggestedFileName =DateTime.Now.ToString("yyyyMMddhhmmss");

我们来进行常规演示,首先是选取单个文件,多个文件,还有文件夹。

选取并显示一张图片:

private async void btn_pickpic_Click(object sender, RoutedEventArgs e)
        {
            FileOpenPicker picker = new FileOpenPicker();
            picker.ViewMode = PickerViewMode.List;
            picker.SuggestedStartLocation =PickerLocationId.PicturesLibrary;
            picker.FileTypeFilter.Add(".jpg");
            picker.FileTypeFilter.Add(".jpeg");
            picker.FileTypeFilter.Add(".png");

            storagefile = await picker.PickSingleFileAsync();
            if (storagefile != null)
            {
                this.tb_pickedpic.Text = "Picked photo: " + storagefile.Name;
                WriteableBitmap writeableBitmap = new WriteableBitmap(500, 500);
                IRandomAccessStream stream = await storagefile?.OpenAsync(FileAccessMode.Read);
                await writeableBitmap.SetSourceAsync(stream);
                image.Source = writeableBitmap;
            }
            else
            {
                this.tb_pickedpic.Text = "Operation cancelled.";
                image.Source = null;
            }
        }

private async void btn_pickmultipic_Click(object sender, RoutedEventArgs e)
        {
            var picker = new FileOpenPicker();
            picker.ViewMode = PickerViewMode.Thumbnail;
            picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            picker.FileTypeFilter.Add(".jpeg");
            picker.FileTypeFilter.Add(".png");
            picker.FileTypeFilter.Add(".jpg");

            IReadOnlyList filelist =await  picker.PickMultipleFilesAsync();
            StringBuilder output = new StringBuilder("Picked files:");
            if (filelist.Count>0)
            {
                foreach (var file in filelist)
                {
                    output.Append(file.Name+"\n");
                }
            }
            else
            {
                output.Append("none!");
            }
            Showmsg(output.ToString());
        }

Showmsg(output.ToString());是弹出对话框,显示所选择的图片。

选取文件夹:

private async void btn_pickfolder_Click(object sender, RoutedEventArgs e)
        {
            FolderPicker folderPicker = new FolderPicker();
            folderPicker.ViewMode = PickerViewMode.List;
            folderPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            folderPicker.FileTypeFilter.Add("*");
            StorageFolder folder = await folderPicker.PickSingleFolderAsync();
            if (folder != null)
            {
               StorageApplicationPermissions.FutureAccessList.AddOrReplace("PickedFolderToken", folder);
               Showmsg("Picked folder: " + folder.Name);
            }
            else
            {
               Showmsg("Operation cancelled.");
            }
        }

接着是保存文件,首先是保存文本文件,然后将上面选取并显示的那张图片保存到另一文件夹。

保存文本文件:

private async void button_Click(object sender, RoutedEventArgs e)
        {
            FileSavePicker savePicker = new FileSavePicker();
            savePicker.SuggestedStartLocation = PickerLocationId.DocumentsLibrary;
            savePicker.FileTypeChoices.Add("文本文件", new List() { ".txt" });
            savePicker.SuggestedFileName = "mytxt";
          
            StorageFile file = await savePicker.PickSaveFileAsync();
            if (file != null)
            {
                CachedFileManager.DeferUpdates(file);
                if (tbxcontent.Text.Trim().Length > 0)
                {
                    await FileIO.WriteTextAsync(file,tbxcontent.Text);
                    FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
                    if (status == FileUpdateStatus.Complete)
                    {
                        this.tbInfo.Text = "File " + file.Name + " was saved.";
                    }
                    else
                    {
                        this.tbInfo.Text = "File " + file.Name + " couldn't be saved.";
                    }
                }
            }
            else
            {
                this.tbInfo.Text = "Operation cancelled.";
            }
        }

运行结果:

win 10

private async void btn_save_Click(object sender, RoutedEventArgs e)
        {
            FileSavePicker picker = new FileSavePicker();
            picker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
            picker.FileTypeChoices.Add("Image", new List() { ".jpg", ".jpeg", ".png", ".bmp", ".gif" });
            picker.SuggestedFileName =DateTime.Now.ToString("yyyyMMddhhmmss");

           StorageFile file= await picker.PickSaveFileAsync();
            if(file!=null&&storagefile!=null)
            {
                CachedFileManager.DeferUpdates(file);

                IBuffer buffer = await FileIO.ReadBufferAsync(storagefile);           //storageFile是已经选中要保存的文件

                await FileIO.WriteBufferAsync(file, buffer);

                FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
            }
        }

运行结果:

win0

现在微软的前景也很不错,微软官方表示,Windows 10将首次以免费升级的模式提供给符合资质的正版Windows 7和8/8.1设备的用户,并存在一定的硬软件和时限要求。此外,Windows 10还对Cortana、Microsoft Edge和Xbox等进行整合。