Script and managed code in Silverlight

Source: Internet
Author: User
All cases

  • JS invoke managed code in Silverlight
  • Managed code in Silverlight invoke JS
  • JS invoke managed code out of Silverlight -- XMLHttpRequest, post back event, hidden button
  • Managed code out of Silverlight invoke JS -- registerstartupscript

1. js invoke managed code in Silverlight

 

Description: need to invoke function or property in managed code which is placed in Silverlight Project

Managed method in Silverlight:

public class Scriptable{    [ScriptableMember()]    public string ManagedCodeToUpper(string str)    {        return str.ToUpper();    }    [ScriptableMember()]    public string ManagedCodeProperty { get; set; }}

Then in APP. XAML. CS, you shoshould modify application_startup, register the class instance to script

private void Application_Startup(object sender, StartupEventArgs e){    this.RootVisual = new Page();    HtmlDocument doc = HtmlPage.Document;    //Enable the html textbox on the page and change the value    doc.GetElementById("Text1").SetProperty("disabled", false);    doc.GetElementById("Text1").SetAttribute("value", "Set from managed code");    //set up scriptable managed types for access from javascript    Scriptable scriptable = new Scriptable();    HtmlPage.RegisterScriptableObject("scriptKey", scriptable);    //HtmlPage.RegisterScriptableObject("scriptKeyStatic", Scriptable.ManagedCodeProperty);    //HtmlPage.RegisterCreateableType("scriptCreateableType", typeof(Scriptable));}

As abve we also can change the property or attribute here with htmldocument (system. Windows. browser)
And register the instance of class Scriptable to JS
After all of these, we can invoke these managed methods with JS

 

Don't forget to add the line below:

<Param name = "onLoad" value = "pluginloaded"/>

2. managed code in Silverlight invoke JS

Description: need to invoke a JS function from managed code and with the JS Function Change Dom's Value

The below JS function is to be invoked from SL managed code

 

Use htmldocument to attach an event to this button

private void Application_Startup(object sender, StartupEventArgs e){    this.RootVisual = new Page();    doc.GetElementById("button2").AttachEvent("click", new EventHandler(scriptable.CallGlobalJSMethod));}

The event handler is as follow

public void CallGlobalJSMethod(object sender, EventArgs e){    HtmlPage.Window.Invoke("JSFunction",                                         "public string ManagedCodeToUpper(string str)",                                        "{",                                                " return str.ToUpper();",                                        "}");}

 

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.