CNET中国旗舰网站
ZDNet China
|
CNET科技资讯网
|
政府采购
|
行业网站联盟
ZDNet China 至顶网是中国最大的企业级IT资源门户
首页
网络安全
存储
服务器
软件
桌面产品
开发
企业管理
白皮书
中小企业
新闻
下载
社区
企业网
无线网
过滤防护
安全管理
网络存储
磁带库/VTL
数据保护
光存储
X86服务器
虚拟化
小型机
操作系统
数据库
中间件
协作办公
手机
笔记本
数码相机
C/S开发
web开发
移动开发
web软件
应用软件
博客
论坛
搜索
搜索
高级搜索
用户登录
用户名:
密码:
登录
新用户注册
社区首页
未证实消息
桌面产品
软件
开发
网络与安全
服务器
存储
下载
苹果爱好者
中小企业
活动专区
病毒/木马
新知天下
CWEEK专属社区
悬赏问答
游客:
注册
|
登录
|
会员
|
搜索
|
论坛设施
|
帮助
ZDNetChina中文社区
»
Web开发
» JavaScript与Silverlight托管代码相互调用
C++开发常用工具荟萃
深入理解C语言指针的奥秘
Vista系统解惑要考虑的10件事
Asp.Net 学习资源列表
J2ME与Web Service的罗曼史
中间件技术的思想、概念、分类
.Net开发人员十种必备工具
用JSP 2.0开发Web应用程序
草根挑战豪门PHPer要为自己正名
高手写的php+flash多人视频聊天室
PHP程序员的优化调试技术和技巧
PHP程序员的优化调试技术和技巧
‹‹ 上一主题
|
下一主题 ››
投票
交易
悬赏
活动
打印
|
推荐
|
订阅
|
收藏
标题:
[转贴]
JavaScript与Silverlight托管代码相互调用
红模仿
(红模仿)
支柱会员
葡萄
UID 315664
精华
2
积分 12323
帖子 1520
威望 5564
ZD币 15147 元
阅读权限 210
注册 2008-4-1
来自 北京
状态 离线
楼主
发表于 2008-5-8 09:17
资料
个人空间
短消息
加为好友
开发者在线
JavaScript与Silverlight托管代码相互调用
要实现JavaScript调用Silverlight程序里面的托管代码,需要先在应用程序的启动(Application_Startup)事件里注册要进行访问的对象,而要从Silverlight的托管代码里访问HTML页面对象或者页面中的JavaScript,使用HtmlPage的Document/HtmlElement和HtmlWindow即可。
下面,我们就以例子来说明两者相互访问的方法,代码里面有很详细的注释,这里不再累述。
Page.xaml:
<
UserControl
x:Class
="SilverlightApplication1.Page"
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;
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;
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:
<%
@ Page Language
=
"
C#
"
AutoEventWireup
=
"
true
"
%>
<%
@ 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
>
运行结果:
[
本帖最后由 红模仿 于 2008-5-8 09:21 AM 编辑
]
1.gif
(30.51 KB)
2008-5-8 09:21
投票
交易
悬赏
活动
控制面板首页
编辑个人资料
积分交易
公众用户组
好友列表
个人空间管理
开通个人空间
基本概况
流量统计
客户软件
发帖量记录
论坛排行
主题排行
发帖排行
积分排行
在线时间
管理团队
管理统计
问卷调查
当前时区 GMT+8, 现在时间是 2009-7-4 13:38
Powered by Discuz! 5.5.0 © 2001-2007 Comsenz Inc.
Processed in 0.080258 second(s), 4/3 queries
TOP
清除 Cookies
-
联系我们
-
ZDNetChina中文社区
-
无图版