Universal App - 使用 Launcher 執行 URI 或 File Type
在 WP 8.0 我寫了二篇介紹了 App Association 的做法:
<Windows Phone 8 - App2App Communication - Protocol (URI Schema)>與<Windows Phone 8 - App2App Communication - File associations>。
該篇將會接續<Universal App - 處理 URI activation>與<>之後說明如何在程式裡使用 Launcher 來執行 URI 或 File Type 來開啟負責處理的 App。
使用「Windows.System.Launcher API」執行特定 file type 或 protocol 對應的預設 App,以下先說明如何操作它:
專用於啟動指特定 file type 或 protocol 的預設 App。方法如下:
Method | Description |
LaunchFileAsync(IStorageFile) | Starts the default app associated with the specified file. |
LaunchFileAsync(IStorageFile, LauncherOptions) | Starts the default app associated with the specified file, using the specified options. |
LaunchUriAsync(Uri) | Starts the default app associated with the URI scheme name for the specified URI. |
LaunchUriAsync(Uri, LauncherOptions) | Starts the default app associated with the URI scheme name for the specified URI, using the specified options. |
指定特定的選項來執行預設 app。可設定的項目如下:
類型 | 名稱 | 說明 | |
Constructor | LauncherOptions | Creates and initializes a new instance of the launcher options object. | |
Property | ContentType | Read/write,Gets or sets the content type that is associated with a URI that represents a file on the network. | |
DesiredRemainingView | Read/write,Launch a target app and have the currently running source app remain on the screen by sharing the space equally with the target app or by taking up more or less space than the target app. | ||
DisplayApplicationPicker | Read/write,Gets or sets a value that indicates whether to display the Open With dialog whenever the association launching API is called. 設定後,請求時會出現 dialog 提供用戶選擇執行的 App。 URI scheme 範例圖: File type 範例圖: | ||
FallbackUri | Read/write,Gets or sets a value that represents a URI that the user should be taken to in the browser if no app exists to handle the file type or URI. | ||
PreferredApplicationDisplayName | Read/write,Gets or sets a value that represents the display name of the app in the store that the user should install if no app exists to handle the file type or URI. 設定 PreferredApplicationPackageFamilyName 讓 App 可以建議用戶去安裝或下載特定的 App 來處理 URI 或是 File。(預設,指定 Launcher 的 URI 或 File 時,系統會藉由其類型建議用戶安裝或搜尋適用的 App) display name:設定提示時顯示 App 的 name; package family name:設定顯示 App 對應的 package family name; 例如:
|
||
PreferredApplicationPackageFamilyName | Read/write,Gets or sets a value that represents the package family name of the app in the Store that the user should install if no app exists to handle the file type or URI.
設定建議如果沒有對應處理的 App 時,用戶應該要安裝的 app (package family name)。 |
||
TreatAsUntrusted | Read/write,Gets or sets a value that indicates whether the system should display a warning that the file or URI is potentially unsafe when starting the app associated with a file or URI.
用於在請求 URI 或 File launch 時要出現 警告 來通知用戶。 |
||
UI | Read-only,Gets the user interface (UI) options when starting a default app. |
範例-1:Launch a file contained in the app package:
async void DefaultLaunch()
{
// Path to the file in the app package to launch
string imageFile = @"images\test.png";
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(imageFile);
if (file != null)
{
// Launch the retrieved file
var success = await Windows.System.Launcher.LaunchFileAsync(file);
if (success)
{
// File launched
}
else
{
// File launch failed
}
}
else
{
// Could not find file
}
}
範例-2:Launch a URI:
async void DefaultLaunch()
{
// Launch the URI
var success = await Windows.System.Launcher.LaunchUriAsync(uri);
if (success)
{
// URI launched
}
else
{
// URI launch failed
}
}
執行 Launcher 的 LaunchUriAsync 或 LaunchFileAsync 均會有 boolean 回傳值,要記得判斷在 false 的時候,代表執行 URI scheme 或 File type 失敗,
App 要自定處理來回應用戶,不然會沒有任務反應讓用戶會迷惑。
======
該篇僅為了補充在使用 Launcher 時要注意與細部的設定參數,因此之前自己在使用時不太知道有那些特別的,
所以特別整理一下,希望對大家有所幫助。
References:
〉Auto-launching with file and URI associations (XAML)
〉How to handle file activation (C#/VB/C++)
〉How to handle protocol activation (C#/VB/C++)
〉How to launch the default app for a URI (XAML)
〉How to launch the default app for a file (C#/VB/C++)
〉How to handle URI activation (XAML)
〉Guidelines for file types and URIs
〉Store Apps 自訂 URI 與 File Association (重要)
〉Building Apps for Windows Phone 8.1
〉Auto-launching apps using file and URI associations for Windows Phone 8