World War I Windows 10 (66) and World War I 66
[Download source code]
Backwater world war I Windows 10 (66)-control (WebView): monitors and processes WebView events
Author: webabcd
Introduction
WebView)
- Listen to full screen events and exit full screen events on the page
- Listen to events that do not support the uri Protocol
- Monitors events that navigate to unsupported file types
- Listener opens a uri event in a new window
- Listen to events with special permissions
Example
1. demonstrate how to listen to the full screen and exit full screen events of the content in WebView, and how to know whether the content in the current WebView is in full screen status; how to listen to events triggered by WebView when attempting to navigate to a uri of an unsupported protocol; how to listen to events triggered by WebView when attempting to navigate to a file of an unsupported type
Controls/WebViewDemo/demo5.html
<!DOCTYPE html>
Controls/WebViewDemo/demo6.html
<! DOCTYPE html>
Controls/WebViewDemo/demo7.html
<! DOCTYPE html>
Controls/WebViewDemo/WebViewDemo7.xaml
<Page x:Class="Windows10.Controls.WebViewDemo.WebViewDemo7" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Windows10.Controls.WebViewDemo" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <Grid Margin="10 0 10 10"> <WebView Name="webView1" Width="400" Height="300" Source="ms-appx-web:///Controls/WebViewDemo/demo5.html" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="5" /> <WebView Name="webView2" Width="400" Height="300" Source="ms-appx-web:///Controls/WebViewDemo/demo6.html" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="420 5 5 5" /> <WebView Name="webView3" Width="400" Height="300" Source="ms-appx-web:///Controls/WebViewDemo/demo7.html" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="5 320 5 5" /> </Grid> </Grid></Page>
Controls/WebViewDemo/WebViewDemo7.xaml. cs
/** WebView-embedded browser Controls (inherited from FrameworkElement, see/Controls/BaseControl/FrameworkElementDemo /) * ContainsFullScreenElementChanged-when the content in WebView enters full screen or exits full screen, the event * ContainsFullScreenElement-whether the content in WebView is in full screen status * UnsupportedUriSchemeIdentified-when you try to navigate to a uri trigger event * UnviewableContentIdentified-event triggered when you try to navigate to a file of a type not supported by WebView *** this example is used to demonstrate * 1. How to listen to the content in WebView to enter full screen and events that exit full screen, and how to know whether the content in the current WebView is in full screen status * 2. How to listen to the events triggered when WebView tries to navigate to the unsupported protocol uri * 3. How to listen to WebView while trying to navigate events triggered when files of unsupported types */using System; using Windows. system; using Windows. UI. popups; using Windows. UI. viewManagement; using Windows. UI. xaml. controls; namespace Windows10.Controls. webViewDemo {public sealed partial class WebViewDemo7: Page {public WebViewDemo7 () {this. initializeComponent (); webView1.ContainsFullScreenElementChanged + = WebView1_ContainsFullScreenElementChanged; audience ++ = audience; webView3.UnviewableContentIdentified + = audience ;} // private void WebView1_ContainsFullScreenElementChanged (WebView sender, object args) {ApplicationView applicationView = ApplicationView triggered when the content in WebView enters full screen or exits full screen. getForCurrentView (); // the content in the WebView is in the full screen if (sender. containsFullScreenElement) {// set the app to full screen mode applicationView. tryEnterFullScreenMode ();} else {// exit app full screen mode applicationView. exitFullScreenMode () ;}// event private async void WebView2_UnsupportedUriSchemeIdentified (WebView sender, javasargs) triggered when you try to navigate to a uri of a protocol not supported by WebView) {// handle it by myself (otherwise, a dialog box will pop up to jump to other apps that support this Protocol or search for apps that support this protocol in the store) args. handled = true; // uri Uri myUri = args. uri; await new MessageDialog (myUri. toString (), "Custom uri "). showAsync () ;}// private async void WebView3_UnviewableContentIdentified (WebView sender, javasargs) event triggered when you try to navigate to a file of a type not supported by WebView {// file type, in this example, the value is "application/pdf" string mediaType = args. mediaType; // uri to which you try to navigate (in this example, this value is https://www.apple.com/cn/iphone/business/docs/iOS_Security_Guide.pdf ) Uri uri = args. Uri; // referrer of the uri (in this example, this value is https://www.apple.com/cn/iphone/business/docs/iOS_Security_Guide.pdf It is not the referrer of uri. Why ?) Uri referrer = args. referrer; if (args. uri. absolutePath. endsWith (". pdf ") {// open the pdf file if (await launcher. launchiliasync (args. uri) {} else {}}}}}
2. demonstrate how to listen to events triggered when WebView tries to open a uri in a new window; how to listen to events triggered when WebView tries to obtain special permissions, such as geographical location
Controls/WebViewDemo/demo8.html
<! DOCTYPE html>
Controls/WebViewDemo/demo9.html
<!DOCTYPE html>
Controls/WebViewDemo/WebViewDemo8.xaml
<Page x:Class="Windows10.Controls.WebViewDemo.WebViewDemo8" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Windows10.Controls.WebViewDemo" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <Grid Margin="10 0 10 10"> <WebView Name="webView1" Width="400" Height="300" Source="ms-appx-web:///Controls/WebViewDemo/demo8.html" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="5" /> <WebView Name="webView2" Width="400" Height="300" Source="ms-appx-web:///Controls/WebViewDemo/demo9.html" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="420 5 5 5" /> </Grid> </Grid></Page>
Controls/WebViewDemo/WebViewDemo8.xaml. cs
/** WebView-embedded browser Controls (inherited from FrameworkElement, see/Controls/BaseControl/FrameworkElementDemo /) * NewWindowRequested-events triggered when you try to open a uri in a new window * PermissionRequested-events triggered when you try to obtain special permissions, for example, geographical location **. This example is used to demonstrate * 1. How to listen to events triggered by WebView when opening a uri in a new window * 2. How to listen to WebView when trying to obtain special permissions event triggered, for example, geographic location */using System; using Windows. UI. popups; using Windows. UI. xaml. controls; namespace Windows10.Controls. webViewDemo {public sealed partial class WebViewDemo8: Page {public WebViewDemo8 () {this. initializeComponent (); webView1.NewWindowRequested + = webviewmedianewwindowrequested; enabled + = enabled;} // private async void webview=newwindowrequested (WebView sender, javasargs) {// handle it by me (otherwise the system will open it in a browser. handled = true; // uri of the window to be opened (in this example, this value is https://www.baidu.com/ ) Uri uri = args. Uri; // referrer of the uri (in this example, this value is https://www.baidu.com/ It is not the referrer of uri. Why ?) Uri referrer = args. referrer; await new MessageDialog (uri. toString (), "uri to be opened "). showAsync () ;}// events triggered when a special permission is obtained, such as private void WebView2_PermissionRequested (WebView sender, WebViewPermissionRequestedEventArgs args) {/** WebViewPermissionRequest-special permission request object * PermissionType-Special Permission type * WebViewPermissionState-Status of Special Permission requests (Unknown, Defer, Allow, Deny) * Uri-uri * Allow () for the request with special permissions-grant the request permission * Deny ()-reject the request permission * Defer () -The latency determines whether to grant */WebViewPermissionRequest permissionRequest = args. permissionRequest ;}}}
OK
[Download source code]