下面是MSDN的使用说明:

The following example shows how to handle the ScriptNotify event. In this example, the InvokeScript is called, which in turn calls the LoadSearch function in the following HTML. 
The HTML file must be hosted in the same domain as the Silverlight application. The Javascript within the HTML page calls window.external.notify,
 which raises the ScriptNotify event in the Silverlight application
必须在同一个域下才可用哦!

example:


The following example shows how to handle the ScriptNotify event. In this example, the InvokeScript is called, which in turn calls the LoadSearch function in the following HTML. The HTML file must be hosted in the same domain as the Silverlight application. The Javascript within the HTML page calls window.external.notify, which raises the ScriptNotify event in the Silverlight application.

<html xmlns="http://www.w3.org/1999/xhtml" ><head><title></title><script type="text/javascript" >    function LoadSearch(searchString) {        window.location = "http://www.bing.com/search?q=" + searchString        window.external.notify("Search completed")     }</script></head><body>Silverlight WebBrowser control.</body></html>


public partial class MainPage : UserControl{public MainPage()    {        InitializeComponent();    }

void WB1_ScriptNotify(object sender, NotifyEventArgs e)    {        Button1.Content = e.Value;        Button1.IsEnabled = false;

    }

private void Button1_Click(object sender, RoutedEventArgs e)    {object results = WB1.InvokeScript("LoadSearch", new string[] { "Silverlight" });    }}

?
<div class="cnblogs_code"><pre><div><span style="color: #000000;">XAML <br><br></span><span style="color: #000000;"><</span><span style="color: #000000;">StackPanel x:Name</span><span style="color: #000000;">=</span><span style="color: #800000;">"</span><span style="color: #800000;">LayoutRoot</span><span style="color: #800000;">"</span><span style="color: #000000;"> Height</span><span style="color: #000000;">=</span><span style="color: #800000;">"</span><span style="color: #800000;">358</span><span style="color: #800000;">"</span><span style="color: #000000;"> Width</span><span style="color: #000000;">=</span><span style="color: #800000;">"</span><span style="color: #800000;">489</span><span style="color: #800000;">"</span><span style="color: #000000;"> Background</span><span style="color: #000000;">=</span><span style="color: #800000;">"</span><span style="color: #800000;">LightBlue</span><span style="color: #800000;">"</span><span style="color: #000000;">></span><span style="color: #000000;"><br>    </span><span style="color: #000000;"><</span><span style="color: #000000;">WebBrowser  x:Name</span><span style="color: #000000;">=</span><span style="color: #800000;">"</span><span style="color: #800000;">WB1</span><span style="color: #800000;">"</span><span style="color: #000000;"> Source</span><span style="color: #000000;">=</span><span style="color: #800000;">"</span><span style="color: #800000;">http://localhost/HTMLPage1.htm</span><span style="color: #800000;">"</span><span style="color: #000000;"> Height</span><span style="color: #000000;">=</span><span style="color: #800000;">"</span><span style="color: #800000;">272</span><span style="color: #800000;">"</span><span style="color: #000000;"> Width</span><span style="color: #000000;">=</span><span style="color: #800000;">"</span><span style="color: #800000;">379</span><span style="color: #800000;">"</span><span style="color: #000000;"> <br>                 ScriptNotify</span><span style="color: #000000;">=</span><span style="color: #800000;">"</span><span style="color: #800000;">WB1_ScriptNotify</span><span style="color: #800000;">"</span><span style="color: #000000;"> Margin</span><span style="color: #000000;">=</span><span style="color: #800000;">"</span><span style="color: #800000;">5</span><span style="color: #800000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">/></span><span style="color: #000000;"><br>    </span><span style="color: #000000;"><</span><span style="color: #000000;">Button Width</span><span style="color: #000000;">=</span><span style="color: #800000;">"</span><span style="color: #800000;">200</span><span style="color: #800000;">"</span><span style="color: #000000;"> x:Name</span><span style="color: #000000;">=</span><span style="color: #800000;">"</span><span style="color: #800000;">Button1</span><span style="color: #800000;">"</span><span style="color: #000000;"> Content</span><span style="color: #000000;">=</span><span style="color: #800000;">"</span><span style="color: #800000;">Click to search!</span><span style="color: #800000;">"</span><span style="color: #000000;"> Click</span><span style="color: #000000;">=</span><span style="color: #800000;">"</span><span style="color: #800000;">Button1_Click</span><span style="color: #800000;">"</span><span style="color: #000000;"> </span><span style="color: #000000;">/></span><span style="color: #000000;"><br></span><span style="color: #000000;"></</span><span style="color: #000000;">StackPanel</span><span style="color: #000000;">></span></div></pre>
</div>
<br>

详细实例请看下文(是一个外国家伙写的,很简单一看就懂的,不翻译了):

Until version 4, Silverlight does not really have a mechanism for displaying HTML based content.

Prior to version 4, you can have Silverlight controls and HTML UI co-existing in the browser and they can interoperate with each other in a rich way. You can even use transparency and windowless techniques to overlay them but you can’t really ask a Silverlight application to display HTML for you. You always have to get it done elsewhere and, furthermore, if you then packaged your application to run as a Silverlight Out-Of-Browser (SLOOB) app then those techniques wouldn’t help as you lose the ability to display HTML at all.

That changes with version 4 with the new WebBrowser control ( WPF also has a Frame and a WebBrowser control which both offer levels of HTML hosting ability – the Silverlight WebBrowser looks like a subset of the WPF version ).

The first thing to say about the WebBrowser control is that it only functions in Silverlight Out-Of-Browser applications ( SLOOBs ) – in the browser the control displays a blank UI so an application developer/designer would need to think about how that would work if the application can also be run within a browser window.

For an out-of-browser application, I can put a simple UI together;

<UserControl x:Class="SilverlightApplication16.MainPage"     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"     mc:Ignorable="d"     d:DesignHeight="300" d:DesignWidth="400">

<Grid x:Name="LayoutRoot" Background="White"> <Grid.RowDefinitions> <RowDefinition Height="Auto"/> <RowDefinition /> </Grid.RowDefinitions> <Grid             x:Name="gridControls"> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition                     Width="Auto" /> </Grid.ColumnDefinitions> <TextBox                 x:Name="txtUri"                 Margin="5"                 HorizontalAlignment="Stretch" /> <Button                 x:Name="buttonGo"                 Grid.Column="1"                 Content="Go"                 Click="OnGo"                 Margin="5" /> </Grid> <WebBrowser             LoadCompleted="BrowserLoadCompleted"             Grid.Row="1"             Margin="5"             x:Name="browserControl" /> </Grid> </UserControl>

and then add a little code-behind that;

view plaincopy to clipboardprint?
using System;  using System.Collections.Generic;  using System.Linq;  using System.Net;  using System.Windows;  using System.Windows.Controls;  using System.Windows.Documents;  using System.Windows.Input;  using System.Windows.Media;  using System.Windows.Media.Animation;  using System.Windows.Shapes;  

namespace SilverlightApplication16  {  public partial class MainPage : UserControl    {  public MainPage()      {        InitializeComponent();      }  private void OnGo(object sender, RoutedEventArgs e)      {        buttonGo.IsEnabled = false;  

      browserControl.Navigate(new Uri(txtUri.Text, UriKind.Absolute));      }  private void BrowserLoadCompleted(object sender, EventArgs e)      {        buttonGo.IsEnabled = true;      }    }  }  

and ( out of browser ) that allows me to navigate to some HTML;

What might not be immediately obvious is that myPage.htm is part of the same web-site that the application originally came from ( i.e. site-of-origin ) and that an attempt to navigate to any site might fail ( subject to cross-domain policy ).

That can be circumvented by having the application also elevate itself. If the application is elevated then cross-domain policy isn’t applied and so we can go anywhere as in the example below ( the application now having been installed as anelevated SLOOB );

You might notice that in this example this is me with my out-of-browser application visiting Microsoft.com which is made up of HTML and Silverlight. So…we have one Silverlight application hosting HTML which hosts another Silverlight application. Similarly, I can visit sites that have “other plugins” like the Flash site below;

I’m not sure how much/little this control is integrated into the Silverlight rendering engine. I tried ( for fun ) applying aPlaneProjection to it and that didn’t really work out so I’m not sure that you’re meant to use the control in this way ( more on doing fancy things with HTML without trying to bend the control in a moment but back to regular stuff for a second… )

What you can also get the control to do is load HTML at runtime via its “NavigateToString” method so if I just change my code;

view plaincopy to clipboardprint?
private void OnGo(object sender, RoutedEventArgs e)  {    buttonGo.IsEnabled = false;  

  browserControl.NavigateToString(txtUri.Text);  }  

then I can just type HTML straight into the textbox and that works out fine ( note – the application does not have to be elevated for this to work );

Now, in the WPF version of this control there are two other “interesting” functions on the control. One is called InvokeScriptand that exists on the Silverlight control and can be used to invoke a piece of script inside the control.

This isn’t intended to work if you use the Navigate method but there’s another way to set the URL which is the Sourceproperty and so I can re-jig my code a little bit such that the code-behind looks like;

view plaincopy to clipboardprint?
public partial class MainPage : UserControl  {  public MainPage()    {      InitializeComponent();    }  private void OnGo(object sender, RoutedEventArgs e)    {      buttonGo.IsEnabled = false;      browserControl.Source = new Uri(txtUri.Text, UriKind.Absolute);    }  private void BrowserLoadCompleted(object sender, EventArgs e)    {      browserControl.InvokeScript("ChangeText", "Done");      buttonGo.IsEnabled = true;    }  }  

and if I then edit the HTML that I want to load so it looks like;

view plaincopy to clipboardprint?
<html>  <head>  <script type="text/javascript">      function ChangeText(text) {          document.getElementById("myDiv").innerText = text;      }  </script>  </head>  <body>  <div id="myDiv">Hello World</div>  </body>  </html> 

then when I load up that particular HTML file with the WebBrowser control I get the expected response straight away;

and so the Silverlight control loads the HTML and then runs the script which changes the DOM inside of the HTML. Note that this isn’t possible for cross-domain content.

It’s also possible to communicate “back” from the hosted Javascript to the Silverlight control although it’s not as functional as it is in WPF. If I re-work my HTML code to look like;

view plaincopy to clipboardprint
<html>  <head>  <script type="text/javascript">      function buttonClick()      {          window.external.Notify("They clicked!");      }  </script>  </head>  <body>  <input type="button" value="test" onclick="buttonClick()" />  </body>  </html> 

and then change my Silverlight UI a little to add a new textbox;

view plaincopy to clipboardprint?
View Code

with code-behind;

view plaincopy to clipboardprint?
using System;  using System.Collections.Generic;  using System.Linq;  using System.Net;  using System.Windows;  using System.Windows.Controls;  using System.Windows.Documents;  using System.Windows.Input;  using System.Windows.Media;  using System.Windows.Media.Animation;  using System.Windows.Shapes;  

namespace SilverlightApplication16  {  public partial class MainPage : UserControl    {  public MainPage()      {        InitializeComponent();      }  private void OnGo(object sender, RoutedEventArgs e)      {        buttonGo.IsEnabled = false;        browserControl.Source = new Uri(txtUri.Text, UriKind.Absolute);      }  private void BrowserLoadCompleted(object sender, EventArgs e)      {        buttonGo.IsEnabled = true;      }  private void OnScriptNotify(object sender, NotifyEventArgs e)      {        txtStatus.Text = e.Value;        }    }  }  

then that allows the hosted Javascript to communicate through the WebBrowser control via the call towindow.external.Notify() which gets picked up on the .NET side by the ScriptNotify event on the WebBrowser control and I just handle it in a little handler and change the text on my txtStatus TextBlock to represent whatever was passed from Javascript ( this is running out-of-browser but not elevated ).

The other “interesting” aspect of the HTML support is that Silverlight 4 also has an HTMLBrush which can take HTML content and paint areas so, whilst my previous attempt to integrated the WebBrowser control with the PlaneProjection system didn’t work very well, I can paint arbitrary areas with a brush. So, if I’ve got a little UI;

view plaincopy to clipboardprint?
View Code

with some code behind it ( note – I feel I should be able to set the WebBrowser.Source property from XAML but it’s not working for me at the time of writing );

view plaincopy to clipboardprint?
using System;  using System.Collections.Generic;  using System.Linq;  using System.Net;  using System.Windows;  using System.Windows.Controls;  using System.Windows.Documents;  using System.Windows.Input;  using System.Windows.Media;  using System.Windows.Media.Animation;  using System.Windows.Shapes;  

namespace SilverlightApplication18  {  public partial class MainPage : UserControl    {  public MainPage()      {        InitializeComponent();  

this.Loaded += (s, e) =>          {            myBrowser.Source = new Uri("http://www.microsoft.com", UriKind.Absolute);          };            }  private void OnRedraw(object sender, RoutedEventArgs e)      {        myBrush.Redraw();      }    }  }  

then that gives me a UI where I’m painting the rectangle on the right hand side with that HtmlBrush pointing to theWebBrowser control on the left hand side;

Note – this isn’t a VisualBrush and so it doesn’t do “live updates”. You’ll notice the Redraw button and the call toHtmlBrush.Redraw and that’s the mechanism for keeping things in sync so it’s more like a snapshot than anything else but pretty interesting regardless.

Silverlight 4 WebBrowser的使用及调用 WebBrowser 中的 javascript 方法相关推荐

  1. Java 反射机制详解:私有方法调用头大?如何通过反射调用类中的私有方法?

    文章目录 前言 一.私有方法在本类中直接调用 1.1.在本类中实例化,调用私有方法 1.2.尝试在其他类直接调用私有方法(错误示范) 二.使用反射实例化类强制调用私有方法 2.1.使用类加载器加载被调 ...

  2. C#Winform调用网页中的JS方法

    其实还是还是相当的简单,本文将详细的用代码来展示一下如何调用,并且送上源代码. 这个主题,其实我是在QQ问问里看到的,那为Q友送上了380分,所以就写做了这样的一个DEMO.同时这里也分享给更多正在寻 ...

  3. ajax远程调用,jquery中的ajax方法怎样通过JSONP进行远程调用

    关于JSONP的概念和为什么要使用JSONP网上已经有很多教程,这一节主要演示下在JQUERY中的ajax方法怎样通过JSONP进行远程调用 首先介绍下$.ajax的参数 type:请求方式 GET/ ...

  4. 自定义工具类Static方法调用业务中Service层方法

    文章目录 前言 一.如何实现? 二.Util工具类实现 代码相关注解 总结 前言 今天需要验证OpenId,来看是否为系统用户,直接封装一个工具类,遇到了调用业务层service的方法.记一次学习笔记 ...

  5. java按钮调用 后台方法_onclick怎么调用后台中的一个方法

    javaScript函数中执行C#代码中的函数: 方法一:1.首先建立一个按钮,在后台将调用或处理的内容写入button_click中; 2.在前台写一个js函数,内容为document.getEle ...

  6. java调用spark_spark中的并行方法调用以及传递方法中使用spark会话

    让我先告诉大家我对Spark很新 . 我需要在表中处理大量记录,当它通过电子邮件分组时大约是100万 . 我需要根据 individual email 和 update the database ba ...

  7. Xamarin Android Webview中JS调用App中的C#方法

    参考链接:https://github.com/xamarin/recipes/tree/master/Recipes/android/controls/webview/call_csharp_fro ...

  8. 调用iframe中的js方法_SolidWorks中标准件库的创建及调用方法

    在使用SolidWorks进行机械设计时,常用的标准件(如螺栓.螺母.垫圈等)通常可以在安装了SolidWorksToolbox插件后调出使用,而许多标准件在Toolbox并不存在,不能从插件中直接调 ...

  9. R语言中调用windows中的字体方法

    windowsFonts(A=windowsFont("华文彩云"),#这里使用的是字体的主题名称B=windowsFont("华文仿宋"),C=windows ...

最新文章

  1. UE商城资源 Motion Symphony 运动匹配插件
  2. Scala开发入门教程
  3. mysql 互斥_MySql中互斥量mutex的实现
  4. Oracle调用接口(OCI)源码剖析(2):执行SQL语句并获取结果
  5. CycleGAN非配对图像生成,定制你的卡通照
  6. OpenCV 中文wiki
  7. js实现选中文字 分享功能
  8. Polly:提升服务可用性
  9. access 图片存储的格式_Ceph对象存储RGW架构及安装实战
  10. bzoj 5369 最大前缀和
  11. 详解双曲函数(对比三角函数推导)
  12. 大白菜超级U盘启动盘制作
  13. pocketpc同步驱动_PocketPC上的NewsBreak RSS Feed阅读器和播客
  14. 并发调度的可串行性:可串行化调度、冲突可串行化调度、两段锁协议
  15. Gluster安装+kubesphere3.0
  16. android 判断是白天还是晚上,然后设置地图模式
  17. REVERSE-COMPETITION-HGAME2022-Week3
  18. linux树莓派扩容,树莓派OpenWrt扩容磁盘分区大小
  19. getTime()得到时间戳不一样
  20. 已解决mysql报错ERROR 1049 (42000): Unknown database ‘数据库‘

热门文章

  1. 对于初学者Python开发难学吗?适合初学者吗?
  2. 成为表情包大帝,仅需几十行python代码,简单易学
  3. 网络爬虫流程与注意事项
  4. 栈的输出_栈和队列--十进制转化为二进制
  5. php session 在线用户,php – 使用$_SESSION超全局获取当前在线用户并将其重新设置回当前会话数据是否很难?...
  6. 设置系统和管理计算机硬件的应用程序,Windows7操作系统中用于设置系统和管理计算机硬件的应用程序是()...
  7. 二叉搜索树c/c++代码实现
  8. 解题报告:SP2713 GSS4 - Can you answer these queries IV(GSS线段树八部曲之四)
  9. 2020德勤面试开始了吗_2020下半年教师资格证面试时间已出!12月10日开始报名
  10. centos 7 mysql 创建用户_【CentOS 7MySQL常用操作4】,MySQL创建用户以及授权#180116