背水一战 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 的事件的更多相关文章
- 背水一战 Windows 10 (65) - 控件(WebView): 对 WebView 中的内容截图, 通过 Share Contract 分享 WebView 中的被选中的内容
[源码下载] 背水一战 Windows 10 (65) - 控件(WebView): 对 WebView 中的内容截图, 通过 Share Contract 分享 WebView 中的被选中的内容 作 ...
- 背水一战 Windows 10 (64) - 控件(WebView): 加载指定 HttpMethod 的请求, 自定义请求的 http header, app 与 js 的交互
[源码下载] 背水一战 Windows 10 (64) - 控件(WebView): 加载指定 HttpMethod 的请求, 自定义请求的 http header, app 与 js 的交互 作者: ...
- 背水一战 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 ...
- 背水一战 Windows 10 (8) - 控件 UI: StateTrigger
[源码下载] 背水一战 Windows 10 (8) - 控件 UI: StateTrigger 作者:webabcd 介绍背水一战 Windows 10 之 控件 UI VisualState 之 ...
- 背水一战 Windows 10 (68) - 控件(控件基类): UIElement - Pointer 相关事件, Tap 相关事件, Key 相关事件, Focus 相关事件
[源码下载] 背水一战 Windows 10 (68) - 控件(控件基类): UIElement - Pointer 相关事件, Tap 相关事件, Key 相关事件, Focus 相关事件 作者: ...
- 背水一战 Windows 10 (67) - 控件(控件基类): DependencyObject - CoreDispatcher, 依赖属性的设置与获取, 依赖属性的变化回调
[源码下载] 背水一战 Windows 10 (67) - 控件(控件基类): DependencyObject - CoreDispatcher, 依赖属性的设置与获取, 依赖属性的变化回调 作者: ...
- 背水一战 Windows 10 (61) - 控件(媒体类): InkCanvas 涂鸦编辑
[源码下载] 背水一战 Windows 10 (61) - 控件(媒体类): InkCanvas 涂鸦编辑 作者:webabcd 介绍背水一战 Windows 10 之 控件(媒体类) InkCanv ...
- 背水一战 Windows 10 (37) - 控件(弹出类): MessageDialog, ContentDialog
[源码下载] 背水一战 Windows 10 (37) - 控件(弹出类): MessageDialog, ContentDialog 作者:webabcd 介绍背水一战 Windows 10 之 控 ...
- 背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu
[源码下载] 背水一战 Windows 10 (36) - 控件(弹出类): ToolTip, Popup, PopupMenu 作者:webabcd 介绍背水一战 Windows 10 之 控件(弹 ...
随机推荐
- Error importing tensorflow. Unless you are using bazel version `CXXABI_1.3.8' not found
I have re-installed Anaconda2. And I got the following error when 'python -c 'import tensorflow'' &g ...
- django 开发之前后端分离开发模式
1. 什么是前后端分离开发的概念: 前端页面运行前端服务器上,负责页面的渲染(静态文件的加载)与跳转 后端代码运行在后端服务器上, 负责数据的处理(提供数据请求的接口) 2. 前后端分离开发碰到的问题 ...
- component lists rendered with v-for should have explicit keys
错误:component lists rendered with v-for should have explicit keys 解析:使用vue 的v-for时,需要:key指定唯一key 文档:h ...
- JEECG-Swagger UI的使用说明
一.代码生成 (此步骤为代码生成器的使用,如不清楚请查阅相关文档视频) 1.进入菜单[在线开发]-->[Online表单开发],选中一张单表/主表,点击代码生成按钮. 2.弹出页面中填写代码生成 ...
- Sql Server数据库之事务,视图,索引
一.事务的定义 事务是一种机制,包含一组操作指令,并将所有的命令作为一个整体一起向系统提交或撤销操作请求(要么都执行,要么都不执行) 二.事务的分类 显式事务:用Begin TRANSCATION开始 ...
- iOS的SVN
1.cornerstone 2.smart svn mac (比较好用) 3.还xcode自带的.
- nlp算法工程师养成记 目标要求
时间规定: 2018.12.07-2018.02.15 能力养成: linux, shell python, c++(会多少算多少) tensorflow, keras, pytorch(tf优先) ...
- 第一章 C++语言入门
标准数据类型 C++语言提供了丰富的数据类型,如整数类型.实数类型(浮点数).字符类型等.每种数据类型均有均值范围,Dev-C++(4.9.9.2)是Windows平台 ...
- mysql面试题集
Mysql 的存储引擎,myisam和innodb的区别. 答: 1.MyISAM 是非事务的存储引擎,适合用于频繁查询的应用.表锁,不会出现死锁,适合小数据,小并发.5.6之前默认myisam 2. ...
- [leetcode]78. Subsets数组子集
Given a set of distinct integers, nums, return all possible subsets (the power set). Note: The solut ...