C# 如何在WebView中以编程方式退出全屏模式?
假设我在全屏模式下打开了html5视频(之后WebView提出了C# 如何在WebView中以编程方式退出全屏模式?,c#,webview,uwp,C#,Webview,Uwp,假设我在全屏模式下打开了html5视频(之后WebView提出了ContainsFullScreenElement更改了,现在它的ContainsFullScreenElement是真的)。如何以编程方式退出它 我正在连接到SystemNavigationManager.GetForCurrentView().BackRequested,如果存在,则希望退出全屏模式,如果没有,则调用WebView.GoBack() WebView没有任何相关的方法,ApplicationView类也没有帮助。好
ContainsFullScreenElement更改了,现在它的ContainsFullScreenElement
是真的)。如何以编程方式退出它
我正在连接到SystemNavigationManager.GetForCurrentView().BackRequested
,如果存在,则希望退出全屏模式,如果没有,则调用WebView.GoBack()
WebView没有任何相关的方法,ApplicationView类也没有帮助。好的,所以在搜索了更多的方法后,我找到了一个解决方案
HTML5具有,可用于要求全屏或退出。您可以使用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,因为这是一个异步方法,事件将进一步传递给其他订阅者而不进行处理,这可能会导致不良行为
编辑:此脚本在周年更新(build 14393)中似乎不起作用。但是,如果您再添加一个带有webkit前缀的检查,它将起作用。类似这样的内容(或者您可以只留下一张带有webkit前缀的支票):
string[] args = {
@"if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.msExitFullscreen) {
document.msExitFullscreen();
} else if(document.webkitExitFullscreen) {
document.webkitExitFullscreen();
}"
};