在开发 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. OpenShift负载分区策略(Router Shading)

    在很多场景下,单靠几个在Infra节点上的Router进行服务请求的转发是不够的,项目中很多时候都有流量隔离的需求,主要场景在于: 一个集群中的不同的环境的流量隔离需求,比如开发走几个Router,生 ...

  2. 谈谈Copy-on-Write容器

    1.简介 Copy-On-Write简称COW,是一种用于程序设计中的优化策略.其基本思路是,从一开始大家都在共享同一个内容,当某个人想要修改这个内容的时候,才会真正把内容Copy出去形成一个新的内容 ...

  3. 数学图形(2.20)3D曲线

    这一节主要是发布我自己写的3D曲线, (1)立体flower线圈 vertices = a = 10.1 b = 3.1 s = (a + b) / b o = i = to (**PI) j = m ...

  4. 数学图形(1.40)T_parameter

    不记得在哪搞了个数学公式生成的图形. vertices = t = to (*PI) r = 2.0 x = r*(*cos(t) - cos(*t)) y = r*(*sin(t) - sin(*t ...

  5. 初学数位DP--hdu 2089

    其实是做topcoder的时候碰到不会的题,看人家说要用数位dp,所以拿http://acm.hdu.edu.cn/showproblem.php?pid=2089来学习了一下 数位dp适合在一段数的 ...

  6. [Python爬虫] 之五:Selenium 处理滚动条

     selenium并不是万能的,有时候页面上操作无法实现的,这时候就需要借助JS来完成了. 当页面上的元素超过一屏后,想操作屏幕下方的元素,是不能直接定位到,会报元素不可见的.这时候需要借助滚动条来拖 ...

  7. [Functional Programming] Working with two functors(Applicative Functors)-- Part1 --.ap

    What is applicative functor: the ability to apply functors to each other. For example we have tow fu ...

  8. netlink error: too many arguments to function 'netlink_kernel_create'

    2.6版本的 netlink_kernel_create(&init_net, NETLINK_TEST, 0, NULL, kernel_receive ,THIS_MODULE);   3 ...

  9. Android studio DrawerLayout

    网上开源项目地址:https://github.com/ikimuhendis/LDrawer 效果图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQW ...

  10. PHP出现Notice警告怎么办

    如下所示,出现Notice警告 程序并没有大错,只是一些变量没有定义,你在代码的第一行加入这一句就可以了:error_reporting(E_ALL ^ E_NOTICE); 恢复正常了.