[源码下载]

背水一战 Windows 10 (94) - 选取器: 自定义文件打开选取器

作者:webabcd

介绍
背水一战 Windows 10 之 选取器

  • 自定义文件打开选取器

示例
1、演示如何开发自定义文件打开选取器
App.xaml.cs

        // 通过文件打开选取器激活应用程序时所调用的方法
protected override void OnFileOpenPickerActivated(FileOpenPickerActivatedEventArgs args)
{
var rootFrame = new Frame();
rootFrame.Navigate(typeof(Windows10.Picker.MyOpenPicker), args);
Window.Current.Content = rootFrame; Window.Current.Activate();
}

Picker/MyOpenPicker.xaml

<Page
x:Class="Windows10.Picker.MyOpenPicker"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.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="10 0 10 10"> <TextBlock Name="lblMsg" Margin="5" /> <Button Name="btnPickLocalFile" Content="选择一个本地文件" Click="btnPickLocalFile_Click" Margin="5" /> <Button Name="btnPickRemoteFile" Content="选择一个远程文件" Click="btnPickRemoteFile_Click" Margin="5" /> </StackPanel>
</Grid>
</Page>

Picker/MyOpenPicker.xaml.cs

/*
* 演示如何开发自定义文件打开选取器
*
* 1、在 Package.appxmanifest 中新增一个“文件打开选取器”声明,并做相关配置
* 2、在 App.xaml.cs 中 override void OnFileOpenPickerActivated(FileOpenPickerActivatedEventArgs args),如果 app 是由文件打开选取器激活的,则会调用此方法
*
* FileOpenPickerActivatedEventArgs - 通过“文件打开选取器”激活应用程序时的事件参数
* FileOpenPickerUI - 获取 FileOpenPickerUI 对象
* Kind - 此 app 被激活的类型(ActivationKind 枚举)
* 比如,如果是通过“文件打开选取器”激活的话,则此值为 FileOpenPicker
* PreviousExecutionState - 此 app 被激活前的状态(ApplicationExecutionState 枚举)
* 比如,如果此 app 被激活前就是运行状态的或,则此值为 Running
* SplashScreen - 获取此 app 的 SplashScreen 对象
* CallerPackageFamilyName - 获取激活了此 app 的应用的包名(但是实际测试发现,获取到的却是此 app 的包名)
* User - 获取激活了此 app 的 User 对象
*
* 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 从选中文件列表中删除对应的文件
* Closing - 用户关闭“自定义文件打开选取器”时触发的事件
*
*
* 注意:测试时发现如果此 app 作为文件打开选取器激活之前是运行状态的话,则在作为文件打开选取器时会出现控件事件无法触发的情况(但是有的时候是正常的),不知道为什么,这一点开发和测试时要注意
*/ using System;
using System.Collections.Generic;
using Windows.ApplicationModel;
using Windows.ApplicationModel.Activation;
using Windows.Storage;
using Windows.Storage.Pickers.Provider;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Navigation; namespace Windows10.Picker
{
public sealed partial class MyOpenPicker : Page
{
private FileOpenPickerUI _fileOpenPickerUI; public MyOpenPicker()
{
this.InitializeComponent();
} protected override void OnNavigatedTo(NavigationEventArgs e)
{
// 获取 FileOpenPickerUI 对象(从 App.xaml.cs 传来的)
FileOpenPickerActivatedEventArgs args = (FileOpenPickerActivatedEventArgs)e.Parameter;
_fileOpenPickerUI = args.FileOpenPickerUI; _fileOpenPickerUI.Title = "自定义文件打开选取器"; // 注意:选择的文件的扩展名必须匹配 AllowedFileTypes 中的定义(其是在调用端的 FileOpenPicker.FileTypeFilter 中配置的)
IReadOnlyList<string> allowedFileTypes = _fileOpenPickerUI.AllowedFileTypes;
lblMsg.Text = "allowedFileTypes: " + string.Join(",", allowedFileTypes);
lblMsg.Text += Environment.NewLine; lblMsg.Text += "Kind: " + args.Kind;
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "SplashScreen.ImageLocation: " + args.SplashScreen.ImageLocation;
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "PreviousExecutionState: " + args.PreviousExecutionState;
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "CallerPackageFamilyName: " + args.CallerPackageFamilyName;
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "User.NonRoamableId: " + args.User.NonRoamableId;
lblMsg.Text += Environment.NewLine; // _fileOpenPickerUI.Closing += _fileOpenPickerUI_Closing; base.OnNavigatedTo(e);
} protected override void OnNavigatedFrom(NavigationEventArgs e)
{
// _fileOpenPickerUI.Closing -= _fileOpenPickerUI_Closing; base.OnNavigatedFrom(e);
} // 选择一个本地文件
private async void btnPickLocalFile_Click(object sender, RoutedEventArgs e)
{
StorageFile file = await Package.Current.InstalledLocation.GetFileAsync(@"Assets\hololens.jpg");
if (_fileOpenPickerUI.CanAddFile(file))
{
AddFileResult result = _fileOpenPickerUI.AddFile("myFile", file); lblMsg.Text = "选择的文件: " + file.Name;
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "AddFileResult: " + result.ToString();
}
} // 选择一个远程文件
private async void btnPickRemoteFile_Click(object sender, 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));
if (_fileOpenPickerUI.CanAddFile(file))
{
AddFileResult result = _fileOpenPickerUI.AddFile("myFile", file); lblMsg.Text = "选择的文件: " + file.Name;
lblMsg.Text += Environment.NewLine;
lblMsg.Text += "AddFileResult: " + result.ToString();
}
}
}
}

2、演示如何调用自定义文件打开选取器
Picker/MyOpenPickerDemo.xaml

<Page
x:Class="Windows10.Picker.MyOpenPickerDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.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="10 0 10 10"> <TextBlock Name="lblMsg" TextWrapping="Wrap" Margin="5">
<Run>
如果需要激活自定义的文件选取窗口,请在弹出的选取器窗口的左侧的导航列表中选择相应的 app
</Run>
<LineBreak />
<Run>
测试时发现如果此 app 作为文件打开选取器激活之前是运行状态的话,则在作为文件打开选取器时会出现控件事件无法触发的情况(但是有的时候是正常的),不知道为什么,这一点开发和测试时要注意
</Run>
</TextBlock> <Button Name="btnMyOpenPicker" Content="弹出文件选择窗口" Click="btnMyOpenPicker_Click" Margin="5" /> </StackPanel>
</Grid>
</Page>

Picker/MyOpenPickerDemo.xaml.cs

/*
* 演示如何调用自定义文件打开选取器
*
* 自定义文件打开选取器参见 MyOpenPicker.xaml
*/ using System;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; namespace Windows10.Picker
{
public sealed partial class MyOpenPickerDemo : Page
{
public MyOpenPickerDemo()
{
this.InitializeComponent();
} private async void btnMyOpenPicker_Click(object sender, RoutedEventArgs e)
{
// 选择一个文件
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.CommitButtonText = "选中此文件";
openPicker.FileTypeFilter.Add("*"); // 弹出文件选择窗口
StorageFile file = await openPicker.PickSingleFileAsync();
if (file != null)
{
lblMsg.Text = "选中文件: " + file.Name;
}
else
{
lblMsg.Text = "取消了";
}
}
}
}

OK
[源码下载]

背水一战 Windows 10 (94) - 选取器: 自定义文件打开选取器的更多相关文章

  1. 背水一战 Windows 10 (95) - 选取器: 自定义文件保存选取器

    [源码下载] 背水一战 Windows 10 (95) - 选取器: 自定义文件保存选取器 作者:webabcd 介绍背水一战 Windows 10 之 选取器 自定义文件保存选取器 示例1.演示如何 ...

  2. 背水一战 Windows 10 (98) - 关联启动: 使用外部程序打开一个文件, 使用外部程序打开一个 Uri

    [源码下载] 背水一战 Windows 10 (98) - 关联启动: 使用外部程序打开一个文件, 使用外部程序打开一个 Uri 作者:webabcd 介绍背水一战 Windows 10 之 关联启动 ...

  3. 背水一战 Windows 10 (97) - 选取器: CachedFileUpdater

    [源码下载] 背水一战 Windows 10 (97) - 选取器: CachedFileUpdater 作者:webabcd 介绍背水一战 Windows 10 之 选取器 CachedFileUp ...

  4. 重新想象 Windows 8 Store Apps (26) - 选取器: 自定义文件选取窗口, 自定义文件保存窗口

    原文:重新想象 Windows 8 Store Apps (26) - 选取器: 自定义文件选取窗口, 自定义文件保存窗口 [源码下载] 重新想象 Windows 8 Store Apps (26) ...

  5. 背水一战 Windows 10 (93) - 选取器: FileOpenPicker, FolderPicker, FileSavePicker

    [源码下载] 背水一战 Windows 10 (93) - 选取器: FileOpenPicker, FolderPicker, FileSavePicker 作者:webabcd 介绍背水一战 Wi ...

  6. 背水一战 Windows 10 (96) - 选取器: ContactPicker

    [源码下载] 背水一战 Windows 10 (96) - 选取器: ContactPicker 作者:webabcd 介绍背水一战 Windows 10 之 选取器 ContactPicker(联系 ...

  7. 背水一战 Windows 10 (11) - 资源: CustomResource, ResourceDictionary, 加载外部的 ResourceDictionary 文件

    [源码下载] 背水一战 Windows 10 (11) - 资源: CustomResource, ResourceDictionary, 加载外部的 ResourceDictionary 文件 作者 ...

  8. 背水一战 Windows 10 (99) - 关联启动: 关联指定的文件类型, 关联指定的协议

    [源码下载] 背水一战 Windows 10 (99) - 关联启动: 关联指定的文件类型, 关联指定的协议 作者:webabcd 介绍背水一战 Windows 10 之 关联启动 关联指定的文件类型 ...

  9. 背水一战 Windows 10 (20) - 绑定: DataContextChanged, UpdateSourceTrigger, 对绑定的数据做自定义转换

    [源码下载] 背水一战 Windows 10 (20) - 绑定: DataContextChanged, UpdateSourceTrigger, 对绑定的数据做自定义转换 作者:webabcd 介 ...

随机推荐

  1. JMeter 通过JSON Extractor 插件来提取响应结果

    接口响应结果,通常为HTML.JSON格式的数据,对于HTML的响应结果的提取,可以通过正则表达式,也可以通过XPath 来提取. 对于JSON格式的数据,可以通过正则表达式.JSON Extract ...

  2. Eclipse中查看JDK类库源代码

    在Eclipse中编写代码时,有时候可能需要了解JDK类库的一些特性,这个时候可以通过查看类的源代码来了解JDK类的详细信息.本文主要内容就是如何直接在Eclipse开发环境中查看JDK类库源代码. ...

  3. 机器学习(四)--------逻辑回归(Logistic Regression)

    逻辑回归(Logistic Regression) 线性回归用来预测,逻辑回归用来分类. 线性回归是拟合函数,逻辑回归是预测函数 逻辑回归就是分类. 分类问题用线性方程是不行的   线性方程拟合的是连 ...

  4. CentOS7升级默认内核

    安装内核升级镜像源 rpm -Uvh https://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm Yum安装内核 yum --e ...

  5. CentOS查询端口占用和清除端口占用的程序

    1.查询端口号占用,根据端口查看进程信息 [root@server2 ~]# lsof -i: COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME httpd ...

  6. 服务管理之rsync

    目录 rsync 1. rsync简介 2. rsync特性 4. rsync命令 5. rsync+inotify rsync 1. rsync简介 rsync是linux系统下的数据镜像备份工具. ...

  7. Android学习(三)

    学号 20189214 <Android程序开发>第八周学习总结 教材学习内容总结 GridView GridView和ListView一样是AbsListView的子类; 都需要一个Ad ...

  8. UVA 10944 Nuts for nuts..

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=21&p ...

  9. 通俗易懂--SVM算法讲解(算法+案例)

    1.SVM讲解 新闻分类案例 SVM是一个很复杂的算法,不是一篇博文就能够讲完的,所以此篇的定位是初学者能够接受的程度,并且讲的都是SVM的一种思想,通过此篇能够使读着会使用SVM就行,具体SVM的推 ...

  10. (摘录)String是值传递还是引用传递

    String应该是一个封装类型,它应该是引用传递,是可以改变值的, 运行的结果应该是”cd”.我们实际运行一下看看, str=ab,这如何解释呢?难道String是基本类型?也说不通呀. 这就要从ja ...