How do you join events in ScriptObject in Silverlight?

HtmlObject provides all the necessary functions for registering event handlers for script and DOM events, but what if the class you need to listen to does not exist as a DOM element and the script variable (reference to ScriptObject) instead?

+3
source share
2 answers

As pointed out by AnthonyWJones, Silverlight cannot bind to JavaScript events. The right thing in this situation is to do the following:

Enable script access in Silverlight:

  • Mark a class using the ScriptableType attribute or marking specific methods with ScriptableMember
  • HtmlPage.RegisterScriptableObject .

Silverlight, JavaScript:

  • JavaScript
  • document.getElementById, Silverlight
  • .Content.. JavaScript. , silverlight.Content.Page.UpdateText().

, JavaScript, JavaScript Silverlight.

+1

javascript . , , .

?

ScriptObject SetProperty, , , Managed , Javascript.

, .

//Javascript in web page.
var myObj = new Thing();

function Thing()
{
     this.doStuff = function()
     {
         if (this.onstuff) this.onstuff("Hello World");
     }
}

// C# code in a Silverlight app.

class SomeClass
{
    private ScriptObject myObject;
    public SomeClass(ScriptObject theObject)
    {
         myObject = theObject;
         myObject.SetProperty("onstuff", (Action<string>)onstuff);
    } 

    function void onstuff(string message)
    {
         //Do something with message
    }

}
+1

All Articles