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

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

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

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

说明

步骤 1: 获取文件

首先,获取该文件的 Windows.Storage.StorageFile 对象。

如果该文件包含在应用的程序包中,则可以使用 Package.installedLocation 属性获取 Windows.Storage.StorageFolder 对象并且使用 Windows.Storage.StorageFolder.getFileAsync 方法获取 StorageFile 对象。

如果该文件在已知的文件夹中,则可以使用 Windows.Storage.KnownFolders 类的属性获取 StorageFolder 并且使用 getFileAsync 方法获取 StorageFile 对象。

步骤 2: 启动该文件

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

选项 方法 描述
默认启动 LaunchFileAsync(IStorageFile) 使用默认处理程序启动指定的文件。
打开方式启动 LaunchFileAsync(IStorageFile, LauncherOptions) 启动指定的文件,该文件让用户通过“打开方式”对话框选择处理程序。
使用推荐的应用反馈启动 LaunchFileAsync(IStorageFile, LauncherOptions) 使用默认处理程序启动指定的文件。如果系统上未安装处理程序,则向用户推荐应用商店中的应用。
以所需的其余视图启动 LaunchFileAsync(IStorageFile, LauncherOptions)(仅适用于 Windows) 使用默认处理程序启动指定的文件。指定首选项以便在启动后停留于屏幕上,然后请求特定的窗口尺寸。

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

Windows Phone:  LauncherOptions.DesiredRemainingView 不受 Windows Phone 支持。

 

Default launch

调用 Windows.System.Launcher.launchFileAsync(IStorageFile) 方法以启动默认的应用。此示例会使用 Windows.Storage.StorageFolder.getFileAsync 方法启动应用包中包含的图像文件 test.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) {
    // 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

LauncherOptions.displayApplicationPicker 设置为 true 的情况下调用 Windows.System.Launcher.launchFileAsync(IStorageFile, LauncherOptions) 方法以启动用户从**“打开方式”**对话框中选择的应用。

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

用于 .png 文件启动的“打开方式”对话框。该对话框中包含一个复选框,指定用户的选择应该用于所有 .png 文件还是仅用于此一个 .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 设置为该应用的名称。操作系统会使用此信息将在应用商店中搜索应用这一常规选项替换为从应用商店中获取推荐的应用这一具体选项。

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

 

用于 .contoso 文件启动的“打开方式”对话框。既然 .contoso 在计算机上未安装处理程序,那么该对话框中会包含一个选项,其中带有“存储”图标和向用户指出应用商店中正确处理程序的文本。该对话框还包含“更多选项”链接。


// 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 之前的版本中不受支持。

Windows Phone: LauncherOptions.DesiredRemainingView 不受 Windows Phone 支持。

// 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 文件中的脚本修改操作系统。

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

完整示例

请参阅关联启动示例 (Windows)

相关主题

任务

如何处理文件激活

如何启动 URI 的默认应用

指南

文件类型和 URI 的指南和清单

参考

Windows.Storage.StorageFile

Windows.System.Launcher.launchFileAsync