html中调用silverlight中的方法
在xaml页面中放置一个textblock控件来绑定数据
<ItemsControl x:Name="cityname">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding CityName}" />
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
实体类:
public class MyData
{
public string MyName { get; set; }
public string CityName { get; set; }
public double Longatuide { get; set; }
public MyData(string name,string cityname, double atuide)
{
CityName=cityname;
MyName = name;
Longatuide = atuide;
}
}
后他代码:
public MainPage()
{
InitializeComponent();
Myinfos("China");
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
HtmlPage.RegisterScriptableObject("showmessage",this); //注册要想浏览器公开的对象,然后才可以从js调用
}
[ScriptableMember] //使用ScriptableMemberAttribute标记那些成员可被脚本化
public void Myinfos(string strwhere)
{
List<MyData> datas = GetMyInfo(strwhere);
cityname.ItemsSource = datas;
}
public List<MyData> GetMyInfo(string strwhere)
{
List<MyData> datas = new List<MyData>();
switch(strwhere)
{
case "China":
{
datas.Add(new MyData("danche","中国",12233));
break;
}
case "America":
{
datas.Add(new MyData("金泰熙","韩国",20013));
break;
}
}
return datas;
}
现在在silverlight宿主html中添加按钮:
</div>
<div id="JScontrol" style="width:400px; height:76px; left:0px;top:200px; position:absolute">
<input type="button" id="America" value="btnAmercia" onclick="callMyinfos('America');" />
<input type="button" id="China" value="btnChina" onclick="callMyinfos('China');" />
</div>
函数:
<script type="text/javascript">
function callMyinfos(country) {
var mycontroldiv = document.getElementById("mycontrol");
mycontroldiv.content.showmessage.Myinfos(country);//此处的showmessage就是在silverlight后台注册的key
}
</script>
完整代码:
<!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>
<title>silverlightviahtmlandjs</title>
<style type="text/css">
html, body { height: 100%; overflow: auto; }
body { padding: 0; margin: 0; }
#silverlightControlHost { height: 100px; width:200px; text-align:center; }
</style>
<script type="text/javascript" src="Silverlight.js"></script>
<script type="text/javascript">
function onSilverlightError(sender, args) {
var appSource = "";
if (sender != null && sender != 0) {
appSource = sender.getHost().Source;
}
var errorType = args.ErrorType;
var iErrorCode = args.ErrorCode;
if (errorType == "ImageError" || errorType == "MediaError") { return; }
var errMsg = "Silverlight 应用程序中未处理的错误 " + appSource + "\n" ;
errMsg += "代码: "+ iErrorCode + " \n";
errMsg += "类别: " + errorType + " \n";
errMsg += "消息: " + args.ErrorMessage + " \n";
if (errorType == "ParserError") {
errMsg += "文件: " + args.xamlFile + " \n";
errMsg += "行: " + args.lineNumber + " \n";
errMsg += "位置: " + args.charPosition + " \n";
}
else if (errorType == "RuntimeError") {
if (args.lineNumber != 0) {
errMsg += "行: " + args.lineNumber + " \n";
errMsg += "位置: " + args.charPosition + " \n";
}
errMsg += "方法名称: " + args.methodName + " \n";
}
throw new Error(errMsg); }
</script>
<script type="text/javascript">
function callMyinfos(country) {
var mycontroldiv = document.getElementById("mycontrol");
mycontroldiv.content.showmessage.Myinfos(country);
}
</script>
</head>
<body>
<form id="form1" runat="server" style="height:100%">
<div id="silverlightControlHost">
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%" id="mycontrol">
<param name="source" value="ClientBin/silverlightviahtmlandjs.xap"/>
<param name="onError" value="onSilverlightError" />
<param name="background" value="white" />
<param name="minRuntimeVersion" value="4.0.50401.0" />
<param name="autoUpgrade" value="true" />
<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50401.0" style="text-decoration:none">
<img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="获取 Microsoft Silverlight" style="border-style:none"/>
</a>
</object>
<iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe>
</div>
<div id="JScontrol" style="width:400px; height:76px; left:0px;top:200px; position:absolute">
<input type="button" id="America" value="btnAmercia" onclick="callMyinfos('America');" />
<input type="button" id="China" value="btnChina" onclick="callMyinfos('China');" />
</div>
</form>
</body>
</html>
html中调用silverlight中的方法的更多相关文章
- VC中调用COM组件的方法(转载)
原文参考:http://hi.baidu.com/mingyueye/item/53ebecd44da76917d80e4449 总结一下在VC中调用COM组件的方法 准备及条件: COM服务器为进程 ...
- (转)DedeCms Runphp 标签中调用其他变量的方法
DedeCms Runphp 标签中调用其他变量的方法 我们都知道,在DedeCMS中是可以使用PHP的,常见的方法就是if else了,例如模板制作中,我们需要对来源和作者进行判断,如果为空,则提示 ...
- vc中调用Com组件的方法详解
vc中调用Com组件的方法详解 转载自:网络,来源未知,如有知晓者请告知我.需求:1.创建myCom.dll,该COM只有一个组件,两个接口: IGetRes--方法Hello(), IGet ...
- vue 在methods中调用mounted中的方法?
首先可以在data中先声明一个变量 比如 isShow=' ' mounted 中 ---> methods 中 ---> this.sureDelBox(item) 直接this调用 ...
- 在JS中调用CS里的方法(PageMethods)
在JS中调用CS里的方法(PageMethods) 2014年04月28日 11:18:18 被动 阅读数:2998 最近一直在看别人写好的一个项目的源代码,感觉好多东西都是之前没有接触过的.今天 ...
- 在Android Studio中调用so中的方法
本节用的so是上节用Android Studio创建的so.想在Android Studio中调用so中的方法,需要先引用so.Android Studio中引用so的方法有二种,下面开始介绍. 一 ...
- 关于controller中调用多个service方法的问题
一般service方法是有事务的,把所有操作封装在一个service方法中是比较安全的. 如果在controller中调用多个service方法,只有查询的情况下是可以这样的.
- 在C++中调用DLL中的函数 (3)
1.dll的优点 代码复用是提高软件开发效率的重要途径.一般而言,只要某部分代码具有通用性,就可将它构造成相对独立的功能模块并在之后的项目中重复使用.比较常见的例子是各种应用程序框架,ATL.MFC等 ...
- 在C++中调用DLL中的函数(3)
1.dll的优点 代码复用是提高软件开发效率的重要途径.一般而言,只要某部分代码具有通用性,就可将它构造成相对独立的功能模块并在之后的项目中重复使用.比较常见的例子是各种应用程序框架,ATL.MFC等 ...
随机推荐
- HTTP与HttpServlet
(1).HTTP协议 Web浏览器和服务器通过HTTP协议在Internet上发送和接收消息.HTTP是一种基于请求/响应模式的协议.客户端发送一个请求,服务器端返回对该请求响应. . (2).HTT ...
- Ant 脚本打印系统属性变量、ant内置属性
Ant 脚本打印系统属性变量.ant内置属性 作用 编写ant脚本的时候,经常会引用到系统属性,本脚本用于打印系统常用属性(System.getProperties)与环境变量(Environment ...
- Win2D 官方文章系列翻译 - 处理设备丢失
本文为个人博客备份文章,原文地址: http://validvoid.net/win2d-handling-device-lost/ “设备丢失”是指 GPU 设备失效无法继续进行渲染的情况.GPU ...
- Spring 3.0以后版本的定时任务
自主开发的定时任务工具,spring task,可以将它比作一个轻量级的Quartz,而且使用起来很简单,除spring相关的包外不需要额外的包,而且支持注解和配置文件两种 <beans xml ...
- C++ 中的返回值
C++中大致有三种返回值:值拷贝(副本),值引用和指针,返回什么类型的值要根据当时情况而定. 如果返回的是大型对象的副本,那么在每一次的函数调用后返回,都会调用该对象类型的拷贝构造函数构造一个新的副本 ...
- 设置Eclipse自动跳转到debug模式的小技巧
默认情况下,eclipse中右键debug,当运行到设置的断点时会自动跳到debug模式下.但由于我的eclipse环境,从开始一直用到现在,中间包括装.卸各种插件,更换版本,从英文界面导到中文界面又 ...
- Material Design参考资料
传送门: http://www.uisdc.com/comprehensive-material-design-note
- PAT1013
#include<cstdio>#include<cstring>#include<vector>using namespace std;const int max ...
- 开源项目:网页实时通信WebRTC
参考资料 [博客系列] Android WebRTC 音视频开发总结 [环境编译] WebRTC入门指南 各平台WebRTC源码网盘下载 Ubuntu14.04编译WebRTC For Android ...
- 无法完成安装:'unsupported configuration: hda-duplex not supported in this QEMU binary'
Linux 有问必答:如何修复"hda-duplex not supported in this QEMU binary" 编译自:http://ask.xmodulo.com/h ...