【Win10 UWP】URI Scheme(一):Windows Store協議的解析和使用

來源:互聯網
上載者:User

標籤:

協議是Windows Phone和Windows Store應用的一個重要特點,可以做到在不同應用之間進行互相呼起調用。小小協議,學問大著呢。我打算寫幾篇關於協議在UWP中使用的文章。

這一講的主要對象是Win10系統內建的市集應用所支援的相關啟動協議ms-windows-store。

 

一. ms-windows-store協議的調用方式

ms-windows-store協議支援在Web或者UWP應用內調用,都能直接將系統內建的市集啟動起來。

1.通過Web啟動市集

大家可能注意到,平時我們在瀏覽器開啟一個Win10應用的url連結時,系統內建的市集會被調起來,這裡面的玄機就是使用了ms-windows-store協議!

比如下面這個UWP應用:https://www.microsoft.com/store/apps/9nblggh5x991

(順便安利一下我最新的UWP應用——禮物說 - 最全美物收藏地)

用F12大法查看這個頁面的源碼,會發現以下這段代碼

 1 <iframe id="cli_redirectFrame" class="hidden"></iframe> 2 <script> 3     oneStoreRedirect(oneStoreUserAgent(), document) 4        .autoRedirectToNativeStorePdp("cli_redirectFrame", { 5              "threshold":"ms-windows-store://pdp/?productid=9NBLGGH5X991\u0026referrer=unistoreweb", 6              "win8x":"ms-windows-store:PDP?PFN=39014AppChampionStudio.-_ttcpf2hpawt8p\u0026referrer=unistoreweb", 7              "phone81":"ms-windows-store:navigate?appid=ddc54195-22fd-43f3-ab67-964ca250bd6c\u0026referrer=unistoreweb", 8              "phone807x":"zune://navigate/?appid=ddc54195-22fd-43f3-ab67-964ca250bd6c\u0026referrer=unistoreweb" 9         }, ‘9NBLGGH5X991‘, false, ‘禮物說 - 最全美物收藏地‘, true, true, false, false);10 </script>    

在頁面載入的時候會自動執行這段代碼,作用就是自動去調用ms-windows-store協議,嘗試把系統的市集App啟動起來,並導航到具體的應用頁面:

在Web中使用協議的方法非常簡單,只需把ms-windows-store協議的Url類比成一個http協議來請求就可以了。

2.在UWP應用中啟動市集

在UWP應用裡,也可以將系統的市集啟動起來。事實上,任何協議都可以試著使用如下API來調用:

1 await Windows.System.Launcher.LaunchUriAsync(new Uri(InputUri));

 

事實上“ms-windows-store://”的作用相當於"http://",ms-windows-store就是一個自訂的URI Scheme,緊跟著的參數規則嚴格按照Uri格式進行。

當然UWP應用(實際上市集也是一個UWP應用)可以在Package.appxmanifest的“聲明”中,聲明這個UWP支援某個自訂的協議(比如叫做myscheme),那麼只要在任何地方(Web、UWP App、iOS App、安卓App,甚至自己這個UWP)調用(myscheme://,注意帶有“//”),這個UWP都可以被啟動起來。從UWP的生命週期角度講,不應該叫“啟動(Launch)”起來,而應該是被“啟用(Activated)”起來。App.cs可以通過OnActivated方法接收到自訂協議裡帶著的Uri參數,從而自行進行解析並作頁面導航處理。這一部分內容我們留到下一篇具體展開。

 

二. ms-windows-store協議的高端用法

我們這講的主題是市集的協議,前面講了很多光是啟動商店進入一個應用的詳情遠遠不能滿足我們平時的開發需求,你可能還需要求好評功能。

MSDN文檔對ms-windows-store作了非常詳細的介紹,連結:https://msdn.microsoft.com/en-us/library/windows/apps/mt228343.aspx

描述 重要參數 URI Scheme
啟動到商店首頁  

ms-windows-store://home

ms-windows-store://                 <--- 如果在UWP應用內可以唯寫這個

啟動到商店的分類

(並非所有使用者都能看到所有類別,目前類別有應用、遊戲、音樂、視頻)

路徑navigatetopage

 

ms-windows-store://navigatetopage/?Id=Apps

ms-windows-store://navigatetopage/?Id=Games

ms-windows-store://navigatetopage/?Id=Music

ms-windows-store://navigatetopage/?Id=Video

啟動到某個應用詳情

路徑pdp (product detail page)

ProductId,ProductId,ProductId

(重要的參數說三遍,UWP最推薦的方式)

ms-windows-store://pdp/?ProductId=9NBLGGH5X991
PFN(Pacakge Family Name) ms-windows-store://pdp/?PFN= Microsoft.Office.OneNote_8wekyb3d8bbwe
PhoneAppId (Windows Phone 7.x/8.x) ms-windows-store://pdp/?PhoneAppId=ca05b3ab-f157-450c-8c49-a1f127f5e71d
AppId (Windows 8.x) ms-windows-store://pdp/?AppId=f022389f-f3a6-417e-ad23-704fbdf57117

啟動到某個應用編寫評論

路徑review、reviewapp(Windows Phone 7.x/8.x)

ProductId,ProductId,ProductId

(重要的參數說三遍,UWP最推薦的方式)

ms-windows-store://review/?ProductId=9NBLGGH5X991
PFN ms-windows-store://review/?PFN= Microsoft.Office.OneNote_8wekyb3d8bbwe
AppId (Windows Phone 7.x/8.x) ms-windows-store://reviewapp/?AppId=ca05b3ab-f157-450c-8c49-a1f127f5e71d
AppId (Windows 8.x) ms-windows-store://review/?AppId=f022389f-f3a6-417e-ad23-704fbdf57117

啟動關聯搜尋

路徑assoc

FileExt(與副檔名關聯的產品) ms-windows-store://assoc/?FileExt=pdf

 

 

Protocol (與協議關聯的產品) ms-windows-store://assoc/?Protocol=liwushuo

 

 

Tags (與標記關聯的產品) ms-windows-store://assoc/?Tags=Photos_Rich_Media_Edit, Camera_Capture_App

啟動搜尋

路徑search

  ms-windows-store://search/?query=OneNote

啟動類別中的產品搜尋

路徑browse

 

ms-windows-store://browse/?type=Apps&cat=Productivity

ms-windows-store://browse/?type=Apps&cat=Health+%26+fitness

啟動某發行者發布的產品搜尋(名稱中允許空格)

路徑publisher

 

ms-windows-store://publisher/?name=AppChampion Studio

啟動下載和更新頁面

 

ms-windows-store://downloadsandupdates

啟動商店設定頁面

 

ms-windows-store://settings

 以上ms-windows-store協議的規則適用於Win10 UWP應用,部分規則WP或Win8.x並不適用。

 

小結

本講的主題是Win10內建的市集相關協議的規則,並介紹了URI Scheme的基本原理。下一講我們講UWP裡使用URI Scheme的具體用法,解析響應以及適用場合。

 

【Win10 UWP】URI Scheme(一):Windows Store協議的解析和使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.