[源码下载]

背水一战 Windows 10 (66) - 控件(WebView): 监听和处理 WebView 的事件

作者:webabcd

介绍
背水一战 Windows 10 之 控件(WebView)

  • 监听页面的进入全屏事件和退出全屏事件
  • 监听导航至不支持 uri 协议的事件
  • 监听导航至不支持类型的文件的事件
  • 监听用新窗口打开 uri 的事件
  • 监听获取特殊权限的事件

示例
1、演示如何监听 WebView 中的内容的进入全屏和退出全屏的事件,以及如何获知当前 WebView 中的内容是否处于全屏状态;如何监听 WebView 在尝试导航至不支持的协议的 uri 时触发的事件;如何监听 WebView 在尝试导航至不支持的类型的文件时触发的事件
Controls/WebViewDemo/demo5.html

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<b>i am demo5.html</b>
<p>
<video controls="controls" autoplay="autoplay">
<source src="http://media.w3.org/2010/05/sintel/trailer.mp4" type="video/mp4" />
</video>
</p>
</body>
</html>

Controls/WebViewDemo/demo6.html

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<b>i am demo6.html</b>
<p>
<a href="mycustom://abc">自定义 Uri 协议</a>
</p>
</body>
</html>

Controls/WebViewDemo/demo7.html

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<b>i am demo7.html</b>
<p>
<a href="https://www.apple.com/cn/iphone/business/docs/iOS_Security_Guide.pdf">打开 pdf 文件</a>
</p>
</body>
</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 - 内嵌浏览器控件(继承自 FrameworkElement, 请参见 /Controls/BaseControl/FrameworkElementDemo/)
* ContainsFullScreenElementChanged - WebView 中的内容进入全屏或退出全屏时触发的事件
* ContainsFullScreenElement - WebView 中的内容是否处于全屏状态
* UnsupportedUriSchemeIdentified - 在尝试导航至 WebView 不支持的协议的 uri 时触发的事件
* UnviewableContentIdentified - 在尝试导航至 WebView 不支持的类型的文件时触发的事件
*
*
* 本例用于演示
* 1、如何监听 WebView 中的内容的进入全屏和退出全屏的事件,以及如何获知当前 WebView 中的内容是否处于全屏状态
* 2、如何监听 WebView 在尝试导航至不支持的协议的 uri 时触发的事件
* 3、如何监听 WebView 在尝试导航至不支持的类型的文件时触发的事件
*/ 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;
webView2.UnsupportedUriSchemeIdentified += WebView2_UnsupportedUriSchemeIdentified;
webView3.UnviewableContentIdentified += WebView3_UnviewableContentIdentified;
} // WebView 中的内容进入全屏或退出全屏时触发的事件
private void WebView1_ContainsFullScreenElementChanged(WebView sender, object args)
{
ApplicationView applicationView = ApplicationView.GetForCurrentView(); // WebView 中的内容处于全屏状体
if (sender.ContainsFullScreenElement)
{
// 将 app 设置为全屏模式
applicationView.TryEnterFullScreenMode();
}
else
{
// 将 app 退出全屏模式
applicationView.ExitFullScreenMode();
}
} // 在尝试导航至 WebView 不支持的协议的 uri 时触发的事件
private async void WebView2_UnsupportedUriSchemeIdentified(WebView sender, WebViewUnsupportedUriSchemeIdentifiedEventArgs args)
{
// 交由我处理吧(否则的话系统会弹出对话框,以便跳转至支持此协议的其他 app 或者在商店搜索支持此协议的 app)
args.Handled = true; // 尝试导航至的 uri
Uri myUri = args.Uri;
await new MessageDialog(myUri.ToString(), "自定义 uri").ShowAsync();
} // 在尝试导航至 WebView 不支持的类型的文件时触发的事件
private async void WebView3_UnviewableContentIdentified(WebView sender, WebViewUnviewableContentIdentifiedEventArgs args)
{
// 文件类型,本例中此值为 "application/pdf"
string mediaType = args.MediaType; // 尝试导航至的 uri(本例中此值为 https://www.apple.com/cn/iphone/business/docs/iOS_Security_Guide.pdf)
Uri uri = args.Uri; // uri 的 referrer(本例中此值为 https://www.apple.com/cn/iphone/business/docs/iOS_Security_Guide.pdf 并不是 uri 的 referrer,为啥?)
Uri referrer = args.Referrer; if (args.Uri.AbsolutePath.EndsWith(".pdf"))
{
// 通过 launcher 打开 pdf 文件
if (await Launcher.LaunchUriAsync(args.Uri))
{ }
else
{ }
}
}
}
}

2、演示如何监听 WebView 在尝试用新开窗口打开 uri 时触发的事件;如何监听 WebView 在尝试获取特殊权限时触发的事件,比如地理位置等
Controls/WebViewDemo/demo8.html

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<b>i am demo8.html</b>
<p>
<a href="https://www.baidu.com/" target="_blank">新开窗口打开 https://www.baidu.com/</a>
</p>
</body>
</html>

Controls/WebViewDemo/demo9.html

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<b>i am demo9.html</b> <script type="text/javascript"> if (navigator.geolocation)
{
navigator.geolocation.getCurrentPosition
(
function (p)
{ },
function (e)
{ }
);
} </script>
</body>
</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 - 内嵌浏览器控件(继承自 FrameworkElement, 请参见 /Controls/BaseControl/FrameworkElementDemo/)
* NewWindowRequested - 在尝试用新开窗口打开 uri 时触发的事件
* PermissionRequested - 在尝试获取特殊权限时触发的事件,比如地理位置等
*
*
* 本例用于演示
* 1、如何监听 WebView 在尝试用新开窗口打开 uri 时触发的事件
* 2、如何监听 WebView 在尝试获取特殊权限时触发的事件,比如地理位置等
*/ 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 += WebView1_NewWindowRequested;
webView2.PermissionRequested += WebView2_PermissionRequested;
} // 在尝试用新开窗口打开 uri 时触发的事件
private async void WebView1_NewWindowRequested(WebView sender, WebViewNewWindowRequestedEventArgs args)
{
// 交由我处理吧(否则的话系统会用浏览器打开)
args.Handled = true; // 需要新开窗口的 uri(本例中此值为 https://www.baidu.com/)
Uri uri = args.Uri; // uri 的 referrer(本例中此值为 https://www.baidu.com/ 并不是 uri 的 referrer,为啥?)
Uri referrer = args.Referrer; await new MessageDialog(uri.ToString(), "需要新开窗口的 uri").ShowAsync();
} // 在尝试获取特殊权限时触发的事件,比如地理位置等
private void WebView2_PermissionRequested(WebView sender, WebViewPermissionRequestedEventArgs args)
{
/*
* WebViewPermissionRequest - 特殊权限请求对象
* PermissionType - 特殊权限类型
* WebViewPermissionState - 特殊权限请求的状态(Unknown, Defer, Allow, Deny)
* Uri - 请求特殊权限的 uri
* Allow() - 授予请求的权限
* Deny() - 拒绝请求的权限
* Defer() - 延迟决定是否授予
*/
WebViewPermissionRequest permissionRequest = args.PermissionRequest;
}
}
}

OK
[源码下载]

背水一战 Windows 10 (66) - 控件(WebView): 监听和处理 WebView 的事件的更多相关文章

  1. 背水一战 Windows 10 (65) - 控件(WebView): 对 WebView 中的内容截图, 通过 Share Contract 分享 WebView 中的被选中的内容

    [源码下载] 背水一战 Windows 10 (65) - 控件(WebView): 对 WebView 中的内容截图, 通过 Share Contract 分享 WebView 中的被选中的内容 作 ...

  2. 背水一战 Windows 10 (64) - 控件(WebView): 加载指定 HttpMethod 的请求, 自定义请求的 http header, app 与 js 的交互

    [源码下载] 背水一战 Windows 10 (64) - 控件(WebView): 加载指定 HttpMethod 的请求, 自定义请求的 http header, app 与 js 的交互 作者: ...

  3. 背水一战 Windows 10 (63) - 控件(WebView): 基础知识, 加载 html, http, https, ms-appx-web:///, embedded resource, ms-appdata:///, ms-local-stream://

    [源码下载] 背水一战 Windows 10 (63) - 控件(WebView): 基础知识, 加载 html, http, https, ms-appx-web:///, embedded res ...

  4. 背水一战 Windows 10 (8) - 控件 UI: StateTrigger

    [源码下载] 背水一战 Windows 10 (8) - 控件 UI: StateTrigger 作者:webabcd 介绍背水一战 Windows 10 之 控件 UI VisualState 之 ...

  5. 背水一战 Windows 10 (68) - 控件(控件基类): UIElement - Pointer 相关事件, Tap 相关事件, Key 相关事件, Focus 相关事件

    [源码下载] 背水一战 Windows 10 (68) - 控件(控件基类): UIElement - Pointer 相关事件, Tap 相关事件, Key 相关事件, Focus 相关事件 作者: ...

  6. 背水一战 Windows 10 (67) - 控件(控件基类): DependencyObject - CoreDispatcher, 依赖属性的设置与获取, 依赖属性的变化回调

    [源码下载] 背水一战 Windows 10 (67) - 控件(控件基类): DependencyObject - CoreDispatcher, 依赖属性的设置与获取, 依赖属性的变化回调 作者: ...

  7. 背水一战 Windows 10 (61) - 控件(媒体类): InkCanvas 涂鸦编辑

    [源码下载] 背水一战 Windows 10 (61) - 控件(媒体类): InkCanvas 涂鸦编辑 作者:webabcd 介绍背水一战 Windows 10 之 控件(媒体类) InkCanv ...

  8. 背水一战 Windows 10 (37) - 控件(弹出类): MessageDialog, ContentDialog

    [源码下载] 背水一战 Windows 10 (37) - 控件(弹出类): MessageDialog, ContentDialog 作者:webabcd 介绍背水一战 Windows 10 之 控 ...

  9. 背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu

    [源码下载] 背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu 作者:webabcd 介绍背水一战 Windows 10 之 控件(弹 ...

随机推荐

  1. SpringBoot 之jsp

    Boot 内嵌的tomcat 是不支持jsp 的, jetty 也是. 虽然boot也是有默认配置一个InternalResourceViewResolver ,但是它并不像我们在springmvc ...

  2. laravel PHPExcel 使用小结

    最近需求要用到PHPExcel,laravel框架中有相应的组件https://github.com/Maatwebsite/Laravel-Excel,我用的是2.1的(3.0文档不详细而且坑似乎也 ...

  3. Java语法 [HelloWorld]

    程序代码: public class lqx {// AAAAANBBBBCKJKSLJIOQL/*请手打哦!*/ public static void main (String[] args) { ...

  4. 接口自动化测试链接https://www.cnblogs.com/finer/

    https://www.cnblogs.com/finer/ 测试框架的基本原则:业务逻辑与测试脚本分离,测试脚本与测试数据分离: 接口自动化的两种方式:工具(jmeter).代码(使用的是pytho ...

  5. selenium下打开Chrome报错解决

    错误如下: [22516:20196:0704/024642.979:ERROR:install_util.cc(597)] Unable to read registry value HKLM\SO ...

  6. AngularJS学习笔记(二)

    一.AngularJS Select(选择框) 1.使用 ng-options 创建选择框 <div ng-app="myApp" ng-controller="m ...

  7. JAVA中float与double的区别

    float是单精度类型,精度是8位有效数字,取值范围是10的-38次方到10的38次方,float占用4个字节的存储空间 double是双精度类型,精度是17位有效数字,取值范围是10的-308次方到 ...

  8. [leetcode]58. Length of Last Word最后一个词的长度

    Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the l ...

  9. [leetcode]47. Permutations全排列(给定序列有重复元素)

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  10. thinkphp 查表返回的数组,js解析有UNICode编码,解决办法

    public function getDeviceMsg(){ $allDevicesMsg = M("newdevicesstatus")->getField(" ...