**************************************** ****************************
*Copyright Notice
*
* This document uses the Creative Commons release. Please strictly abide by this authorization protocol.
* This article was first published onBlogThis statement is an integral part of this article.
* Author's network name: prodigal son
* Author EMAIL: Dayichen (at) 163.com
* Author's BLOG: Http: // Www. Cnblogs. Com/Walkingboy
*
**************************************** ****************************
[Silverlight probing] using Javascript to call silverlight
-Written by prodigal son @ cnblogs.com (07-06-20)
Abstract:
In the above [Silverlight probing] using Javascript to call silverlight, we have discussed how to use javascript to call the managed method of silverlight. This is relatively easier, because Silverlight is compatible with the DOM object method, but in turn it is more troublesome, because javascript designers cannot think of opening an interface for silverlight so early :)
Environment:
- Codename Orcas Beta1
- Silverlight 1.1 Alpha
- IE 6.0
Ideas:
Even though javascript objects cannot be directly called from Silverlight, we can only call up and down from DOM objects related to them. To implement interoperability, Silverlight must first provide an object for javascript operations, that is, the [Scriptable] label we mentioned in [Silverlight exploration] using Javascript to call silverlight methods, however, this alone is not enough. The scenario here is only the function definition provided by Silverlight, while the javascript instance function implementation is then called in Silverlight. Think about this scenario, which is a bit similar to what we used before. Yes, it is delegate (or use Event Events ). Now the scenario is similar. Let's test it and see if it works.
Compile the Silverlight hosting method:
[Scriptable] public partial class Page: Canvas {public Page () {// register the client instance WebApplication. current. registerScriptableObject ("LangZi", this);} public void Page_Loaded (object o, EventArgs e) {// Required to initialize variablesInitializeComponent (); // register a button event
BtnCallScript. MouseLeftButtonUp + = new MouseEventHandler (btnCallScript_MouseLeftButtonUp);} void btnCallScript_MouseLeftButtonUp (object sender, MouseEventArgs e) {if (CallScript! = Null) {// CallScript (this, new MyEventArgs ("called by server! "); CallScript (this, EventArgs. Empty) ;}// register the event definitions accessible to the Client [Scriptable] public event EventHandler CallScript ;}
Compared with the above, SayHello is only replaced with an Event. Other changes are not significant.
Client injection method to event:
function createSilverlight(){Sys.Silverlight.createObjectEx({source: "Page.xaml",parentElement: document.getElementById("SilverlightControlHost"),id: "SilverlightControl",properties: {width: "100%",height: "100%",version: "0.95",enableHtmlAccess: true},events: {onLoad: OnLoaded}});}function OnLoaded(sender,args){//debugger;sender.Content.LangZi.CallScript = OnCallScript;}function OnCallScript(sender, args){//debugger;alert("Running!");}
Expansion ideas:
The most common EventHandler is used here. Whether to support custom EventHandler or not is further studied.
Code tested in this article: SilverlightCallJavascript.rar