如何接收連結 (HTML)
[ 本文的目標對象是撰寫 Windows 執行階段 App 的 Windows 8.x 和 Windows Phone 8.x 開發人員。如果您正在開發適用於 Windows 10 的 App,請參閱 最新文件 ]
連結是使用者想要分享的一種常見資料格式。有些時候使用者會直接分享連結 (如網站上的文章)。當使用者分享 HTML 或線上內容時,支援連結會是另一個實用的選項。
這個主題示範如何接受來源應用程式分享的單一連結。
您必須知道的事
技術
先決條件
- 您應該熟悉 Visual Studio 以及相關範本。
- 您應該熟悉 JavaScript。
指示
步驟 1: 支援分享協定
您的應用程式可以接收分享的內容之前,必須先宣告它支援分享協定。這個協定基本上會讓系統知道您的應用程式能夠接收內容。如果您使用 Microsoft Visual Studio 範本建立應用程式,以下是支援分享協定的方法:
- 在設計工具檢視中開啟資訊清單檔案 (package.manifest)。
- 開啟 [宣告] 索引標籤。
- 從 [可用宣告]**** 清單中選擇 [共用目標]。
- 按一下 [加入]****,在應用程式新增共用目標合約的支援。
步驟 2: 指定應用程式支援連結
如果要支援連結,您必須指定應用程式支援 URI 格式:
- 開啟資訊清單檔案。
- 在 [資料格式] 區段中,按一下 [加入新的]****。
- 輸入 "text" (不要加上引號)。
前面的步驟會將下段文字新增到資訊清單中:
<Extensions>
<Extension Category="windows.shareTarget">
<ShareTarget>
<DataFormat>uri</DataFormat>
</ShareTarget>
</Extension>
</Extensions>
注意 當您的應用程式依據共用目標合約啟用時,您可以指定不同的進入點。若要這樣做,請修改套件資訊清單中 [共用目標] 宣告中 [應用程式]**** 設定區段中 [起始頁] 項目。也強烈建議您在該頁面使用專門處理啟用的另一個 JavaScript 檔案。如需範例,請查看分享內容目標應用程式範例。
步驟 3: 新增事件處理常式來偵測應用程式何時啟用。
當使用者選取您的應用程式來分享內容時,系統會啟用您的應用程式。因為啟用的方法很多,所以您需要新增程式碼以偵測發生啟用的原因。您可以檢查 kind 屬性的值來這樣做。
app.onactivated = function (args) {
if (args.detail.kind === activation.ActivationKind.launch) {
// The application has been launched. Initialize as appropriate.
} else if (args.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.shareTarget) {
...
}
};
如果您為共用目標合約使用專用的起始頁,可以略過檢查 kind 屬性
步驟 4: 取得 ShareOperation 物件。
ShareOperation 物件包含應用程式所需的全部資料,讓應用程式取得使用者想分享的內容。
shareOperation = args.detail.shareOperation;
步驟 5: 快速地從啟用的事件處理常式傳回。
activated 事件處理常式必須快速傳回。將來自於 activated 事件處理常式的非同步事件排入佇列,這樣啟用的事件傳回之後就會開始處理分享資料。
WinJS.Application.addEventListener("shareready", shareReady, false);
WinJS.Application.queueEvent({ type: "shareready" });
剩餘的步驟是實作 shareReady
函式。
步驟 6: 檢查 DataPackageView 是否包含 URI。
ShareOperation 物件包含一個 DataPackageView 物件。這個物件是 DataPackage 物件 (來源應用程式用來建立資料) 的唯讀版本。使用此物件來查看要分享的內容是否包含連結。
if (shareOperation.data.contains(Windows.ApplicationModel.DataTransfer.StandardDataFormats.uri)) {
// Code to process URI goes here.
}
檢查 DataPackage 是否包含您有興趣的資料格式是很好的做法,即使您的應用程式僅支援一種格式。這樣會讓應用程式以後可以輕鬆地支援其他資料格式類型及檔案格式。
步驟 7: 處理連結。
若要取得 URI,請呼叫 DataPackageView.getUriAsync 方法。
shareOperation.data.getUriAsync().then(function (uri) {
if (uri != null) {
// In this sample, we only display the URI. To output the link using this example,
// you need a div tag with an id of "output" in your HTML file.
// In your app, replace this with whatever is appropriate for your scenario.
document.getElementById("output").innerText = uri.absoluteUri;
}
});
步驟 8: 呼叫 reportCompleted。
在您的應用程式成功完成分享內容後,請呼叫 reportCompleted。呼叫這個方法之後,系統會關閉您的應用程式。
shareOperation.reportCompleted();
備註
有些應用程式可能會提供不是以 http:// 或 https:// 開頭的統一資源識別項 (URI)。例如,來源應用程式可以提供一個啟用通訊協定,將使用者直接帶到應用程式本身的內容。如果您的應用程式已經支援所有 URI,預設就已包含這項功能。如果您的應用程式只限於特定 URI 類型,則應該考慮下列幾項:
- 如果您支援 HTML,請改用 DataPackage 中提供的 HTML。
- 如果您不支援 HTML 但支援文字,請使用 DataPackage 中的文字。
- 如果上述的選項都無法成功,請考慮顯示一個訊息,告訴使用者您不支援特定 URI。
在所有的情況下,試著將資料的標題和描述整合到使用經驗中。這可以讓使用者更了解分享的內容。
請查看我們的分享內容目標應用程式範例中的程式碼範例,了解應用程式接收分享之影像的整個端對端經驗。
完整範例
var shareOperation = null;
function shareReady(args) {
if (shareOperation.data.contains(Windows.ApplicationModel.DataTransfer.StandardDataFormats.uri)) {
shareOperation.data.getUriAsync().done(function (uri) {
// In this sample, we only display the URI. To output the link using this example,
// you need a div tag with an id of "output" in your HTML file.
// In your app, replace this with whatever is appropriate for your scenario.
document.getElementById("output").innerText = "Uri: " + uri.absoluteUri;
});
}
}
app.onactivated = function (args) {
if (args.detail.kind === activation.ActivationKind.launch) {
// The app was launced. Initialize as appropriate.
args.setPromise(WinJS.UI.processAll());
} else if (args.detail.kind === Windows.ApplicationModel.Activation.ActivationKind.shareTarget) {
// This app was been activated for the Share contract
args.setPromise(WinJS.UI.processAll());
// We receive the ShareOperation object as part of the eventArgs
shareOperation = args.detail.shareOperation;
// We queue an asychronous event so that working with the ShareOperation
// object does not block or delay the return of the activation handler.
WinJS.Application.addEventListener("shareready", shareReady, false);
WinJS.Application.queueEvent({ type: "shareready" });
}
};
相關主題
Windows.ApplicationModel.DataTransfer