[源码下载]

背水一战 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的更多相关文章

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

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

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

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

  3. 背水一战 Windows 10 (26) - XAML: x:DeferLoadStrategy, x:Null

    [源码下载] 背水一战 Windows 10 (26) - XAML: x:DeferLoadStrategy, x:Null 作者:webabcd 介绍背水一战 Windows 10 之 XAML ...

  4. 背水一战 Windows 10 (120) - 后台任务: 后台上传任务

    [源码下载] 背水一战 Windows 10 (120) - 后台任务: 后台上传任务 作者:webabcd 介绍背水一战 Windows 10 之 后台任务 后台上传任务 示例演示 uwp 的后台上 ...

  5. 背水一战 Windows 10 (119) - 后台任务: 后台下载任务(任务分组,组完成后触发后台任务)

    [源码下载] 背水一战 Windows 10 (119) - 后台任务: 后台下载任务(任务分组,组完成后触发后台任务) 作者:webabcd 介绍背水一战 Windows 10 之 后台任务 后台下 ...

  6. 背水一战 Windows 10 (118) - 后台任务: 后台下载任务(任务分组,并行或串行执行,组完成后通知)

    [源码下载] 背水一战 Windows 10 (118) - 后台任务: 后台下载任务(任务分组,并行或串行执行,组完成后通知) 作者:webabcd 介绍背水一战 Windows 10 之 后台任务 ...

  7. 背水一战 Windows 10 (117) - 后台任务: 后台下载任务

    [源码下载] 背水一战 Windows 10 (117) - 后台任务: 后台下载任务 作者:webabcd 介绍背水一战 Windows 10 之 后台任务 后台下载任务 示例演示 uwp 的后台下 ...

  8. 背水一战 Windows 10 (100) - 应用间通信: 分享

    [源码下载] 背水一战 Windows 10 (100) - 应用间通信: 分享 作者:webabcd 介绍背水一战 Windows 10 之 应用间通信 分享 示例1.本例用于演示如何开发一个分享的 ...

  9. 背水一战 Windows 10 (56) - 控件(集合类): ListViewBase - 基础知识, 拖动项

    [源码下载] 背水一战 Windows 10 (56) - 控件(集合类): ListViewBase - 基础知识, 拖动项 作者:webabcd 介绍背水一战 Windows 10 之 控件(集合 ...

随机推荐

  1. centos7 安装部署jenkins

    一.简介 jenkins是一个Java开发的开源持续集成工具,广泛用于项目开发,具有自动化构建.测试和部署等功能,它的运行需要Java环境. 二.搭建环境准备:# cat /etc/redhat-re ...

  2. .gitinore配置失效问题

    问题:在.gitinore中配置忽略项,配置失效 原因:新增加忽略项已经提交过,在暂存区或分支上被版本控制 解决:删除暂存区或分支上的文件(本地需要使用, 只是不希望这个文件被版本控制), 可以使用 ...

  3. python读取数据库出txt报表

    python出报表使用到了数据库访问,文件读写,字符串切片处理.还可以扩展到电子邮件的发送,异常处理以及定时批任务. 总之在学习中发现还是有蛮多乐趣在其中. #coding=utf-8 ' impor ...

  4. python 杨辉三角实现逻辑

    程序输出需要实现如下效果: [1] [1,1] [1,2,1] [1,3,3,1] ...... 方法:迭代,生成器 def triangles() L = [1] while True: yiled ...

  5. python requests与aiohttp 速度对比

    环境:centos7 python3.6 测试网址:www.bai.com 测试方式:抓取百度100次 结果: aio: 10.702147483825684srequests: 12.4046785 ...

  6. Finance API文档

    0. 公共部分 请求url {apiRoot}/{method}?ver={version}&appkey={appkey}&sign={sign} 参数名 说明 示例 apiRoot ...

  7. sql语句性能优化

    需要的准备知识 1最左前缀匹配 mysql会一直向右匹配直到遇到范围查询(>.<.between.like)就停止匹配, 对于where条件 a = 1 and b> 2 and c ...

  8. AX_CreateAndPostPurch

    static void CreateAndPostPurch(Args _args) { List il = new List(Types::Record); DocumentNum Document ...

  9. 每天五分钟,玩转Docker。

    Docker技术在国内如火如荼的流行了起来,我当然也想要赶上这时髦的技术啦.下面,我将重新拾起一年多未用的Docker,继续我的云计算之路. Day 1  学习Docker,先从Docker的命令行工 ...

  10. [Ionic] Error: No provider for Http! Error: No provider for Http!

    1. 打开src/app/app.module.ts 2. 在最上面导入 import{HttpModule} from '@angular/http'; 3. 在imports块中加入:HttpMo ...