Call Silverlight methods from Javascript by name

这一生的挚爱 提交于 2019-12-24 09:58:18

问题


Is it possible to get a reference to a Silverlight method purely by name from Javascript, and then invoke it? With pure Javascript objects you would be something like this:

var f = theObj["theMethodName"];    
f.call(theObj, "an arg");

But treating a Silverlight object as an associative array doesn't seem work.

I'm guessing I could probably use Eval as a last resort, but I'd rather avoid it.


回答1:


The question is on how to call a Silverlight function from Javascript by name. You can easily call methods on an object directly by enabling a method for scripting using the ScriptableMember attribute, but you can't invoke it as a string directly.

I think you're stuck with eval.




回答2:


HtmlPage.Window.Invoke("theMethodName", "An arg");

OR

var obj = HtmlPage.Document.GetElementByID("theObj"); obj.Invoke("theMethodName", "an Arg");

...

Ah, re-reading it...no, no access to the reflection API. You'd have to expose it formally. Its still a managed object...just exposed as an 'object' in JScript. So not the same as a prototype object.




回答3:


This works:

theObj["theMethodName"]("an arg");    

But this does not:

theObj["theMethodName"].apply(null, "an arg");

at least I didn't manage to use apply (and call) :(



来源:https://stackoverflow.com/questions/3963487/call-silverlight-methods-from-javascript-by-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!