02、获取 WebView 控件中,加载的 HTML 网页内容
在开发 app 的时候,WebView 是经常使用的控件。而且有时需要向 WebView 中的 html 内容
注入额外的 js 进行操作。这里记录一下在当前 WebView 控件中,获取 html 内容的方法。
运行效果:

1、首先在工程根目录下面放一个 test.html 文件,里面放入一些 html,作为测试内容:
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<!--绿色背景、居中-->
<div style="width:400px;height:100px;color:black;background:#0f0;margin:0px auto;font-size:40px;">
测试内容
</div>
</body>
</html>
放在根目录下:

2、在页面上放置一个 WebView 用来显示网页内容,另外放置两个按钮:
<Grid Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel HorizontalAlignment="Center" Orientation="Horizontal">
<Button Content="方法 一" Click="Button_f1_Click"/>
<Button Content="方法 二" Click="Button_f2_Click"/>
</StackPanel>
<WebView x:Name="webView" Grid.Row="2" Source="test.html"/>
</Grid>
3、在 C# 页面:
public MainPage()
{
this.InitializeComponent(); this.Loaded += MainPage_Loaded; webView.ScriptNotify += webView_ScriptNotify;
} // 把 appx 根目录下的 test.html 网页内容加载到 WebView 中
async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
string html = "";
StorageFile file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync("test.html"); using (StreamReader sr = new StreamReader(await file.OpenStreamForReadAsync()))
{
html = sr.ReadToEnd();
} // 如果网页内容是从网络下载的,则可以把 getHTML() 方法 注入到 html 中
html += "<script>function getHTML(){ window.external.notify(document.documentElement.innerHTML)}</script>"; webView.NavigateToString(html);
} // 方法 一:调用注入的 js 方法
async void Button_f1_Click(object sender, RoutedEventArgs e)
{
await webView.InvokeScriptAsync("getHTML", null); } void webView_ScriptNotify(object sender, NotifyEventArgs e)
{
// e.Value 中为方法调用的返回值
Debug.WriteLine(e.Value);
} // 方法 二:直接通过 html 中 window 对象的 eval 方法,将当前网页内容返回
async void Button_f2_Click(object sender, RoutedEventArgs e)
{
// 直接将结果返回(返回值为 WebView 中的 html),并不会出发 ScriptNotify 事件
var html = await webView.InvokeScriptAsync("eval", new[] { "document.documentElement.innerHTML" }); Debug.WriteLine(html);
}
02、获取 WebView 控件中,加载的 HTML 网页内容的更多相关文章
- 【高德地图API】Pivot控件中加载地图并禁止Pivot手势
如题,解决方案,参考[Windows phone应用开发[20]-禁止Pivot手势]http://www.cnblogs.com/chenkai/p/3408658.html. xaml代码清单 ...
- 重新想象 Windows 8.1 Store Apps (81) - 控件增强: 加载本地 html, 智能替换 html 中的 url 引用, 通过 Share Contract 分享 WebView 中的内容, 为 WebView 截图
原文:重新想象 Windows 8.1 Store Apps (81) - 控件增强: 加载本地 html, 智能替换 html 中的 url 引用, 通过 Share Contract 分享 Web ...
- easyui控件的加载顺序
使用easyui做布局时,会模仿窗口程序界面,做出一些较复杂的布局.按由外层到内层的顺序: (最外层)panel->tabs->tabs1 ->tabs2->layout-&g ...
- 使用DevExpress.XtraTabbedMdi.XtraTabbedMdiManager控件来加载MDI窗体
使用DevExpress.XtraTabbedMdi.XtraTabbedMdiManager控件来加载MDI窗体 [csharp] view plaincopyprint? <SPAN ...
- .net获取select控件中的文本内容
.net获取select控件中的文本内容 2009-11-28 21:19小V古 | 分类:C#/.NET | 浏览1374次 <select id="SecType" st ...
- GridView控件中加自动排列序号
GridView控件中加自动排列序号 为 Gridview 增加一个新的空白列,如下: <asp:BoundField HeaderText="序号"> < ...
- Delphi7 第三方控件1stClass4000的TfcImageBtn按钮控件动态加载jpg图片例子
Delphi7 第三方控件1stClass4000的TfcImageBtn按钮控件动态加载jpg图片例子 procedure TForm1.Button1Click(Sender: TObject); ...
- WPF中获取TreeView以及ListView获取其本身滚动条的方法,可实现自行调节scoll滚动的位置(可相应获取任何控件中的内部滚动条)
原文:WPF中获取TreeView以及ListView获取其本身滚动条的方法,可实现自行调节scoll滚动的位置(可相应获取任何控件中的内部滚动条) 对于TreeView而言: TreeViewAut ...
- 在WinForm应用程序中,使用选项卡控件来加载不同的Form界面!
TabPage tp=new TabPage(); your选项卡控件.Controls.Add(tp); From1 frm=new Form1(); frm.TopLevel = false; f ...
随机推荐
- WCF 自承载 提供源码
一.WCF 简单介绍 Windows Communication Foundation(WCF)是由微软发展的一组数据通信的应用程序开发接口,是一套通讯接口.现在比较流行的SOA就可以通过WCF实现. ...
- 搭建keepalived遇到的问题
搭建keepalived遇到的问题 主机192.168.100.10 备机192.168.100.20 VIP192.168.100.30 一.防火墙开启后双机都是master. 在搭建之后开启防火墙 ...
- systemctl使用
systemctl start httpd.service 这会启动httpd服务,就我们而言,Apache HTTP服务器. 要停掉它,需要以root身份使用该命令: systemctl stop ...
- ipv6相关
link-local和site-local address http://blog.163.com/s_u/blog/static/13308367201052411431027/ 学习IPV6的时候 ...
- js冒泡法和数组转换成字符串
js代码: window.onload = function(){ var mian = document.getElementById( "mian" ); var mian1 ...
- Java浮点运算-BigDecimal
package com.hsun.test; import static java.lang.System.out; import java.math.BigDecimal; public class ...
- jtemplate 为javascript前端html模版引擎
最近的项目中用到了jtemplate, 它是客户端基于javascript的模板引擎,绑定的数据为json对象.以前我在页面上显示数据列表时最喜欢用Repeater控件了,因为它相对与其它几个服务端控 ...
- Negative seek offset的解决方法
ZIP文件解压的时候出现错误:Negative seek offset 原因是把rar文件直接改为ZIP. 必须要 重新压缩为ZIP文件 不能直接修改文件名.
- uva10401Injured Queen Problem(递推)
题目:uva10401Injured Queen Problem(递推) 题目大意:依然是在棋盘上放皇后的问题,这些皇后是受伤的皇后,攻击范围缩小了.攻击范围在图中用阴影表示(题目).然后给出棋盘的现 ...
- Python 倒叙切片
倒序切片 对于list,既然Python支持L[-1]取倒数第一个元素,那么它同样支持倒数切片,试试: >>> L = ['Adam', 'Lisa', 'Bart', 'Paul' ...