背水一战 Windows 10 (98) - 关联启动: 使用外部程序打开一个文件, 使用外部程序打开一个 Uri
作者:webabcd
介绍
背水一战 Windows 10 之 关联启动
- 使用外部程序打开一个文件
- 使用外部程序打开一个 Uri
示例
1、演示如何使用外部程序打开一个文件
AssociationLaunching/LaunchFile.xaml
<Page
x:Class="Windows10.AssociationLaunching.LaunchFile"
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 Background="Transparent">
<StackPanel Margin="10 0 10 10"> <TextBlock Name="lblMsg" Margin="5" /> <RadioButton Content="使用默认打开方式打开文件" Name="radDefault" GroupName="LaunchType" IsChecked="True" />
<RadioButton Content="使用默认打开方式打开文件,打开前弹出警告框" Name="radWarning" GroupName="LaunchType" />
<RadioButton Content="选择指定的打开方式打开文件" Name="radOpenWith" GroupName="LaunchType" /> <Button Content="打开一个 .jpg 格式文件" Name="btnLaunchFile" Click="btnLaunchFile_Click" Margin="5" /> </StackPanel>
</Grid>
</Page>
AssociationLaunching/LaunchFile.xaml.cs
/*
* 演示如何使用外部程序打开一个文件
*
* Launcher - 用于启动与指定文件相关的应用程序
* LaunchFileAsync(IStorageFile file) - 打开指定的文件
* LaunchFileAsync(IStorageFile file, LauncherOptions options) - 打开指定的文件
*
* LauncherOptions - 启动外部应用程序时的相关选项
* TreatAsUntrusted - 使用默认应用程序打开指定的文件时,是否弹出安全警告
* DisplayApplicationPicker - 是否弹出“打开方式”对话框
* UI.InvocationPoint - 指定“打开方式”对话框的显示位置
*
* 当指定的文件不被任何应用程序支持时,可以用以下下两种方法处理
* 1、指定 LauncherOptions.FallbackUri: 打开浏览器并跳转到指定的地址
* 2、指定 LauncherOptions.PreferredApplicationDisplayName 和 LauncherOptions.PreferredApplicationPackageFamilyName
* PreferredApplicationDisplayName - 指定在弹出的“在商店搜索”对话框中所显示的应用程序名称
* PreferredApplicationPackageFamilyName - 用户点击“在商店搜索”后,会在商店搜索指定 PackageFamilyName
*/ using System;
using Windows.ApplicationModel;
using Windows.Foundation;
using Windows.Storage;
using Windows.System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media; namespace Windows10.AssociationLaunching
{
public sealed partial class LaunchFile : Page
{
public LaunchFile()
{
this.InitializeComponent();
} private async void btnLaunchFile_Click(object sender, RoutedEventArgs e)
{
// 指定需要打开的文件
StorageFile file = await Package.Current.InstalledLocation.GetFileAsync(@"Assets\hololens.jpg"); // 指定打开文件过程中相关的各种选项
LauncherOptions options = new LauncherOptions();
if (radWarning.IsChecked.Value)
{
options.TreatAsUntrusted = true;
}
if (radOpenWith.IsChecked.Value)
{
Point openWithPosition = GetOpenWithPosition(btnLaunchFile); options.DisplayApplicationPicker = true;
options.UI.InvocationPoint = openWithPosition;
} // 使用外部程序打开指定的文件
bool success = await Launcher.LaunchFileAsync(file, options);
if (success)
{
lblMsg.Text = "打开成功";
}
else
{
lblMsg.Text = "打开失败";
}
} // 获取“打开方式”对话框的显示位置,即关联 Button 的左下角点的坐标
private Point GetOpenWithPosition(FrameworkElement element)
{
GeneralTransform buttonTransform = element.TransformToVisual(null); Point desiredLocation = buttonTransform.TransformPoint(new Point());
desiredLocation.Y = desiredLocation.Y + element.ActualHeight; return desiredLocation;
}
}
}
2、演示如何使用外部程序打开指定的 Uri
AssociationLaunching/LaunchUri.xaml
<Page
x:Class="Windows10.AssociationLaunching.LaunchUri"
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 Background="Transparent"> <StackPanel Margin="10 0 10 10">
<TextBlock Name="lblMsg" Margin="5" /> <RadioButton Content="使用默认打开方式打开指定的 Uri" Name="radDefault" GroupName="LaunchType" IsChecked="True" />
<RadioButton Content="使用默认打开方式打开指定的 Uri,打开前弹出警告框" Name="radWarning" GroupName="LaunchType" />
<RadioButton Content="选择指定的打开方式打开指定的 Uri" Name="radOpenWith" GroupName="LaunchType" /> <Button Content="打开一个 uri" Name="btnLaunchUri" Click="btnLaunchUri_Click" Margin="5" /> </StackPanel>
</Grid>
</Page>
AssociationLaunching/LaunchUri.xaml.cs
/*
* 演示如何使用外部程序打开指定的 Uri
*
* Launcher - 用于启动与指定 Uri 相关的应用程序
* LaunchUriAsync(Uri uri) - 打开指定的 Uri
* LaunchUriAsync(Uri uri, LauncherOptions options) - 打开指定的 Uri
* LaunchUriForResultsAsync(Uri uri, LauncherOptions options, ValueSet inputData) - 打开指定的 Uri,并可以获取到被激活的 app 返回的数据(参见 App2AppCommunication/LaunchUriForResults.xaml.cs)
*
* LauncherOptions - 启动外部应用程序时的相关选项
* TreatAsUntrusted - 使用默认应用程序打开指定的文件时,是否弹出安全警告
* DisplayApplicationPicker - 是否弹出“打开方式”对话框
* UI.InvocationPoint - 指定“打开方式”对话框的显示位置
* TargetApplicationPackageFamilyName - 如果要指定必须用某一目标程序打开的话,就指定此目标程序的 PackageFamilyName
*
* 当指定的 Uri 不被任何应用程序支持时,可以用以下下两种方法处理
* 1、指定 LauncherOptions.FallbackUri: 打开浏览器并跳转到指定的地址
* 2、指定 LauncherOptions.PreferredApplicationDisplayName 和 LauncherOptions.PreferredApplicationPackageFamilyName
* PreferredApplicationDisplayName - 指定在弹出的“在商店搜索”对话框中所显示的应用程序名称
* PreferredApplicationPackageFamilyName - 用户点击“在商店搜索”后,会在商店搜索指定 PackageFamilyName
*/ using System;
using Windows.Foundation;
using Windows.System;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media; namespace Windows10.AssociationLaunching
{
public sealed partial class LaunchUri : Page
{
public LaunchUri()
{
this.InitializeComponent();
} private async void btnLaunchUri_Click(object sender, RoutedEventArgs e)
{
// 指定需要打开的 Uri
Uri uri = new Uri("http://webabcd.cnblogs.com"); // 指定打开 Uri 过程中相关的各种选项
LauncherOptions options = new LauncherOptions();
if (radWarning.IsChecked.Value)
{
options.TreatAsUntrusted = true;
}
if (radOpenWith.IsChecked.Value)
{
Point openWithPosition = GetOpenWithPosition(btnLaunchUri); options.DisplayApplicationPicker = true;
options.UI.InvocationPoint = openWithPosition;
} // 使用外部程序打开指定的 Uri
bool success = await Launcher.LaunchUriAsync(uri, options);
if (success)
{
lblMsg.Text = "打开成功";
}
else
{
lblMsg.Text = "打开失败";
}
} // 获取“打开方式”对话框的显示位置,即关联 Button 的左下角点的坐标
private Point GetOpenWithPosition(FrameworkElement element)
{
GeneralTransform buttonTransform = element.TransformToVisual(null); Point desiredLocation = buttonTransform.TransformPoint(new Point());
desiredLocation.Y = desiredLocation.Y + element.ActualHeight; return desiredLocation;
}
}
}
OK
[源码下载]
背水一战 Windows 10 (98) - 关联启动: 使用外部程序打开一个文件, 使用外部程序打开一个 Uri的更多相关文章
- 背水一战 Windows 10 (99) - 关联启动: 关联指定的文件类型, 关联指定的协议
[源码下载] 背水一战 Windows 10 (99) - 关联启动: 关联指定的文件类型, 关联指定的协议 作者:webabcd 介绍背水一战 Windows 10 之 关联启动 关联指定的文件类型 ...
- 背水一战 Windows 10 (2) - UI: 概述, 启动屏幕, 屏幕方向
[源码下载] 背水一战 Windows 10 (2) - UI: 概述, 启动屏幕, 屏幕方向 作者:webabcd 介绍背水一战 Windows 10 之 UI UI 设计概述 启动屏幕(闪屏) 屏 ...
- 背水一战 Windows 10 (26) - XAML: x:DeferLoadStrategy, x:Null
[源码下载] 背水一战 Windows 10 (26) - XAML: x:DeferLoadStrategy, x:Null 作者:webabcd 介绍背水一战 Windows 10 之 XAML ...
- 背水一战 Windows 10 (120) - 后台任务: 后台上传任务
[源码下载] 背水一战 Windows 10 (120) - 后台任务: 后台上传任务 作者:webabcd 介绍背水一战 Windows 10 之 后台任务 后台上传任务 示例演示 uwp 的后台上 ...
- 背水一战 Windows 10 (119) - 后台任务: 后台下载任务(任务分组,组完成后触发后台任务)
[源码下载] 背水一战 Windows 10 (119) - 后台任务: 后台下载任务(任务分组,组完成后触发后台任务) 作者:webabcd 介绍背水一战 Windows 10 之 后台任务 后台下 ...
- 背水一战 Windows 10 (118) - 后台任务: 后台下载任务(任务分组,并行或串行执行,组完成后通知)
[源码下载] 背水一战 Windows 10 (118) - 后台任务: 后台下载任务(任务分组,并行或串行执行,组完成后通知) 作者:webabcd 介绍背水一战 Windows 10 之 后台任务 ...
- 背水一战 Windows 10 (117) - 后台任务: 后台下载任务
[源码下载] 背水一战 Windows 10 (117) - 后台任务: 后台下载任务 作者:webabcd 介绍背水一战 Windows 10 之 后台任务 后台下载任务 示例演示 uwp 的后台下 ...
- 背水一战 Windows 10 (100) - 应用间通信: 分享
[源码下载] 背水一战 Windows 10 (100) - 应用间通信: 分享 作者:webabcd 介绍背水一战 Windows 10 之 应用间通信 分享 示例1.本例用于演示如何开发一个分享的 ...
- 背水一战 Windows 10 (56) - 控件(集合类): ListViewBase - 基础知识, 拖动项
[源码下载] 背水一战 Windows 10 (56) - 控件(集合类): ListViewBase - 基础知识, 拖动项 作者:webabcd 介绍背水一战 Windows 10 之 控件(集合 ...
随机推荐
- python基础 ------ 集合
---恢复内容开始--- ----- 集合 使用场景:网购的订单.与商品ID 一一对应的商品信息 python的内置类型:List Tuple Dictionary ---- 列表 LI ...
- oracle中的exists 和 in 用法详解
以前一直不知道exists和in的用法与效率,这次的项目中需要用到,所以自己研究了一下.下面是我举两个例子说明两者之间的效率问题. 前言概述: “exists”和“in”的效率问题,涉及到效率问题也就 ...
- [leetcode]432. All O`one Data Structure全O(1)数据结构
Implement a data structure supporting the following operations: Inc(Key) - Inserts a new key with va ...
- SQL Server 异常解决:语句被终止。完成执行语句前已用完最大递归 100。
问题出现业务场景: 我司有个缺料分析报表,有一个字段是适用机种,需要通过BOM递归读取顶层父物料.这个错就是缺料分析报表执行时报的错: 原因分析定位: 通过网上一些资料,猜测应该是某个递归查询语句,遇 ...
- CentOS7.2 1511部署RabbitMQ
一.安装RabbitMQ依赖的的Erlang最小支持包(当然也可以安装完整的Erlang) 1.下载RabbitMQ的Erlang最小支持包源文件 git clone https://github.c ...
- Flask 验证码 点击验证码刷新
---恢复内容开始--- import random import string # Image:一个画布 # ImageDraw:一个画笔 # ImageFont:画笔的字体 # pip insta ...
- Springmvc中@RequestMapping 属性用法归纳
简介: @RequestMapping RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上.用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径. RequestM ...
- mysql8.0 1251错误
ALTER USER 'root'@'localhost' IDENTIFIED BY 'password' PASSWORD EXPIRE NEVER; ALTER USER '; FLUSH PR ...
- 2018上IEC计算机高级语言(C)作业 第3次作业_说明
一.博客作业内容 2018上IEC计算机高级语言(C)作业 第3次作业 二.评分规则说明 1.程序调试题,要描述出调试所遇到问题及修改内容,并表述清楚程序功能.流程图不规范的会减1-2分: 2.知识点 ...
- 将地图定位封装为ng指令
一.HTML结构<div tabindex="-1" class="modal fade in active modal-map" role=" ...