对于Silverlight与JS相互调用感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解jsclientheight,并且为您提供关于Silverlight2学习教程(五):JavaScri
对于Silverlight与JS相互调用感兴趣的读者,本文将提供您所需要的所有信息,我们将详细讲解js clientheight,并且为您提供关于Silverlight 2学习教程(五):JavaScript与Silverlight托管代码相互调用、Silverlight 2学习教程(六): Silverlight托管代码调用Javascript中的JSON对象、Silverlight 5 beta新特性探索系列:1.安装Silverlight 5 beta环境以及OOB模式下Silverlight 5 多窗口支持、Silverlight Events:与John Papa面对面学习Silverlight 4的宝贵知识。
本文目录一览:- Silverlight与JS相互调用(js clientheight)
- Silverlight 2学习教程(五):JavaScript与Silverlight托管代码相互调用
- Silverlight 2学习教程(六): Silverlight托管代码调用Javascript中的JSON对象
- Silverlight 5 beta新特性探索系列:1.安装Silverlight 5 beta环境以及OOB模式下Silverlight 5 多窗口支持
- Silverlight Events:与John Papa面对面学习Silverlight 4
Silverlight与JS相互调用(js clientheight)
1.sl调用js
比如我们在页面中定义一个js函数:
<script type="text/javascript">
function fnTest(msg) {
alert('fnTest:' + msg);
}
</script>
sl中可以有二种调用方式:
HtmlPage.Window.CreateInstance("fnTest","CreateInstance");
或者
HtmlPage.Window.Invoke("fnTest","Invoke");
运行的效果完全相同,但是建议用Invoke方法,原因是:
CreateInstance是HtmlWindow中定义的方法,而HtmlWindow继承自HtmlObject,再向上找HtmlObject又继承ScriptObject,最终调用的还是ScriptObject中的Invoke方法,所以用CreateInstance绕来绕去,最终还是绕到了Invoke上,还不如一步到位直接Invoke更高效。
2.sl修改dom元素属性
比如页面中有一个文本框: <input type="text" id="txtTarget" name="txtTarget" />
sl中同样也有二种方法调用:
HtmlPage.Document.GetElementById("txtTarget").SetAttribute("value","Silverlight");
HtmlPage.Document.GetElementById("txtTarget").SetProperty("value","Silverlight");
功能完全相同,同样建议用SetProperty方法,理由同上
3.sl为dom元素附加事件
HtmlPage.Document.GetElementById("txtTarget").AttachEvent("focus",new EventHandler(AttachJsMethod));
private void AttachJsMethod(object o,EventArgs e)
{
HtmlPage.Window.Invoke("fnTest","silverlight");
}
以上代码将为txtGarget文本框附加onfocus事件处理函数:fnTest
4.js调用sl中的方法,属性
sl部分的关键处理是:要在暴露出来的方法/属性前加上[ScriptableMember()],参考下面的代码:
[ScriptableMember()]
public string Hello(string Msg)
{
return "Hello," + Msg;
}
[ScriptableMember()]
public void Test(string msg)
{
MessageBox.Show("From Silverlight:" + msg);
}
private string _myName = "";
[ScriptableMember()]
public string MyName { set { _myName = value; } get { return _myName.Length == 0 ? "MyName Test" : _myName; } }
这里定义二个方法(一个有返回值,一个没返回值),外加一个属性
然后要在App.Xaml.cs中注册类型,参考下面这样:
private void Application_Startup(object sender,StartupEventArgs e)
{
MainPage mp = new MainPage();
this.RootVisual = mp;
HtmlPage.RegisterScriptableObject("myMainPage",mp);
}
js中这可以使用了,不过得先给silverlight的object标签加一个参数,参考下面这样:
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2"
width="100%" height="100%">
<param name="source" value="ClientBin/JavaScriptInvoke.xap" />
<param name="onError" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="3.0.40818.0" />
<param name="onLoad" value="SilverlightLoaded" />
<param name="autoUpgrade" value="true" />
<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40818.0"https://www.jb51.cc/tag/decoration/" target="_blank">decoration: none">
<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight"
/>
</a>
</object>
然后定义对应的函数:
<script type="text/javascript">
var slCtl = null;
function SilverlightLoaded(sender) {
slCtl = sender.getHost();
alert(slCtl.Content.myMainPage.Hello("JavaScript"));
}
var fnClickTest = function() {
if (slCtl != null) {
slCtl.Content.myMainPage.Test("Hi")
alert(slCtl.Content.myMainPage.MyName);
}
}
</script>
为了测试fnClickTest,可以在页面中加一个button,类似这样:
<button onclick="fnClicktest()">调用SL中的方法</button>
说明:SilverlightLoaded会在silverlight控件加载完成时触发,触发完了以后,slCtl这个全局变量就保留了对silverlight的引用,其它地方就能继续调用silverlight中的各种方法和属性了
Silverlight 2学习教程(五):JavaScript与Silverlight托管代码相互调用
要实现JavaScript调用Silverlight程序里面的托管代码,需要先在应用程序的启动(Application_Startup)事件里注册要进行访问的对象,而要从Silverlight的托管代码里访问HTML页面对象或者页面中的JavaScript,使用HtmlPage的Document/HtmlElement和HtmlWindow即可。
下面,我们就以例子来说明两者相互访问的方法,代码里面有很详细的注释,这里不再累述。
Page.xaml:
xmlns ="http://schemas.microsoft.com/client/2007"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
Width ="400" Height ="300" >
< Grid x:Name ="LayoutRoot" Background ="White" >
< Canvas Canvas.Top ="20" >
< TextBlock Canvas.Top ="10" Canvas.Left ="20" > 请输入您的姓名: </ TextBlock >
< TextBox x:Name ="UserInput" Width ="200" Height ="30" Canvas.Top ="40" Canvas.Left ="20" ></ TextBox >
< TextBlock x:Name ="Msg" Canvas.Top ="90" Canvas.Left ="20" Foreground ="Navy" FontSize ="36" ></ TextBlock >
< Button Click ="Button_Click" Content ="单击我" FontSize ="24" Width ="160" Height ="60" x:Name ="BtnTest" Canvas.Top ="160" Canvas.Left ="20" ></ Button >
</ Canvas >
</ Grid >
</ UserControl >
Page.xaml.cs:
using System.Collections.Generic;
using System.Linq;
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;
using System.Windows.browser;
namespace SilverlightApplication1
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
}
private void Button_Click( object sender, RoutedEventArgs e)
{
string UserInputContent = this .UserInput.Text;
if (UserInputContent.Equals(String.Empty))
{
UserInputContent = " Hello Silverlight World! " ;
}
else
{
UserInputContent = " 你好, " + UserInputContent;
}
HtmlWindow win = HtmlPage.Window;
this .Msg.Text = UserInputContent;
win.Alert( " Silverlight 里面弹出的对话框。 " + UserInputContent);
// 执行页面中的js函数:
win.Eval( " getArraytest() " );
Object[] args = { " 将此参数传递给 js 函数 " };
win.Invoke( " getArrayTest " , args);
// 如果页面中的值
HtmlDocument doc = HtmlPage.Document;
doc.GetElementById( " UserName " ).SetAttribute( " value " , this .UserInput.Text);
}
[ScriptableMember()]
public string InterInvole()
{
string username = HtmlPage.Document.GetElementById( " UserName " ).GetAttribute( " value " );
this .UserInput.Text = username;
this .Msg.Text = " 您输入了: " + username;
return " 你从js脚本中调用了 Silverlight 里面的方法。 " ;
}
}
}
App.xaml.cs:
using System.Collections.Generic;
using System.Linq;
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;
using System.Windows.browser;
namespace SilverlightApplication1
{
public partial class App : Application
{
public App()
{
this .Startup += this .Application_Startup;
this .Exit += this .Application_Exit;
this .UnhandledException += this .Application_UnhandledException;
InitializeComponent();
}
private void Application_Startup( object sender, StartupEventArgs e)
{
// Load the main control
Page p = new Page();
HtmlPage.RegisterScriptableObject( " SilverlightApplicationExample " , p);
// 请注意这里的定义方法,如果这里的p写成 new Page(),则Javascript基本不能给 UserInput 赋值!
this .RootVisual = p;
}
private void Application_Exit( object sender, EventArgs e)
{
}
private void Application_UnhandledException( object sender, ApplicationUnhandledExceptionEventArgs e)
{
}
}
}
SilverlightApplication1TestPage.aspx:
<% @ Register Assembly = " System.Web.Silverlight " Namespace = " System.Web.UI.SilverlightControls " TagPrefix = " asp " %>
<! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
< html xmlns ="http://www.w3.org/1999/xhtml" >
< head runat ="server" >
< title > Silverlight 2托管代码与Javascript交互的例子 </ title >
< script type ="text/javascript" >
// <!{CDATA[
// 定义全局变量:
var testvar = " 孟宪会 " ;
// 定义全局函数:
function getArraytest()
{
if (arguments.length > 0 )
{
alert( " js 对话框:您传递了参数。 " + arguments[ 0 ]);
return arguments[ 0 ];
}
else
{
alert( " js 对话框:无参数调用。 " );
return " js 函数返回值 " ;
}
}
function SetUserName()
{
alert(SilverlightPlugin.Content.SilverlightApplicationExample.InterInvole());
}
// 定义Silverlight插件对象
var SilverlightPlugin = null ;;
function pluginLoaded(sender)
{
SilverlightPlugin = sender.get_element();
}
// ]]>
</ script >
</ head >
< body style ="height: 100%; margin: 0;" >
< form id ="form1" runat ="server" >
< div style ="border: 2px solid #EEE; margin: 20px;padding:20px" >
请输入你的名字: < input id ="UserName" type ="text" value ="" />
< input type ="button" onclick ="SetUserName()" value ="将名字传递到 Silverlight" />
</ div >
< br />
< div style ="border: 2px solid #EEE;margin: 20px;" >
< asp:ScriptManager ID ="ScriptManager1" runat ="server" >
</ asp:ScriptManager >
< asp:Silverlight ID ="Xaml1" runat ="server" OnPluginLoaded ="pluginLoaded" Source ="~/ClientBin/SilverlightApplication1.xap" Version ="2.0" Width ="400px" Height ="300px" />
</ div >
</ form >
</ body >
</ html >
运行结果:
Silverlight 2学习教程(六): Silverlight托管代码调用Javascript中的JSON对象
xmlns
xmlns:x
Width
{
{
{
InitializeComponent();
}
{
{
UserInputContent = "Hello Silverlight World!";
}
{
UserInputContent = "你好,
}
HtmlWindow win = HtmlPage.Window;
win.Alert("Silverlight 里面弹出的对话框。
Object[] args = { "将此参数传递给
win.Invoke("getArrayTest", args);