2016-01-20 124 views
4

假設我以全屏模式打開了html5視頻(此後WebView提出了ContainsFullScreenElementChanged,現在它的確是ContainsFullScreenElement)。我怎樣才能以編程方式退出它?如何在WebView中以編程方式退出全屏模式?

我連接到SystemNavigationManager.GetForCurrentView().BackRequested並希望退出全屏模式,如果它存在,並調用WebView.GoBack(),如果它不是。

WebView沒有任何相關的方法,ApplicationView類也沒有幫助。

回答

3

好的,所以在搜索了一些之後,我找到了一個解決方案。

HTML5有fullscreen api,它可用於要求全屏或退出它。您可以使用WebView的InvokeScriptAsync方法來運行它。在我的具體情況我結束了類似下面的代碼:

string[] args = { 
    @"if (document.exitFullscreen) { 
     document.exitFullscreen(); 
     } 
     else if (document.msExitFullscreen) { 
     document.msExitFullscreen(); 
     }" 
}; 

await CurrentWebView.InvokeScriptAsync("eval", args); 

第一句話居然是什麼對我的作品,但我離開,以防萬一第二個。

哦順便說一句,如果你在BackRequested處理程序調用InvokeScriptAsync像我一樣,你可能會想你怎麼稱呼它之前設置BackRequestedEventArgs.Handled爲true ,因爲這是一個異步方法和事件將進一步傳遞到未處理的其他用戶,這可能會導致不良行爲。

編輯:似乎這個腳本不能在週年紀念更新(建立14393)。但是,如果您再添加一個帶有webkit前綴的檢查,它將起作用。像這樣的東西(或者你可以留下一個帶有webkit前綴的單一支票):

string[] args = { 
    @"if (document.exitFullscreen) { 
     document.exitFullscreen(); 
    } else if (document.msExitFullscreen) { 
     document.msExitFullscreen(); 
    } else if(document.webkitExitFullscreen) { 
     document.webkitExitFullscreen(); 
    }" 
}; 
相關問題