修改html文件的缺省应用,如何启动文件的默认应用 (HTML)

如何启动文件的默认应用 (HTML)

12/11/2015

本文内容

[ 本文适用于编写 Windows 运行时应用的 Windows 8.x 和 Windows Phone 8.x 开发人员。如果你要针对 Windows 10 进行开发,请参阅 最新文档 ]

了解如何启动文件的默认应用。很多应用需要使用它们自身无法处理的文件。例如,电子邮件应用接收大量文件类型并且需要使用一种方式在其默认处理程序中启动这些文件。

这些步骤显示了如何使用 Windows.System.Launcher API 为应用自身无法处理的文件启动默认处理程序。

说明

步骤 1: 获取文件

步骤 2: 启动该文件

Windows 提供了用于为文件启动默认处理程序的多个不同选项。以下图表和以下部分中介绍了这些选项。

选项

方法

描述

使用推荐的应用反馈启动

使用默认处理程序启动指定的文件。如果系统上未安装处理程序,则向用户推荐应用商店中的应用。

以所需的其余视图启动

使用默认处理程序启动指定的文件。指定首选项以便在启动后停留于屏幕上,然后请求特定的窗口尺寸。

Windows 8.1:LauncherOptions.DesiredRemainingView 在 Windows 8.1 和 Windows Server 2012 R2 之前的版本中不受支持。

Default launch

// Path to the file in the app package to launch

var imageFile = "images\\test.png";

// Get the image file from the package's image directory

Windows.ApplicationModel.Package.current.installedLocation.getFileAsync(imageFile).then(

function (file) {

// Launch the retrieved file using the default app

Windows.System.Launcher.launchFileAsync(file).then(

function (success) {

if (success) {

// File launched

} else {

// File launch failed

}

});

});

Open with launch

当用户希望选择默认应用以外的应用来打开某个特定文件时,我们建议你使用“打开方式”对话框。 例如,如果你的应用允许用户启动某个图像文件,则默认的处理程序将可能是查看器应用。 在某些情况下,用户可能需要编辑图像而不只是查看图像。使用“打开方式”****选项及“应用程序栏”或上下文菜单中的备用命令,让用户在此类情况下打开“打开方式”****对话框并选择编辑器应用。

1bcec573a0e7315090cd8f2fd6f8c4c2.png

// Path to the file in the app package to launch

var imageFile = "images\\test.png";

// Get the image file from the package's image directory

Windows.ApplicationModel.Package.current.installedLocation.getFileAsync(imageFile).then(

function (file) {

// Set the show picker option

var options = new Windows.System.LauncherOptions();

options.displayApplicationPicker = true;

// Launch the retrieved file using the selected app

Windows.System.Launcher.launchFileAsync(file, options).then(

function (success) {

if (success) {

// File launched

} else {

// File launch failed

}

});

});

Launch with a recommended app fallback

在某些情况下,用户可能未安装用以处理所启动文件的应用。默认情况下,为处理此类情况,操作系统会向用户提供一个链接,帮助其在应用商店中搜索相应的应用。如果你希望为用户提供具体的建议,告知他们在此情况下应获取何种应用,则可以随所启用的文件传递该建议。若要执行此操作,调用 Windows.System.Launcher.launchFileAsync(IStorageFile, LauncherOptions) 方法,将 LauncherOptions.preferredApplicationPackageFamilyName 设置为应用商店中要推荐的应用的程序包系列名称。 然后,将 LauncherOptions.preferredApplicationDisplayName 设置为该应用的名称。操作系统会使用此信息将在应用商店中搜索应用这一常规选项替换为从应用商店中获取推荐的应用这一具体选项。

注意  必须设置这些选项才能推荐应用。设置一个而不设置另一个将导致出现故障。

50b5d2a11f86db1624b542245a86170a.png

// Path to the file in the app package to launch

var imageFile = "images\\test.contoso";

// Get the image file from the package's image directory

Windows.ApplicationModel.Package.current.installedLocation.getFileAsync(imageFile).then(

function (file) {

// Set the recommended app

var options = new Windows.System.LauncherOptions();

options.preferredApplicationPackageFamilyName = "Contoso.FileApp_8wknc82po1e";

options.preferredApplicationDisplayName = "Contoso File App";

// Launch the retrieved file pass in the recommended app

// in case the user has no apps installed to handle the file

Windows.System.Launcher.launchFileAsync(file, options).then(

function (success) {

if (success) {

// File launched

} else {

// File launch failed

}

});

});

以所需的其余视图启动(仅适用于 Windows)

调用 LaunchFileAsync 的源应用可请求在文件启动后停留于屏幕上。默认情况下,Windows 会尝试在负责处理该文件的源应用和目标应用之间平等地共享可用空间。源应用可借助 DesiredRemainingView 属性向操作系统指示希望其应用占用较多或较少的可用空间。此外,还可使用 DesiredRemainingView 以指示源应用在文件启动后无需停留于屏幕上,并可由目标应用完全替代。此属性仅指定调用应用的首选窗口大小。不指定可能会同时显示在屏幕上的其他应用的行为。

注意  Windows 在确定目标应用的最终窗口尺寸时会考虑多个不同因素;例如,源应用的首选项、屏幕上的应用数量以及屏幕的方向。设置 DesiredRemainingView 并不保证为目标应用设定具体的窗口化行为。

Windows 8.1: LauncherOptions.DesiredRemainingView 在 Windows 8.1 和 Windows Server 2012 R2 之前的版本中不受支持。

// Path to the file in the app package to launch

var imageFile = "images\\test.png";

// Get the image file from the package's image directory

Windows.ApplicationModel.Package.current.installedLocation.getFileAsync(imageFile).done(

function (file) {

// Set the desired remaining view

var options = new Windows.System.LauncherOptions();

options.desiredRemainingView = Windows.UI.ViewManagement.ViewSizePreference.useLess;

// Launch the retrieved file using the selected app

Windows.System.Launcher.launchFileAsync(file, options).done(

function (success) {

if (success) {

// File launched

} else {

// File launch failed

}

});

});

备注

你的应用不能选择要启动的应用。用户将确定要启动哪个应用。用户可以选择一个 Windows 应用商店应用或桌面应用。

启动文件时,你的应用必须是前台应用,即对于用户必须是可见的。此要求有助于确保用户保持控制。为满足此要求,需确保将文件的所有启动都直接绑定到应用的 UI 中。 大多数情况下,用户总是必须采取某个操作来发起文件启动。如果你尝试启动某个文件并且你的应用不在前台,则启动将失败,且会调用错误回调。

如果包含代码或脚本的文件类型(例如 .exe、.msi 和 .js 文件)由操作系统自动执行,则你无法启动这些文件类型。此限制可防止用户遭受可能修改操作系统的潜在恶意文件的损害。如果可以包含脚本的文件类型由可隔离脚本的应用来执行(例如 .docx 文件),则你可以使用此方法来启动这些文件类型。Microsoft Word 之类的应用可防止 .docx 文件中的脚本修改操作系统。

如果你尝试启动受限制的文件类型,则启动将失败,且会调用错误回调。如果你的应用处理许多不同类型的文件,并且你预计会遇到该错误,则应该为你的用户提供回退体验。例如,你可以为用户提供将文件保存到桌面的选项,然后用户可以从桌面打开该文件。

完整示例

相关主题

任务

指南

参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值