啟動 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 配置可讓您按一下超連結來開啟 App。URI schemes let you open apps by clicking hyperlinks. 就像您可以使用 mailto: 來建立新電子郵件一樣,您可以使用 http: 來開啟預設的網頁瀏覽器。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-chap 至:和 ms 逐步解說: bingmaps:, ms-drive-to:, and ms-walk-to: 地圖 AppMaps app
HTTPhttp: 預設網頁瀏覽器Default web browser
mailtomailto: 預設電子郵件 AppDefault email app
ms-call:ms-call: 呼叫 AppCall app
ms-chat:ms-chat: 訊息中心 AppMessaging app
ms-people:ms-people: 連絡人應用程式People app
ms-photos:ms-photos: 相片 AppPhotos app
ms 設定:ms-settings: 設定 AppSettings app
ms-store:ms-store: 市集應用程式Store app
ms-tonepicker:ms-tonepicker: 音調選擇器Tone picker
ms-yellowpage:ms-yellowpage: 附近號碼 AppNearby Numbers app
msnweather:msnweather: 氣象應用程式Weather app

例如,下列 URI 會開啟預設瀏覽器,並顯示 Bing 網站。For example, the following URI opens the default browser and displays the Bing web site.

https://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. 使用者決定要啟動哪個 app。The user determines which app is launched. 您可以登錄多個 app 來處理相同的 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. 如果有多個 app 可能已登錄相同的 URI 配置,您的 app 可能會建議啟動特定 app。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. 呼叫此方法時,您的 app 必須是前景 app,也就是說,使用者必須看得到您的 app。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,但您的 app 不在前景,則啟動將會失敗,並會叫用您的錯誤回呼。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.

警告對話方塊會顯示在 app 變暗的背景上。

如果您一律要顯示此提示,請使用 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 的 App,則推薦一個 AppRecommend an app if one is not available to handle the URI

在某些情況下,使用者可能尚未安裝可處理您要啟動之 URI 的 App。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. 如果您想提供使用者在此情況下應取得何種 app 的特定建議,可以在啟動 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.

在多個 app 已登錄要處理 URI 配置時,建議十分有用。Recommendations are also useful when more than one app has registered to handle a URI scheme. 在建議特定 app 時,如果該 app 已安裝,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. 作業系統會使用此資訊,並搭配從市集取得建議 app 的特定選項,以取代在市集中搜尋 app 的一般選項。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 的來源 app 可要求在 URI 啟動後停留在畫面上。Source apps that call LaunchUriAsync can request that they remain on screen after a URI launch. 根據預設,Windows 會嘗試將所有可用空間平均分享給來源 app 與用來處理 URI 的目標 app。By default, Windows attempts to share all available space equally between the source app and the target app that handles the URI. 來源 app 可以使用 DesiredRemainingView 屬性,告知作業系統要讓 app 視窗佔用較多或較少可用空間。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,指示來源 app 在 URI 啟動後不需要停留在畫面上,且可由目標 app 完全取代。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. 這個屬性只會指定發出呼叫的 app 的慣用視窗大小。This property only specifies the preferred window size of the calling app. 它不會指定其他可能也同時在螢幕上之 app 的行為。It doesn't specify the behavior of other apps that may happen to also be on screen at the same time.

注意   當 Windows 判斷來源應用程式的最終視窗大小時,Windows 會考慮許多不同的因素,例如,來源應用程式的喜好設定、畫面上的應用程式數目、螢幕方向等等。Note  Windows 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.

撥號 App 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 呼叫 App 設定頁面。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: 啟動預設電子郵件 app。Launches the default email app.
mailto: [ 電子郵件地址]mailto:[email address] 啟動電子郵件 app,並以指定於 [收件者] 行上的電子郵件地址建立新訊息。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 配置啟動預設網頁瀏覽器。Use the http: URI scheme to launch the default web browser.

URI 配置URI Scheme 結果Results
http:http: 啟動預設網頁瀏覽器。Launches the default web browser.

地圖 App 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 地圖 app,並顯示以紐約市為中心的地圖。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. 若要在您自己的 App 中使用地圖控制項,請參閱顯示 2D、3D 和 Streetside 檢視的地圖To use the map control in your own app, see Display maps with 2D, 3D, and Streetside views.

訊息中心 App 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: 啟動訊息中心應用程式。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} 允許以特定的傳輸識別碼啟動訊息中心應用程式。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.

傳遞參數時會透過 ValueSet 傳遞給 LaunchURI API。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.

附近號碼應用程式 URI 配置Nearby Numbers app URI scheme

使用 ms-yellowpage: URI 配置啟動附近號碼應用程式。Use the ms-yellowpage: URI scheme to launch the Nearby Numbers app.

URI 配置URI Scheme 結果Results
yellowpage:? input = [ 關鍵字 ]&方法 = [ String 或 T9]ms-yellowpage:?input=[keyword]&method=[String or T9] 啟動「附近號碼 App」。Launches the Nearby Numbers app.
input 是指您想要搜尋的關鍵字。input refers to the keyword you want to search.
method 指的是搜尋 (字串或 T9 搜尋) 的類型。method 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:
若要查看影像: ms-photos:viewer?fileName=c:\users\userName\Pictures\image.jpgTo view an image: ms-photos:viewer?fileName=c:\users\userName\Pictures\image.jpg
或編輯影片: ms-photos:videoedit?InputToken=123abc&Action=Trim&StartTime=01:02:03Or 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. 例如: c:\users\userName\Pictures\ImageToView.jpgFor 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} 是下列其中一個: SlowMotionFrameExtractionTrimView筆墨A parameter that indicates which video editing mode to open the Photos app in, where {action} is one of: SlowMotion, FrameExtraction, Trim, View, Ink. 需要採取動作Action is required.
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"{timespan} must be in the format "hh:mm:ss.ffff". 如果未指定,則預設為 00:00:00.0000If 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. 啟動設定 app 是撰寫隱私權感知 app 的重要部分。Launching to the Settings app is an important part of writing a privacy-aware app. 如果您的 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 會開啟設定 app,並顯示相機隱私權設定。For example, the following URI opens the Settings app and displays the camera privacy settings.

ms-settings:privacy-webcam

相機隱私權設定。

如需詳細資訊,請參閱啟動 Windows 設定 app隱私權感知 app 的指導方針For more info, see Launch the Windows Settings app and Guidelines for privacy-aware apps.

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 = [ 緯度 ]&lo = [ 經度]msnweather://forecast?la=[latitude]&lo=[longitude] 根據位置地理座標,在 [預測] 頁面中啟動氣象應用程式。Launches the Weather app in the Forecast page based on a location geographic coordinates.
latitude 指的是位置的緯度。latitude refers to the latitude of the location.
longitude 指的是位置的經度。longitude refers to the longitude of the location.