启动 URI 的默认应用Launch the default app for a URI

重要的 APIImportant APIs

了解如何启动统一资源标识符 (URI) 的默认应用。Learn how to launch the default app for a Uniform Resource Identifier (URI). URI 允许你启动其他应用以执行特定任务。URIs allow you to launch another app to perform a specific task. 本主题还提供许多内置于 Windows 的 URI 方案的概述。This topic also provides an overview of the many URI schemes built into Windows. 你也可以启动自定义 URI。You can launch custom URIs too. 有关注册自定义 URI 方案和处理 URI 激活的详细信息,请参阅处理 URI 激活For more info about registering a custom URI scheme and handling URI activation, see Handle URI activation.

URI 方案允许你通过单击超链接来打开应用。URI schemes let you open apps by clicking hyperlinks. 正如可以使用 mailto: 开始打开电子邮件,你可以使用 http: 打开默认 Web 浏览器Just as you can start a new email using mailto:, you can open the default web browser using http:

本主题介绍了以下内置于 Windows 的 URI 方案:This topic describes the following URI schemes built into Windows:

URI 方案URI Scheme 启动Launches
bingmaps:、ms-drive-to: 和 ms-walk-to:bingmaps:, ms-drive-to:, and ms-walk-to: “地图”应用Maps app
http:http: 默认 Web 浏览器Default web browser
mailto:mailto: 默认电子邮件应用Default email app
ms-call:ms-call: 调用应用Call app
ms-chat:ms-chat: “消息”应用Messaging app
ms-people:ms-people: “人脉”应用People app
ms-photos:ms-photos: “照片”应用Photos app
ms-settings:ms-settings: “设置”应用Settings app
ms-store:ms-store: “应用商店”应用Store app
ms-tonepicker:ms-tonepicker: 音调选取器Tone picker
ms-yellowpage:ms-yellowpage: “114 查号”应用Nearby Numbers app
msnweather:msnweather: 天气应用Weather app


例如,以下 URI 打开默认浏览器并显示必应网站。For example, the following URI opens the default browser and displays the Bing web site.

http://bing.com

你还可以启动自定义 URI 方案。You can also launch custom URI schemes too. 如果未安装处理该 URI 的应用,你可以建议用户安装应用。If there is no app installed to handle that URI, you can recommend an app for the user to install. 有关详细信息,请参阅在没有可以处理该 URI 的应用时推荐一个应用For more info, see Recommend an app if one is not available to handle the URI.

通常,你的应用不能选择要启动的应用。In general, your app can't select the app that is launched. 用户确定启动哪个应用。The user determines which app is launched. 可以注册多个应用来处理同一个 URI 方案。More than one app can register to handle the same URI scheme. 上述操作不适用于保留 URI 方案。The exception to this is for reserved URI schemes. 忽略注册保留 URI 方案。Registrations of reserved URI schemes are ignored. 有关保留 URI 方案的完整列表,请参阅处理 URI 激活For the full list of reserved URI schemes, see Handle URI activation. 在多个应用可能已注册同一个 URI 方案的情况下,你的应用可以推荐一个要启动的特定应用。In cases where more than one app may have registered the same URI scheme, your app can recommend a specific app to be launched. 有关详细信息,请参阅在没有可以处理该 URI 的应用时推荐一个应用For more info, see Recommend an app if one is not available to handle the URI.

调用 LaunchUriAsync 以启动 URICall LaunchUriAsync to launch a URI

使用 LaunchUriAsync 方法启动 URI。Use the LaunchUriAsync method to launch a URI. 调用此方法时,你的应用必须是前台应用,即对于用户必须是可见的。When you call this method, your app must be the foreground app, that is, it must be visible to the user. 此要求有助于确保用户保持控制。This requirement helps ensure that the user remains in control. 为满足此要求,请确保将 URI 的所有启动都直接绑定到到应用的 UI 中。To meet this requirement, make sure that you tie all URI launches directly to the UI of your app. 用户必须总是采取某种操作来发起 URI 启动。The user must always take some action to initiate a URI launch. 如果尝试启动 URI 并且你的应用不在前台运行,则启动将失败,且会调用错误回调。If you attempt to launch a URI and your app isn't in the foreground, the launch will fail and your error callback will be invoked.

首先创建 System.Uri 对象来表示 URI,然后将其传递给 LaunchUriAsync 方法。First create a System.Uri object to represent the URI, then pass that to the LaunchUriAsync method. 使用返回结果以查看调用是否成功,如以下示例所示。Use the return result to see if the call succeeded, as shown in the following example.

private async void launchURI_Click(object sender, RoutedEventArgs e)
{
   // The URI to launch
   var uriBing = new Uri(@"http://www.bing.com");

   // Launch the URI
   var success = await Windows.System.Launcher.LaunchUriAsync(uriBing);

   if (success)
   {
      // URI launched
   }
   else
   {
      // URI launch failed
   }
}

在某些情况下,操作系统将提示用户查看是否确实要切换应用。In some cases, the operating system will prompt the user to see if they actually want to switch apps.

警告对话框覆盖了应用的灰显背景。

如果始终希望出现此提示,请使用 Windows.System.LauncherOptions.TreatAsUntrusted 属性告诉操作系统显示一个警告。If you always want this prompt to occur, use the Windows.System.LauncherOptions.TreatAsUntrusted property to tell the operating system to display a warning.

// The URI to launch
var uriBing = new Uri(@"http://www.bing.com");

// Set the option to show a warning
var promptOptions = new Windows.System.LauncherOptions();
promptOptions.TreatAsUntrusted = true;

// Launch the URI
var success = await Windows.System.Launcher.LaunchUriAsync(uriBing, promptOptions);

在没有可以处理该 URI 的应用时推荐一个应用Recommend an app if one is not available to handle the URI

在某些情况下,用户可能未安装用以处理所启动 URI 的应用。In some cases, the user might not have an app installed to handle the URI that you are launching. 默认情况下,为处理此类情况,操作系统会向用户提供一个链接,帮助其在应用商店中搜索相应的应用。By default, the operating system handles these cases by providing the user with a link to search for an appropriate app on the store. 如果你希望为用户提供具体的建议,告知他们在此情况下应获取何种应用,则可以随所启用的 URI 传递该建议。If you want to give the user a specific recommendation for which app to acquire in this scenario, you can do so by passing that recommendation along with the URI that you are launching.

当多个应用注册为处理某个 URI 方案时,推荐还是有用的。Recommendations are also useful when more than one app has registered to handle a URI scheme. 通过推荐一个特定应用,Windows 将打开该应用(如果已安装该应用)。By recommending a specific app, Windows will open that app if it is already installed.

若要进行推荐,则调用 Windows.System.Launcher.LaunchUriAsync(Uri, LauncherOptions) 方法,并将 LauncherOptions.preferredApplicationPackageFamilyName 设置为应用商店中要推荐的应用的程序包系列名称。To make a recommendation, call the Windows.System.Launcher.LaunchUriAsync(Uri, LauncherOptions) method with LauncherOptions.preferredApplicationPackageFamilyName set to the package family name of the app in the store that you want to recommend. 操作系统会使用此信息将在应用商店中搜索应用这一常规选项替换为从应用商店中获取推荐的应用这一具体选项。The operating system uses this info to replace the general option to search for an app in the store with a specific option to acquire the recommended app from the store.

// Set the recommended app
var options = new Windows.System.LauncherOptions();
options.PreferredApplicationPackageFamilyName = "Contoso.URIApp_8wknc82po1e";
options.PreferredApplicationDisplayName = "Contoso URI Ap";

// Launch the URI and pass in the recommended app
// in case the user has no apps installed to handle the URI
var success = await Windows.System.Launcher.LaunchUriAsync(uriContoso, options);

设置其余视图首选项Set remaining view preference

调用 LaunchUriAsync 的源应用可请求在 URI 启动后停留于屏幕上。Source apps that call LaunchUriAsync can request that they remain on screen after a URI launch. 默认情况下,Windows 会尝试在处理该 URI 的源应用和目标应用之间平等地共享所有可用空间。By default, Windows attempts to share all available space equally between the source app and the target app that handles the URI. 源应用可使用 DesiredRemainingView 属性向操作系统指示希望其应用占用较多或较少的可用空间。Source apps can use the DesiredRemainingView property to indicate to the operating system that they prefer their app window to take up more or less of the available space. 此外,还可使用 DesiredRemainingView 指示源应用在 URI 启动后无需停留于屏幕上,并可由目标应用完全替代。DesiredRemainingView can also be used to indicate that the source app doesn't need to remain on screen after the URI launch and can be completely replaced by the target app. 此属性仅指定调用应用的首选窗口大小。This property only specifies the preferred window size of the calling app. 不指定可能会同时显示在屏幕上的其他应用的行为。It doesn't specify the behavior of other apps that may happen to also be on screen at the same time.

注意Windows 时会考虑多个不同因素它确定源应用的最终窗口尺寸,例如源应用的首选项、 屏幕、 屏幕方向等上的应用数量。NoteWindows takes into account multiple different factors when it determines the source app's final window size, for example, the preference of the source app, the number of apps on screen, the screen orientation, and so on. 设置 DesiredRemainingView 并不能保证为源应用设定具体的窗口化行为。By setting DesiredRemainingView, you aren't guaranteed a specific windowing behavior for the source app.

// Set the desired remaining view.
var options = new Windows.System.LauncherOptions();
options.DesiredRemainingView = Windows.UI.ViewManagement.ViewSizePreference.UseLess;

// Launch the URI
var success = await Windows.System.Launcher.LaunchUriAsync(uriContoso, options);

URI 方案URI Schemes

下面介绍了各种 URI 方案。The various URI schemes are described below.

呼叫应用 URI 方案Call app URI scheme

使用 ms-call: URI 方案来启动“呼叫”应用。Use the ms-call: URI scheme to launch the Call app.

URI 方案URI Scheme 结果Result
ms-call:settingsms-call:settings “呼叫”应用设置页面。Calls app settings page.

电子邮件 URI 方案Email URI scheme

使用 mailto: URI 方案来启动默认邮件应用。Use the mailto: URI scheme to launch the default mail app.

URI 方案URI Scheme 结果Results
mailto:mailto: 启动默认电子邮件应用。Launches the default email app.
mailto:[email address]mailto:[email address] 启动电子邮件应用并使用“收件人”一行上特定的电子邮件地址创建新邮件。Launches the email app and creates a new message with the specified email address on the To line. 请注意,在用户点击“发送”之前,不会发送电子邮件。Note that the email is not sent until the user taps send.

HTTP URI 方案HTTP URI scheme

使用 http: URI 方案来启动默认 Web 浏览器。Use the http: URI scheme to launch the default web browser.

URI 方案URI Scheme 结果Results
http:http: 启动默认 Web 浏览器。Launches the default web browser.

“地图”应用 URI 方案Maps app URI schemes

使用 bingmaps:ms-drive-to:ms-walk-to: URI 方案启动 Windows 地图应用,使其启动后显示特定的地图、路线和搜索结果。Use the bingmaps:, ms-drive-to:, and ms-walk-to: URI schemes to launch the Windows Maps app to specific maps, directions, and search results. 例如,以下 URI 将打开 Windows 地图应用,并显示以纽约市为中心的地图。For example, the following URI opens the Windows Maps app and displays a map centered over New York City.

bingmaps:?cp=40.726966~-74.006076

Windows 地图应用的示例。

有关详细信息,请参阅启动 Windows 地图应用For more info, see Launch the Windows Maps app. 若要在你自己的应用中使用地图控件,请参阅以 2D、3D 和街景视图方式显示地图To use the map control in your own app, see Display maps with 2D, 3D, and Streetside views.

“消息”应用 URI 方案Messaging app URI scheme

使用 ms-chat: URI 方案启动 Windows 消息应用。Use the ms-chat: URI scheme to launch the Windows Messaging app.

URI 方案URI scheme 结果Results
ms-chat:ms-chat: 启动“Messaging”应用。Launches the Messaging app.
ms-chat:?ContactID={contacted}ms-chat:?ContactID={contacted} 允许使用特定联系人的信息启动消息应用程序。Allows the messaging application to be launched with a particular contact’s information.
ms-chat:?Body={body}ms-chat:?Body={body} 允许使用要用作消息内容的字符串启动消息应用程序。Allows the messaging application to be launched with a string to use as the content of the message.
ms-chat:?Addresses={address}&Body={body}ms-chat:?Addresses={address}&Body={body} 允许使用特定地址的信息以及要用作消息内容的字符串启动消息应用程序。Allows the messaging application to be launched with a particular addresses' information, and with a string to use as the content of the message. 注意:可以串联地址。Note: Addresses can be concatenated.
ms-chat:?TransportId={transportId}ms-chat:?TransportId={transportId} 允许使用特定传输 ID 启动消息应用程序。Allows the messaging application to be launched with a particular transport ID.

音调选取器 URI 方案Tone picker URI scheme

使用 ms-tonepicker: URI 方案选择铃声、闹钟和系统音。Use the ms-tonepicker: URI scheme to choose ringtones, alarms, and system tones. 还可以保存新铃声和获取铃声的显示名称。You can also save new ringtones and get the display name of a tone.

URI 方案URI Scheme 结果Results
ms-tonepicker:ms-tonepicker: 选取铃声、闹钟和系统音。Pick ringtones, alarms, and system tones.

参数通过 LaunchURI API 的 ValueSet 传递。Parameters are passed via a ValueSet to the LaunchURI API. 有关详细信息,请参阅使用 ms-tonepicker URI 方案选择并保存音调See Choose and save tones using the ms-tonepicker URI scheme for details.

“114 查号”应用 URI 方案Nearby Numbers app URI scheme

使用 ms-yellowpage: URI 方案来启动“114 查号”应用。Use the ms-yellowpage: URI scheme to launch the Nearby Numbers app.

URI 方案URI Scheme 结果Results
ms-yellowpage:?input=[keyword]&method=[String or T9]ms-yellowpage:?input=[keyword]&method=[String or T9] 启动“114 查号”应用。Launches the Nearby Numbers app.
input 指的是想要搜索的关键字。refers to the keyword you want to search.
method 指的是搜索类型(字符串或 T9 搜索)。refers to the type of search (string or T9 search).
如果 methodT9(一种键盘),则 keyword 应该是映射到 T9 键盘字母搜索的数字字符串。If method is T9 (a type of keyboard) then keyword should be a numeric string that maps to the T9 keyboard letters to search for.
如果 methodString,则 keyword 是要搜索的关键字。If method is String then keyword is the keyword to search for.

“人脉”应用 URI 方案People app URI scheme

使用 ms-people: URI 方案来启动“人脉”应用。Use the ms-people: URI scheme to launch the People app. 有关详细信息,请参阅启动“人脉”应用For more info, see Launch the People app.

“照片”应用 URI 方案Photos app URI scheme

使用 ms-photos: URI 方案来启动“照片”应用,以查看图像或编辑视频。Use the ms-photos: URI scheme to launch the Photos app to view an image or edit a video. 例如:For example:
查看图像:To view an image: ms-photos:viewer?fileName=c:\users\userName\Pictures\image.jpg
或编辑视频:Or to edit a video: ms-photos:videoedit?InputToken=123abc&Action=Trim&StartTime=01:02:03

备注

用于编辑视频或显示图像的 URI 仅适用于桌面。The URIs to edit a video or display an image are only available on desktop.

URI 方案URI scheme 结果Results
ms-photos:viewer?fileName={filename}ms-photos:viewer?fileName={filename} 启动“照片”应用查看指定的图像,其 {filename} 为完全限定的路径名称。Launches the Photos app to view the specified image where {filename} is a fully-qualified path name. 例如:For example: c:\users\userName\Pictures\ImageToView.jpg
ms-photos:videoedit?InputToken={input token}ms-photos:videoedit?InputToken={input token} 以视频编辑模式为由文件标记表示的文件启动“照片”应用。Launches the Photos app in video editing mode for the file represented by the file token. InputToken 是必需的。InputToken is required. 使用 SharedStorageAccessManager 来获取文件的标记。Use the SharedStorageAccessManager to get a token for a file.
ms-photos:videoedit?Action={action}ms-photos:videoedit?Action={action} 一个可选参数,用于在指定视频编辑模式中打开“照片”应用,该模式的 {action} 为以下内容之一:SlowMotionFrameExtractionTrimViewInkAn optional parameter that opens the Photos app in the specified video editing mode where {action} is one of: SlowMotion, FrameExtraction, Trim, View, Ink. 如果未指定,则默认为 ViewIf not specified, defaults to View
ms-photos:videoedit?StartTime={timespan}ms-photos:videoedit?StartTime={timespan} 一个可选参数,用于指定视频开始播放的位置。An optional parameter that specifies where to start playing the video. {timespan} 必须为 "hh:mm:ss.ffff" 格式。must be in the format "hh:mm:ss.ffff". 如果未指定,则默认为If not specified, defaults to 00:00:00.0000

“设置”应用 URI 方案Settings app URI scheme

使用 ms-settings: URI 方案启动 Windows“设置”应用Use the ms-settings: URI scheme to launch the Windows Settings app. 启动为设置应用是编写隐私感知应用的重要组成部分。Launching to the Settings app is an important part of writing a privacy-aware app. 如果你的应用无法访问敏感资源,我们建议为用户提供到该资源的隐私设置的方便链接。If your app can't access a sensitive resource, we recommend providing the user a convenient link to the privacy settings for that resource. 例如,以下 URI 将打开设置应用,并显示相机隐私设置。For example, the following URI opens the Settings app and displays the camera privacy settings.

ms-settings:privacy-webcam

相机隐私设置。

有关详细信息,请参阅启动 Windows“设置”应用隐私感知应用指南For more info, see Launch the Windows Settings app and Guidelines for privacy-aware apps.

Microsoft Store 应用 URI 方案Store app URI scheme

使用 ms-windows-store: URI 方案来启动 UWP 应用Use the ms-windows-store: URI scheme to Launch the UWP app. 打开产品详细信息页面、产品查看页面和搜索页面等。例如,以下 URI 将打开 UWP 应用并启动 Microsoft Store 的主页。Open product detail pages, product review pages, and search pages, etc. For example, the following URI opens the UWP app and launches the home page of the Store.

ms-windows-store://home/

有关详细信息,请参阅启动 UWP 应用For more info, see Launch the UWP app.

天气应用 URI 方案Weather app URI scheme

使用msnweather: URI 方案来启动天气应用。Use the msnweather: URI scheme to launch the Weather app.

URI 方案URI Scheme 结果Results
msnweather://forecast?la= [latitude] 和 lo = \ [longitude]msnweather://forecast?la=[latitude]&lo=[longitude] 启动天气应用中基于位置的地理坐标的预测页面。Launches the Weather app in the Forecast page based on a location geographic coordinates.
latitude 指位置的纬度。refers to the latitude of the location.
longitude 指的经度的位置。refers to the longitude of the location.