zoukankan      html  css  js  c++  java
  • windows phone 8.1开发:文件选择器FileOpenPicker

    原文出自:http://www.bcmeng.com/fileopenpicker/

    今天小梦给大家分享一下 windows phone 8.1中的文件选择器,和之前的windows phone8的不同,在windows phone8中如果要选择照片,可以直接用照片选择器,但是这在windows phone8.1中没有了,windows phone8.1 增加了文件选择器.相比之下,文件选择器不仅可以选择照片,还可以选择各类文件,手机和SD卡都可以,还可以查找网路中其他设备所共享的文件,还可以在Onedrive上浏览并选择需要的文件.

    文件选择器分为:FileOpenPicker和FileSavePicker俩类.前者用来打开已经存在的任何文件,后者可以创建新文件或者覆盖同名文件.

    我们先来看FileOpenPicker.常用属性:

    • FileTypeFilter:文件类型过滤器,是一个string类型的列表,每一个元素为一个有效的文件扩展名.
    • CommitButtonText:提交按钮上显示的文本.
    • ContinuationData: 利用ContinuationData 来记录一些信息,以保证应用恢复时能获取应用挂起的信息.

    注意:SuggestedStartLocation和ViewMode属性在windows phone 8.1中是无效的!仅在windows 8.1中有效.

    俩个方法:

    • PickSingleFileAndContinue:选取单个文件并继续
    • PickMultipleFilesAndContinue 选取多个文件并继续

    下面我们来看一个具体示例,利用文件选择器选择一张照片:

    首先实例化FileOpenPicker对象,设置文件类型,设置 ContinuationData

    FileOpenPicker openPicker = new FileOpenPicker();
    
                openPicker.FileTypeFilter.Add(".jpg");
    
                openPicker.ContinuationData["Operation"] = "Image";
                openPicker.PickSingleFileAndContinue();
    

      然后处理OnActivated事件:

           protected override void OnActivated(IActivatedEventArgs args)
         {
    
                if (args is FileOpenPickerContinuationEventArgs)
            {
            Frame rootFrame = Window.Current.Content as Frame;
              if (!rootFrame.Navigate(typeof(MainPage)))
            {
                throw new Exception("Failed to create target page");
            }
    
            var p = rootFrame.Content as MainPage;
            p.FilePickerEvent = (FileOpenPickerContinuationEventArgs)args;
         }
    
        Window.Current.Activate();
    }
    

      最后在使用文件选择器的页面处理返回的数据:

     private FileOpenPickerContinuationEventArgs _filePickerEventArgs = null;
            public FileOpenPickerContinuationEventArgs FilePickerEvent
            {
                get { return _filePickerEventArgs; }
                set
                {
                    _filePickerEventArgs = value;
                    ContinueFileOpenPicker(_filePickerEventArgs);
                }
            }
    
            public async void ContinueFileOpenPicker(FileOpenPickerContinuationEventArgs args)
            {
                if ((args.ContinuationData["Operation"] as string) == "Image" && args.Files != null && args.Files.Count > 0)
                {
                    StorageFile file = args.Files[0];
                    IRandomAccessStream fileStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
                    BitmapImage bitmapImage = new BitmapImage();
                    bitmapImage.SetSource(fileStream);
                    image.Source = bitmapImage;
                }
            }

    使用FileOpenPicker的方法就是这样,获取其他文件时,不同的地方是文件类型的设置以及返回数据的处理.

  • 相关阅读:
    03-HTML之body标签
    02-HTML之head标签
    01-HTML介绍
    第十五章 并发编程
    第十四章 网络编程
    第十三章 模块和包
    第十二章 异常和错误
    第十一章 面向对象进阶
    第九章 常用模块(续)
    003 配置负载均衡
  • 原文地址:https://www.cnblogs.com/xdoudou/p/3936951.html
Copyright © 2011-2022 走看看