[源码下载]

背水一战 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. python note 16 re模块的使用

    1.re模块(#regex) # 查找 # findall : 匹配所有 每一项都是列表中的一个元素 import re ret = re.findall('\d+','dawdawd154wadwa ...

  2. java中解析excel 批量插入数据库

    Facade 层 实现类 (@Service("samePeriodModelImportFacade")) 1.  获取cells 的方法 public Cells getCel ...

  3. bootstrap table dataView展开行详情,p元素自动换行

    // bootstrap table 行详情展开,p元素自动换行1 .tableClass .detail-view p{ white-space: normal; }

  4. python zlib ,zlib 压缩流

    zlib 字符串:使用zlib.compress可以压缩字符串.使用zlib.decompress可以解压字符串. 数据流:压缩:compressobj,解压:decompressobj       ...

  5. python3 requestsGET请求传参

    GET方式传参方式一: import requests url = 'http://www.baidu.com/s?page=2' # 使用?携带参数 r = requests.get(url) pr ...

  6. 基础汇编指令(16bit 32bit 64bit)

    (zz from http://blog.luoyuanhang.com/) ##常见寄存器 寄存器 16位 32位 64位 累加寄存器 accumulator AX EAX RAX 基址寄存器 ba ...

  7. 教程:使用cPanel管理域名和数据库

    cPanel是一个基于web的基于web的控制面板,它简化了许多常见的系统管理任务,如网站创建.数据库部署和管理等.本指南向您展示了如何使用cPanel用户帐户管理域和数据库.所有这些指令都与位于端口 ...

  8. python 打包成 windows .EXE

    1. 升级pip python -m pip install --upgrade pip 2.安装 pyinstall (打包程序) pip install pyinstaller 3 开始打包(打包 ...

  9. ef 连接mysql,code first

    方法“MySql.Data.MySqlClient.MySqlProviderServices.GetDbProviderManifestToken(System.Data.Common.DbConn ...

  10. WebDriver中的操作使用

    1.WebDriver中使用jquery假如设定jquery包的路径为path,则程序如下: String jquery=null; FileInputStream input = new FileI ...