在开发 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 网页内容的更多相关文章

  1. 【高德地图API】Pivot控件中加载地图并禁止Pivot手势

    如题,解决方案,参考[Windows phone应用开发[20]-禁止Pivot手势]http://www.cnblogs.com/chenkai/p/3408658.html. xaml代码清单   ...

  2. 重新想象 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 ...

  3. easyui控件的加载顺序

    使用easyui做布局时,会模仿窗口程序界面,做出一些较复杂的布局.按由外层到内层的顺序: (最外层)panel->tabs->tabs1 ->tabs2->layout-&g ...

  4. 使用DevExpress.XtraTabbedMdi.XtraTabbedMdiManager控件来加载MDI窗体

    使用DevExpress.XtraTabbedMdi.XtraTabbedMdiManager控件来加载MDI窗体     [csharp] view plaincopyprint? <SPAN ...

  5. .net获取select控件中的文本内容

    .net获取select控件中的文本内容 2009-11-28 21:19小V古 | 分类:C#/.NET | 浏览1374次 <select id="SecType" st ...

  6. GridView控件中加自动排列序号

    GridView控件中加自动排列序号 为 Gridview 增加一个新的空白列,如下: <asp:BoundField  HeaderText="序号">    < ...

  7. Delphi7 第三方控件1stClass4000的TfcImageBtn按钮控件动态加载jpg图片例子

    Delphi7 第三方控件1stClass4000的TfcImageBtn按钮控件动态加载jpg图片例子 procedure TForm1.Button1Click(Sender: TObject); ...

  8. WPF中获取TreeView以及ListView获取其本身滚动条的方法,可实现自行调节scoll滚动的位置(可相应获取任何控件中的内部滚动条)

    原文:WPF中获取TreeView以及ListView获取其本身滚动条的方法,可实现自行调节scoll滚动的位置(可相应获取任何控件中的内部滚动条) 对于TreeView而言: TreeViewAut ...

  9. 在WinForm应用程序中,使用选项卡控件来加载不同的Form界面!

    TabPage tp=new TabPage(); your选项卡控件.Controls.Add(tp); From1 frm=new Form1(); frm.TopLevel = false; f ...

随机推荐

  1. SVM初学

    一.            一点基础数学知识 如今硕士都快毕业了,反而将自己的很多数学知识忘的几乎相同了.所以.如今决心再捡起来.以补齐自己的数学短板.为以后的研究做好铺垫吧.如今结合自己学习SVM. ...

  2. iOS: iOS各种设备信息获取

    Author:si1ence Link:http://www.jianshu.com/p/b23016bb97af 为了统计用户信息.下发广告,服务器端往往需要手机用户设备及app的各种信息,下面讲述 ...

  3. [Python爬虫] 之七:selenium webdriver定位不到元素的五种原因及解决办法(转载)

    转载:http://www.51testing.com/html/87/300987-831171.html 1.动态id定位不到元素for example:        //WebElement ...

  4. 更改DNS轻松访问google.com,FaceBook,Youtube等

    将默认的Dns更改为42.120.21.30即可打开 https://www.google.com/ https://www.facebook.com/ https://www.youtube.com ...

  5. 基于java语言的给cube添加custom view来实现权限控制

    今天是农历2014年的最后一个工作日了,在这里提前祝大家新年快乐.羊年大吉!当然本人今天也拿出来点儿真东西,做为献给大家的新年礼物,依次共勉. 下文主要讲述的是使用Java代码来完成对cube基于部门 ...

  6. [Functional Programming] Working with two functors(Applicative Functors)-- Part2 --liftAN

    Let's examine a pointfree way to write these applicative calls. Since we know map is equal to of/ap, ...

  7. js 能实现监听F5页面刷新子iframe 而父页面不刷新

    重点是阻止默认的刷新,这样外部页面就不刷新了,然后指定刷新iframe,我下面给出了思路 document.onkeypress = function(e){ if(e.keyCode == 116) ...

  8. Python中的作用域

    Python中的作用域 Python 中,一个变量的作用域总是由在代码中被赋值的地方所决定的. 当 Python 遇到一个变量的话他会按照这样的顺序进行搜索: 本地作用域(Local)→当前作用域被嵌 ...

  9. OpenJudge百炼习题解答(C++)--题4010:2011

    题: 总时间限制:  1000ms  内存限制:  65536kB 描写叙述 已知长度最大为200位的正整数n.请求出2011^n的后四位. 输入 第一行为一个正整数k,代表有k组数据,k<=2 ...

  10. OpenERP财务管理若干概念讲解

    来自:http://shine-it.net/index.php/topic,2431.0.html 一.记账凭证(Account Move) 会计上的记账凭证,也叫会计分录,在OpenERP中叫&q ...