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等 ...
随机推荐
- cocos2dx lua 绑定之二:手动绑定自定义类中的函数
cococs2dx 3.13.1 + vs2013 + win10 1.首先按照<cocos2dx lua 绑定之一:自动绑定自定义类>绑定Student类 2.在Student类中增加一 ...
- FlashBuilder的快捷键
Ctrl-F11: 执行(Run) F11: 除错(Debug) Ctrl-Alt-Down: 重复目前所在编辑列(Repeat current line ) Alt-Up: 移动本列,或选择列往上移 ...
- Dll学习(二)__declspec用法详解
__declspec用于指定所给定类型的实例的与Microsoft相关的存储方式.其它的有关存储方式的修饰符如static与extern等是C和 C++语言的ANSI规范,而__declspec是一种 ...
- socket学习笔记——并发服务器与I/O程序分割客户端
client.c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <u ...
- App交互demo
Android <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" ...
- c# 将页面导出到word(含图片及控件)
/// <summary> /// 创建word /// <param name="filePath">文件路径 </param> /// &l ...
- CLRS:build_max_heap(strorage in array)
//用满二叉树存储,从n/2处开始递归向上调整(n/2后均为叶子节点,无需调整)使得根最大 //满二叉树顺序存储,左子2i,右子2i+1: #include<stdio.h>#includ ...
- linux 内存使用
# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda1 50G 1.9G 45G 5% / tmpfs 1.9G 0 1.9G 0% ...
- ENVI二次开发模式下的Landsat数据读取
从usgs网站或者马里兰大学下载TM或Landsat原始数据,数据可能包括9个tif数据,两个txt文件和一个gtf文件.示例结构如下: ENVI下可以直接打开*_MTL.txt文件打开,打开后波段列 ...
- 【MySQL】MHA部署与MasterFailover代码分析
官网:https://code.google.com/p/mysql-master-ha/ 参考:http://blog.csdn.net/wulantian/article/details/1328 ...