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

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

了解如何启动统一资源标识符 (URI) 的默认应用。使用 URI,你可以在操作系统上启动其他应用以执行特定任务。例如,如果你要允许用户向应用中的联系人发送邮件,你可以使用 mailto: URI 启动用户的默认电子邮件应用。

这些步骤显示了如何使用 Windows.System.Launcher API 为 URI 启动默认处理程序。

说明

步骤 1: 创建 URI

为要启动的 URI 创建 Windows.Foundation.Uri 对象。此 URI 使用 http 方案名称。


// The URI to launch
var uriToLaunch = "https://www.bing.com";

// Create a Uri object from a URI string 
var uri = new Windows.Foundation.Uri(uriToLaunch);

步骤 2: 启动 URI

操作系统提供了用于为 URI 启动默认处理程序的多个不同选项。这些选项将在此图表中和以下各节中进行介绍。

选项 方法 描述
默认启动 LaunchUriAsync(Uri) 使用默认处理程序启动指定的 URI。
启动警告对话框 LaunchUriAsync(Uri, LauncherOptions) 操作系统将先显示警告对话框,然后启动指定 URI。
使用推荐的应用反馈启动 LaunchUriAsync(Uri, LauncherOptions) 使用默认处理程序启动指定的 URI。如果系统上未安装处理程序,则向用户推荐应用商店中的应用。
以所需的其余视图启动 LaunchUriAsync(Uri, LauncherOptions)(仅适用于 Windows) 使用默认处理程序启动指定的 URI。指定首选项以便在启动后停留于屏幕上,然后请求特定的窗口尺寸。

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

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

 

这些示例使用 Windows.System.Launcher.launchUriAsync 方法启动 URI。这是过载的方法。

Default launch

调用 Windows.System.Launcher.launchUriAsync(Uri) 方法以启动通过使用 http URI 的默认应用在步骤 1 中创建的 URI。

// Launch the URI
Windows.System.Launcher.launchUriAsync(uri).then(   
   function (success) {
      if (success) {
        // URI launched
      } else {
        // URI launch failed
      }
   });

Launch with a warning dialog

调用 Windows.System.Launcher.launchUriAsync(Uri, LauncherOptions) 方法以启动在步骤 1 中创建的 URI 并发出一个警告。使用 treatAsUntrusted 属性指示操作系统应显示一个警告。

注意  

如果设置了 treatAsUntrusted 属性,并且你使用 a 元素启动 URI,则在事件处理程序中调用 preventDefault

 

警告对话框覆盖了应用的灰显背景。对话框询问用户是否要切换应用并在右下方具有“是”和“否”按钮。“否”按钮高亮显示。

function linkClickHandler(eventInfo) {
    var link = eventInfo.target;
    if (eventInfo.srcElement && (
        (eventInfo.type === "click") ||
        (eventInfo.type === "keydown" && (
        eventInfo.keyCode === WinJS.Utilities.Key.enter ||
        eventInfo.keyCode === WinJS.Utilities.Key.space)))) {
        eventInfo.preventDefault();
        if (link.href.indexOf("ms-appx") > -1) {
            WinJS.Navigation.navigate(link.href);
        }
        else if (link.href.indexOf("http") > -1) {
            // Create a Uri object from a URI string 
            var uri = new Windows.Foundation.Uri(link.href);
            var options = new Windows.System.LauncherOptions();
            // Launch the URI with a warning prompt
            options.treatAsUntrusted = true;
            // Launch the URI
            Windows.System.Launcher.launchUriAsync(uri, options).then(
                function (success) {
                    if (success) {
                        // URI launched
                    } else {
                        // URI launch failed
                    }
                });
        }
    }
}

Launch with a recommended app fallback

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

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

 

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

// Set the recommended app.
var options = new Windows.System.LauncherOptions();
options.preferredApplicationPackageFamilyName = "Contoso.URIApp_8wknc82po1e";
options.preferredApplicationDisplayName = "Contoso URI App";

// Launch the URI and pass in the recommended app 
// in case the user has no apps installed to handle the URI
Windows.System.Launcher.launchUriAsync(uri, options).then(
   function (success) {
      if (success) {
        // Uri launched
      } else {
        // Uri launch failed
      }
   });

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

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

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

 

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

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

// Launch the URI with a desired remaining view
var options = new Windows.System.LauncherOptions();
options.desiredRemainingView = Windows.UI.ViewManagement.ViewSizePreference.useLess;

Windows.System.Launcher.launchUriAsync(uri, options).then(
   function (success) {
      if (success) {
        // URI launched
      } else {
        // URI launch failed
      }
   });

备注

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

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

必须指定 privateNetworkClientServer 功能才能启动 Intranet URI,例如,指向网络位置的 file:/// URI。

使用此方法无法启动本地区域中的 URI。例如,应用无法使用 file:/// URI 来访问本地设备上的文件。你必须改为使用 Storage APIs 才能访问这些文件。如果尝试启动 intranet URI 并且没有正确的功能或本地区域 URI,则启动将失败,且会调用错误回调。

完整示例

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

相关主题

任务

如何处理协议激活

如何启动文件的默认应用

指南

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

参考

Windows.System.Launcher.launchUriAsync