Take a list, do not want to use Silverlight 5来 to make a project, has never touched this thing before, in order to work, the bite to go. Groping for a night, big to sort out some of the things that are needed in the project, the following as a preliminary record:
Silverlight 5 How to talk to JavaScript
Silverlight 5 belongs to the client thing, the client side of something with the server to communicate, the first time to think of the JavaScript-based Ajax this universal glue, so first from the Silverlight 5 and JavaScript interaction approach.
One, Silverlight 5 speaks, JavaScript listens
从Silverlight 5里向Javascript发送程序运行请求。Silverlight 5端代码具体如下:
HtmlPage.Window.Invoke("Javascript函数方式""传值-Oyiboy");
With the above code, you can run JavaScript script code directly in Silverlight 5 and send the necessary data out.
Second, JavaScript speaks, Silverlight 5 listens
Javascript使用ajax获取服务器端数据后发送给Silverlight 5,以达到Silverlight 5与服务器端的交互效果。Silverlight 5端代码具体如下:
//设置值 [ScriptableMember()]//这行是关键,必须有这个javascript才能请求到这个方法 publicvoidsetVal(string D) { this.textView.Text = D; } //javascript主动要求返回值 [ScriptableMember()] publicstringreturnVal() { returnthis.textView.Text; }
Html代码调整:需要在Silverlight 5插件的object代码内里添加以下参数设置句,以达到插件在加载后获取siliverlight对象。
<param name="onLoad"value="siliverLoaded" />
Javascript代码具体如下:
//siliverlight Object varSiliverlightobj =NULL;//The Silverlight 5 onload event-triggered function set in the above HTML code function siliverloaded(sender, args) {Siliverlightobj = Sender.gethost (); }//The. Buttonset and. Buttonreu in the following code are two buttons with these classes, and the buttons are not written out in detail . //This is the Setval method that runs within Silverlight 5$(". Buttonset"). Click ( function () {SiliverlightObj.Content.Main.setVal ("JavaScript passed in value-oyiboy"); })//This is the ReturnVal method that runs within Silverlight 5$(". Buttonreu"). Click ( function () {Alert (SiliverlightObj.Content.Main.returnVal ()); });
The above several ways, the flexible use of the live basically completely solved the Silverlight 5 and server-side communication between, OK, although this article is the topic of Silverlight 5 and JavaScript, but the ultimate goal is Silverlight 5 and server-side interaction , Ajax is not a new thing anyway, so I skipped it.
Impressions: Through Silverlight 5 Object SiliverlightObj.Content.Main This big channeling thing to see, Siliverlightobj still can do more things, concrete also slowly groping, if later need to use, may also out of this description article, perhaps.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
A preliminary study of Silverlight 5 (C #)