在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中的方法的更多相关文章

  1. VC中调用COM组件的方法(转载)

    原文参考:http://hi.baidu.com/mingyueye/item/53ebecd44da76917d80e4449 总结一下在VC中调用COM组件的方法 准备及条件: COM服务器为进程 ...

  2. (转)DedeCms Runphp 标签中调用其他变量的方法

    DedeCms Runphp 标签中调用其他变量的方法 我们都知道,在DedeCMS中是可以使用PHP的,常见的方法就是if else了,例如模板制作中,我们需要对来源和作者进行判断,如果为空,则提示 ...

  3. vc中调用Com组件的方法详解

    vc中调用Com组件的方法详解 转载自:网络,来源未知,如有知晓者请告知我.需求:1.创建myCom.dll,该COM只有一个组件,两个接口:   IGetRes--方法Hello(),   IGet ...

  4. vue 在methods中调用mounted中的方法?

    首先可以在data中先声明一个变量 比如 isShow=' ' mounted 中 ---> methods 中 --->  this.sureDelBox(item) 直接this调用 ...

  5. 在JS中调用CS里的方法(PageMethods)

    在JS中调用CS里的方法(PageMethods) 2014年04月28日 11:18:18 被动 阅读数:2998   最近一直在看别人写好的一个项目的源代码,感觉好多东西都是之前没有接触过的.今天 ...

  6. 在Android Studio中调用so中的方法

    本节用的so是上节用Android Studio创建的so.想在Android Studio中调用so中的方法,需要先引用so.Android Studio中引用so的方法有二种,下面开始介绍. 一 ...

  7. 关于controller中调用多个service方法的问题

    一般service方法是有事务的,把所有操作封装在一个service方法中是比较安全的. 如果在controller中调用多个service方法,只有查询的情况下是可以这样的.

  8. 在C++中调用DLL中的函数 (3)

    1.dll的优点 代码复用是提高软件开发效率的重要途径.一般而言,只要某部分代码具有通用性,就可将它构造成相对独立的功能模块并在之后的项目中重复使用.比较常见的例子是各种应用程序框架,ATL.MFC等 ...

  9. 在C++中调用DLL中的函数(3)

    1.dll的优点 代码复用是提高软件开发效率的重要途径.一般而言,只要某部分代码具有通用性,就可将它构造成相对独立的功能模块并在之后的项目中重复使用.比较常见的例子是各种应用程序框架,ATL.MFC等 ...

随机推荐

  1. OC基础(17)

    ARC基本概念 ARC快速入门 ARC下的内存管理 ARC和MRC兼容和转换 *:first-child { margin-top: 0 !important; } body > *:last- ...

  2. Redis多机功能之Sentinel

    Sentinel的目的:监视主从服务器,并在主服务器下线时自动进行故障转移 启动Sentinel 通过执行Redis安装文件中的redis-sentinel程序,可以启动一个Sentinel实例: r ...

  3. IBM Appscan基本操作手册

    一.操作前提 1.首先下载Appscan的安装包 2.安装Appscan 二.操作流程 1.双击图标,打开Appscan软件 2.打开软件后,页面显示如下: 3.选择“文件-新建”,弹出如下的窗口: ...

  4. 【PL/SQL练习】DML语句的处理(可以处理多行数据)

    1.Insert (在表中插入一行数据,并查看) SQL> desc t1; Name Type Nullable Default Comments ---- ------------ ---- ...

  5. .NET程序与CA对接一直提示重定向

    最可能问题:应用程序服务器与CA服务器时间不同步 解决方法: 打开时间,选择internet时间,操作如图,在.net程序服务器 输入CA服务器的IP ,或者CA服务器输入 .net程序部署服务器的I ...

  6. 学习总结 java 异常

    package com.hanqi.ec; public class Test1 { public static void main(String[] args) { int a = 10 , b = ...

  7. 学习练习 java 程序设计园的周长面积

    编写一个Java程序,计算半径为3.0的圆周长和面积并输出结果. 注:系统类Math位于java.lang包中,圆周率π可以由Math类的静态属性PI得到,其定义为“public static fin ...

  8. 洛谷P1206 [USACO1.2]回文平方数 Palindromic Squares

    P1206 [USACO1.2]回文平方数 Palindromic Squares 271通过 501提交 题目提供者该用户不存在 标签USACO 难度普及- 提交  讨论  题解 最新讨论 暂时没有 ...

  9. 揭秘TPM安全芯片技术及加密应用

    揭秘TPM安全芯片技术及加密应用 首发:http://safe.it168.com/a2012/0912/1396/000001396884.shtml 从2003年开始,重要数据丢失已经成为严重的信 ...

  10. 【qt4.8.6】qt-everywhere-opensource-src-4.8.6静态库编译,搭建vs2010 + Qt4.8.6环境

    公司的电脑上无法运行QtCreator, 又想用Qt,只能搞vs2010+Qt了, 看到运行时要链接一个几M到十几M的QtCore.dll和QtGui.dll,又有一种在用C#写的程序的感觉,很不爽, ...