背水一战 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 之 控件(集合 ...
随机推荐
- CentOS7+CDH5.14.0安装全流程记录,图文详解全程实测-总目录
CentOS7+CDH5.14.0安装全流程记录,图文详解全程实测-总目录: 0.Windows 10本机下载Xshell,以方便往Linux主机上上传大文件 1.CentOS7+CDH5.14.0安 ...
- Zookeeper到底是干嘛的
在Zookeeper的官网上有这么一句话:ZooKeeper is a centralized service for maintaining configuration information, n ...
- JavaSE基础知识(5)—面向对象(方法的重写与重载)
一.重写 1.说明 子类对继承过来的父类的方法进行改造,这种现象称为方法的重写或覆盖或覆写(Override) 2.要求 方法签名完全一致,jdk5.0之后,允许返回类型可以是子类类型,权限修饰符可以 ...
- Synchronized和Lock的区别
①synchronized是jvm的关键字,Lock是Java类: ②synchronized会自动释放锁,而Lock需要在finally语句中主动释放锁,否则会造成死锁 ③用synchronized ...
- ASP.NET Core使用NLog记录日志到Microsoft Sql Server
在之前的文章中介绍了如何在ASP.NET Core使用NLog,本文为您介绍在ASP.NET Core使用NLog记录到Microsoft Sql Server 1.我们需要添加依赖: NLog.We ...
- hbase-连接流程
root和meta表 在版本0.9.8之前,存在root表,之后的版本中去除了root表,meta表主要记录了每个表在region的分布情况. meta结构 从表格中可以看出,rowkey格式:tab ...
- Swoole addProcess的使用
addProcess函数 是添加一个用户自定义的工作进程.这个有什么用呢?服务在启动后,可以用于监控.上报或者其他特殊的任务. 注意这个添加的进程是被manager进程管理的.如果这个添加的用户进程经 ...
- skynet记录2:模块简介
稍后填坑 bson.so client.so lpeg.so md5.so skynet.so sproto.so gate.so harbor.so logger.so snlua. ...
- lombok的简单介绍
##lombok的使用 一直在使用lombok的set和get,对其他的功能用的比较少,蓦然发现这个库好用的功能不要太多啊 有必要深入理解一番. ###lombok安装 1 需要IDE支持,不然开发的 ...
- UI设计学习之工具中的色彩模式分析
图像根据其呈现的颜色样式分为多种色彩模式,常见的为RGB模式.CMYK模式.灰度模式.位图模式和索引模式. RGB模式 这是Photoshop最常用的颜色模式,也称之为真彩色颜色模式,在RGB模式 ...