[源码下载]

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

作者:webabcd

介绍
背水一战 Windows 10 之 关联启动

  • 关联指定的文件类型
  • 关联指定的协议

示例
1、演示如何关联指定的文件类型(即用本程序打开指定类型的文件)
App.xaml.cs

        // 通过打开文件激活应用程序时所调用的方法
protected override void OnFileActivated(FileActivatedEventArgs args)
{
Frame rootFrame = new Frame();
rootFrame.Navigate(typeof(Windows10.AssociationLaunching.FileTypeAssociation), args);
Window.Current.Content = rootFrame; Window.Current.Activate();
}

AssociationLaunching/FileTypeAssociation.xaml

<Page
x:Class="Windows10.AssociationLaunching.FileTypeAssociation"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.AssociationLaunching"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Name="grid" Background="Transparent"> <TextBlock Name="lblMsg" TextWrapping="Wrap" Margin="5">
<Run>本程序可以打开 .webabcd 类型的文件</Run>
<LineBreak />
<Run>测试方法:在桌面新建一个文本文件,随便输一些字符,然后将后缀名改为 .webabcd,最后打开文件,本程序会显示其文本内容</Run>
</TextBlock> </Grid>
</Page>

AssociationLaunching/FileTypeAssociation.xaml.cs

/*
* 演示如何关联指定的文件类型(即用本程序打开指定类型的文件)
*
* 1、在 Package.appxmanifest 中新增一个“文件类型关联”声明,并做相关配置
* 本例中的这部分的配置如下
* <uap:Extension Category="windows.fileTypeAssociation">
* <uap:FileTypeAssociation Name=".webabcd">
* <uap:DisplayName>打开 .webabcd 文件</uap:DisplayName>
* <uap:Logo>Assets\StoreLogo.png</uap:Logo>
* <uap:InfoTip>用 win10 demo 打开 .webabcd 文件</uap:InfoTip>
* <uap:SupportedFileTypes>
* <uap:FileType>.webabcd</uap:FileType>
* </uap:SupportedFileTypes>
* </uap:FileTypeAssociation>
* </uap:Extension>
* 2、在 App.xaml.cs 中 override void OnFileActivated(FileActivatedEventArgs args),以获取相关的文件信息
*
* FileActivatedEventArgs - 通过打开文件激活应用程序时的事件参数
* Files - 相关的文件信息
* Kind - 此 app 被激活的类型(ActivationKind 枚举)
* 比如,如果是通过“打开文件”激活的话,则此值为 File
* PreviousExecutionState - 此 app 被激活前的状态(ApplicationExecutionState 枚举)
* 比如,如果此 app 被激活前就是运行状态的或,则此值为 Running
* NeighboringFilesQuery - 获取当前文件的相邻文件
* SplashScreen - 获取此 app 的 SplashScreen 对象
* User - 获取激活了此 app 的 User 对象
*/ using System;
using Windows.ApplicationModel.Activation;
using Windows.Storage;
using Windows.UI;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation; namespace Windows10.AssociationLaunching
{
public sealed partial class FileTypeAssociation : Page
{
private FileActivatedEventArgs _fileActivated; public FileTypeAssociation()
{
this.InitializeComponent();
} protected async override void OnNavigatedTo(NavigationEventArgs e)
{
// 获取 FileActivatedEventArgs 对象(从 App.xaml.cs 传来的)
_fileActivated = e.Parameter as FileActivatedEventArgs; // 获取文件中的文本内容,并显示
if (_fileActivated != null)
{
grid.Background = new SolidColorBrush(Colors.Blue);
lblMsg.Foreground = new SolidColorBrush(Colors.White); IStorageFile isf = _fileActivated.Files[] as IStorageFile;
lblMsg.Text = $"激活程序的文件是“{isf.Name}”,其文本内容为:{await FileIO.ReadTextAsync(isf)}";
}
}
}
}

2、演示如何关联指定的协议(即用本程序处理指定的协议)
App.xaml.cs

        protected override void OnActivated(IActivatedEventArgs args)
{
// 通过协议激活应用程序时(参见 AssociationLaunching/ProtocolAssociation.xaml.cs 中的示例)
if (args.Kind == ActivationKind.Protocol)
{
ProtocolActivatedEventArgs protocolArgs = args as ProtocolActivatedEventArgs; Frame rootFrame = new Frame();
rootFrame.Navigate(typeof(Windows10.AssociationLaunching.ProtocolAssociation), protocolArgs);
Window.Current.Content = rootFrame;
}
}

AssociationLaunching/ProtocolAssociation.xaml

<Page
x:Class="Windows10.AssociationLaunching.ProtocolAssociation"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows10.AssociationLaunching"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Name="grid" Background="Transparent"> <StackPanel Margin="10 0 10 10">
<TextBlock Name="lblMsg" Margin="5">
<Run>本程序可以处理 webabcd: 协议</Run>
</TextBlock> <Button Name="btnProtocol" Content="打开自定义协议 webabcd:data" Click="btnProtocol_Click" Margin="5" />
</StackPanel> </Grid>
</Page>

AssociationLaunching/ProtocolAssociation.xaml.cs

/*
* 演示如何关联指定的协议(即用本程序处理指定的协议)
*
* 1、在 Package.appxmanifest 中新增一个“协议”声明,并做相关配置
* 本例中的这部分的配置如下,协议名必须全小写
* <uap:Extension Category="windows.protocol">
* <uap:Protocol Name="webabcd">
* <uap:Logo>Assets\StoreLogo.png</uap:Logo>
* </uap:Protocol>
* </uap:Extension>
* 2、在 App.xaml.cs 中 override void OnActivated(IActivatedEventArgs args),以获取相关的协议信息
*
*
* ProtocolActivatedEventArgs - 通过协议激活应用程序时的事件参数
* Uri - 协议的 uri
* CallerPackageFamilyName - 激活当前 app 的 app 的 PackageFamilyName
* Kind - 此 app 被激活的类型(ActivationKind 枚举)
* 本例为 ActivationKind.Protocol
* PreviousExecutionState - 此 app 被激活前的状态(ApplicationExecutionState 枚举)
* 比如,如果此 app 被激活前就是运行状态的或,则此值为 Running
* SplashScreen - 获取此 app 的 SplashScreen 对象
* User - 获取激活了此 app 的 User 对象
*/ using System;
using Windows.ApplicationModel.Activation;
using Windows.System;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation; namespace Windows10.AssociationLaunching
{
public sealed partial class ProtocolAssociation : Page
{
private ProtocolActivatedEventArgs _protocolArgs; public ProtocolAssociation()
{
this.InitializeComponent();
} protected override void OnNavigatedTo(NavigationEventArgs e)
{
// 获取 ProtocolActivatedEventArgs 对象(从 App.xaml.cs 传来的)
_protocolArgs = e.Parameter as ProtocolActivatedEventArgs; // 显示协议的详细信息
if (_protocolArgs != null)
{
grid.Background = new SolidColorBrush(Colors.Blue);
lblMsg.Foreground = new SolidColorBrush(Colors.White); lblMsg.Text = "激活程序的自定义协议为: " + _protocolArgs.Uri.AbsoluteUri;
}
} private async void btnProtocol_Click(object sender, RoutedEventArgs e)
{
// 打开自定义协议 webabcd:data
Uri uri = new Uri("webabcd:data");
await Launcher.LaunchUriAsync(uri); // 打开 IE 浏览器,在地址栏输入 webabcd:data,即会打开本程序来处理此协议
}
}
}

OK
[源码下载]

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

  1. 背水一战 Windows 10 (91) - 文件系统: Application Data 中的文件操作, Application Data 中的“设置”操作, 通过 uri 引用 Application Data 中的媒体

    [源码下载] 背水一战 Windows 10 (91) - 文件系统: Application Data 中的文件操作, Application Data 中的“设置”操作, 通过 uri 引用 Ap ...

  2. 背水一战 Windows 10 (90) - 文件系统: 获取 Package 中的文件, 可移动存储中的文件操作, “库”管理

    [源码下载] 背水一战 Windows 10 (90) - 文件系统: 获取 Package 中的文件, 可移动存储中的文件操作, “库”管理 作者:webabcd 介绍背水一战 Windows 10 ...

  3. 与众不同 windows phone (38) - 8.0 关联启动: 使用外部程序打开一个文件或URI, 关联指定的文件类型或协议

    [源码下载] 与众不同 windows phone (38) - 8.0 关联启动: 使用外部程序打开一个文件或URI, 关联指定的文件类型或协议 作者:webabcd 介绍与众不同 windows ...

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

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

  5. 重新想象 Windows 8 Store Apps (33) - 关联启动: 使用外部程序打开一个文件或uri, 关联指定的文件类型或协议

    原文:重新想象 Windows 8 Store Apps (33) - 关联启动: 使用外部程序打开一个文件或uri, 关联指定的文件类型或协议 [源码下载] 重新想象 Windows 8 Store ...

  6. 背水一战 Windows 10 (2) - UI: 概述, 启动屏幕, 屏幕方向

    [源码下载] 背水一战 Windows 10 (2) - UI: 概述, 启动屏幕, 屏幕方向 作者:webabcd 介绍背水一战 Windows 10 之 UI UI 设计概述 启动屏幕(闪屏) 屏 ...

  7. 背水一战 Windows 10 (101) - 应用间通信: 通过协议打开指定的 app 并传递数据以及获取返回数据, 将本 app 沙盒内的文件共享给其他 app 使用

    [源码下载] 背水一战 Windows 10 (101) - 应用间通信: 通过协议打开指定的 app 并传递数据以及获取返回数据, 将本 app 沙盒内的文件共享给其他 app 使用 作者:weba ...

  8. 背水一战 Windows 10 (122) - 其它: 通过 Windows.System.Profile 命名空间下的类获取信息, 查找指定类或接口的所在程序集的所有子类和子接口

    [源码下载] 背水一战 Windows 10 (122) - 其它: 通过 Windows.System.Profile 命名空间下的类获取信息, 查找指定类或接口的所在程序集的所有子类和子接口 作者 ...

  9. 背水一战 Windows 10 (106) - 通知(Toast): 通过 toast 打开协议, 通过 toast 选择在指定的时间之后延迟提醒或者取消延迟提醒

    [源码下载] 背水一战 Windows 10 (106) - 通知(Toast): 通过 toast 打开协议, 通过 toast 选择在指定的时间之后延迟提醒或者取消延迟提醒 作者:webabcd ...

随机推荐

  1. Find out where to contain the smartforms

    Go to table E071 and give smarforms name and it will give the transport req for that. Run SE03, choo ...

  2. 七牛存储qshell工具

    ---恢复内容开始--- 工具地址:https://developer.qiniu.com/kodo/tools/1302/qshell 下载完成后:根据自己的系统选择需要的可执行文件,支持linux ...

  3. 分布式事务Hmily TCC源码--学习整合

    一.什么是分布式事务 分布式事务是指事务的参与者.支持事务的服务器.资源服务器以及事务管理器分别位于不同的分布式系统的不同节点上, 本质上来说,分布式事务是为了保证不同数据库的数据一致性 TCC事务主 ...

  4. T4设计时模板调试

    在Visual Studio内调试T4设计时模板有多个方法:安装使用带调试功能的第三方工具,利用System.Diagnostics.Debugger实时调试器,VS内置的T4调试工具.使用第三方工具 ...

  5. 20175234 2018-2019-2 《Java程序设计》第九周学习总结

    目录 20175234 2018-2019-2 <Java程序设计>第九周学习总结 教材学习内容总结 教材学习中的问题和解决过程 代码托管 感想 学习进度条 参考资料 20175234 2 ...

  6. 十三、实现Comparable接口和new Comparator<T>(){ }排序的实现过程

    参考:https://www.cnblogs.com/igoodful/p/9517784.html Collections有两种比较规则方式,第一种是使用自身的比较规则: 该类必须实现Compara ...

  7. Django之ORM操作

    Django之ORM操作 前言 Django框架功能齐全自带数据库操作功能,本文主要介绍Django的ORM框架 到目前为止,当我们的程序涉及到数据库相关操作时,我们一般都会这么搞: 创建数据库,设计 ...

  8. 使用DOS命令无法启动MySQL

    今天使用命令dos 命令 net start mysql 启动mysql的使用出现以下情况 无法正常启动mysql服务. 原因是: 启动dos命令窗口时的用户权限太低,无法正常使用 解决办法: 搜索c ...

  9. Java学习笔记day_01

    Java学习笔记(复习整理) 虽然不知道该怎么写,但是不起步就永远不知道该怎么做..刚开始可能会写的很差劲,但会一点一点变好的. 本笔记是以我按照传智播客的视频和Java核心思想来学习,前面的基础部分 ...

  10. python的语法小结之生成器和迭代器

    生成器: 首先介绍一下列表生成式:a=[x for x in range(10)]               >>>>>>[0, 1, 2, 3, 4, 5, 6 ...