[Silverlight probing] Using Silverlight to call Javascript

Source: Internet
Author: User

 

**************************************** ****************************
*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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.