在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. IT综合学习网站收集

    最近整理了一下曾经使用过的IT从入门到广泛的综合类基础学习网站,记录下来,以便初学者使用: 1.http://www.w3school.com.cn/  中文版基础在线学习平台 2.http://ww ...

  2. No.002 Add Two Numbers

    Add Two Numbers Total Accepted: 160702 Total Submissions: 664770 Difficulty: Medium You are given tw ...

  3. SQO (标准查询运算符)方法 & Linq To Object

    #region SQO (标准查询运算符) 方法 #region Where() Find() FindAll() FirstOrDefault()等方法 static void c01where() ...

  4. procfs

    https://www.kernel.org/doc/Documentation/filesystems/proc.txt /proc/stat cpu 493610 1050 955506 6140 ...

  5. .NET Reflector反编译的方法

    首先启动.NET Reflector,然后添加进入dll或exe.然后选择Export Source Code...,将反编译后的代码文件,生成到指定目录. 到这一步骤时,稍等一会,就能够在指定目录就 ...

  6. druid parser

    有没有好用的开源sql语法分析器? - 匿名用户的回答 - 知乎 druid parser

  7. HTML中,按钮button与submit区别是什么?

    type=button 单纯是按钮功能:type=submit 是发送表单:既然是这样,那么能否全部用submit代替button? Sumit提交表单,Button需要绑定事件才可以用提交数据不可以 ...

  8. vim的.vimrc文件设置

    set nocompatibleset autowriteset autoreadset nobackupset noswapfile " --- syntax and indent --- ...

  9. Spring框架搭建遇到的问题汇总

    1.The resource is not on the build path of a Java project 然后把相应的依赖加入构建路径 2.Type mismatch: cannot con ...

  10. 1.7见识一下什么叫Linux驱动:LED

    1.任何的Linux驱动都有一个装载函数(装载驱动时调用)和一个卸载函数(卸载驱动时调用): 2.装载函数和卸载函数分别通过module_init和module_exit宏指定.