[源码下载]

背水一战 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. 早期自学jQuery-二事件

    本节目录 ----------①鼠标事件 ----------②键盘事件 ----------③表单事件 ----------④文档窗口事件 ----------⑤举例,按下回车键触发事件 事件函数 ...

  2. JavaScript倒计时实现

    /** * 倒计时函数 * @param {String}} endTime 终止时间戳 */ const countDown = (endTime, callback) => { const ...

  3. python中TCP粘包问题解决方案

    TCP协议中的粘包问题 1.粘包现象 基于TCP写一个远程cmd功能 #服务端 import socket import subprocess sever = socket.socket() seve ...

  4. 【392】Python 列表解析

    参考: Python3 数据结构 | 菜鸟教程 列表推导式 列表推导式提供了从序列创建列表的简单途径.通常应用程序将一些操作应用于某个序列的每个元素,用其获得的结果作为生成新列表的元素,或者根据确定的 ...

  5. PowerDesign生成数据库

    最近要忙期考,但还是决定每天抽点空来写CodeSmith的系列文章了,在此实在不敢用教程这个词语,毕竟自己对CodeSmith了解的也不是很多,有很多牛人都在博客园发布了不少关于CodeSmith的文 ...

  6. Python文件操作---合并文本文件内容

    目前一个用的比较多的功能:将多个小文件的内容合并在一个统一的文件中,对原始文件重命名标记其已被处理过.之前使用其他脚本写的,尝试用python写了一下,顺便熟悉一下python的文件处理命令. 原始文 ...

  7. springboot寻找property的顺序

    Spring Boot uses a very particular PropertySource order that is designed to allow sensible overridin ...

  8. python获取文件夹的大小(即取出所有文件计算大小)

    import os path = r'/Users/authurchen/PycharmProjects/Demo' # print(os.listdir(path)) ls = os.listdir ...

  9. C语言之标准源文件模板

    /*======================================================================================* * 版权 : xxx ...

  10. 微服务SpringCloud无法进行服务消费

    最近用SpringCloud做微服务,一直无法成功进行服务消费. 我使用的服务消费者是Feign,声明式调用服务提供者. 排查过程 1.检查服务提供者: (1)对提供的方法进行测试,确保提供的服务没有 ...