[IronPython] IronPython to JavaScript
Michael Foord
fuzzyman at voidspace.org.uk
Wed Jan 14 00:01:49 CET 2009
Ahhh... there is a detail I missed out in that article. You can only
hook up the Javascript function after the Silverlight control has
loaded. This is *possibly* the cause of your problems.
I do the hooking up in an onload function:
function onload() {
control =
document.getElementById('SilverlightPlugin');
control.Content.setCode.Event = setCode;
}
You pass this function in the online parameter in the Silverlight object
element:
<object id="SilverlightPlugin"
data="data:application/x-silverlight,"
type="application/x-silverlight-2" width="450" height="540">
<param name="source" value="app.xap"/>
<param name="onerror" value="onSilverlightError" />
<param name="onload" value="onload" />
Hopefully this sorts out the problem for you.
Michael Foord
Kristian Jaksch wrote:
> Thanks for the reply!
>
> No, I can't get it to work with Javascript either. I added the script
> below to my page:
>
> <script type="text/javascript">
> function some_function(sender, args) {
> value = args.val;
> alert(value);
> }
>
> control = document.getElementById("sl2");
> control.Content.event.Event = some_function; // THIS
> LINE GIVES AN ERROR
> </script>
>
> I'm not sure about the exact translation of the error message (It's in
> swedish) but it's something like:
>
> "Error: Invalid pointer"
>
> I guess it has something to do with the error I receive in my "app.py"
> file as I described previously.
>
> I made a minimal app.py file to test it again but I still receive the
> same error. This file also test to get a string from Javascript to
> Ironpython and that works just fine. Below is my complete file:
>
>
> import clr, sys
>
> clr.AddReferenceToFile("Scriptable.dll")
>
> from System.Windows import Application
> from System.Windows.Browser import *
> from System.Windows.Controls import *
> from System.Windows import Input
> from Scriptable import *
>
>
> class GetFromEditor(ScriptableForString):
> def _method(self, string):
> root.message.Text = string
> #return new_string
>
> GetFromEditor = GetFromEditor()
>
> root = Application.Current.LoadRootVisual(UserControl(), "app.xaml")
>
> HtmlPage.RegisterScriptableObject("GetFromEditor", GetFromEditor)
>
>
> event = ScriptableEvent()
>
> # This must also be registered
> HtmlPage.RegisterScriptableObject("event", event)
>
> args = ScriptableEventArgs()
> args.val = 'some string'
> event.OnEvent(args)
>
>
> It must be something I'm missing but I just can't see what it could
> be. Do you know of any good example were this approach is used?
>
> Thanks again!
>
>
> 2009/1/12 Michael Foord <fuzzyman at voidspace.org.uk>:
>
>> xkrja wrote:
>>
>>> I'm trying to call javascript from IronPython (via Silverlight 2) but I
>>> can'
>>> get it to work properly. I looked at this example:
>>>
>>> http://www.voidspace.org.uk/ironpython/silverlight/scriptable.shtml#id10
>>> http://www.voidspace.org.uk/ironpython/silverlight/scriptable.shtml#id10
>>> It's explained very clearly but when I try to run my code I get a System
>>> Error:
>>>
>>> SystemError: Object reference not set to an instance of an object.
>>>
>>> I just inserted the lines of code found on the example page in my
>>> IronPyton
>>> code:
>>>
>>> event = ScriptableEvent()
>>>
>>> # This must also be registered
>>> HtmlPage.RegisterScriptableObject("event", event)
>>>
>>> args = ScriptableEventArgs()
>>> args.val = 'some string'
>>> event.OnEvent(args) #This gives me a System Error
>>>
>>> I create a C# .dll as explained in the example and add a reference to it.
>>> The source is below:
>>>
>>> using System;
>>> using System.Windows.Browser;
>>>
>>> namespace Scriptable
>>> {
>>>
>>> [ScriptableTypeAttribute]
>>> public class ScriptableForString
>>> {
>>> [ScriptableMemberAttribute]
>>> public string method(string value)
>>> { return this._method(value); }
>>>
>>> public virtual string _method(string value)
>>> { return "override me"; }
>>> }
>>>
>>> [ScriptableType]
>>> public class ScriptableEvent
>>> {
>>> [ScriptableMember]
>>> public event EventHandler Event;
>>>
>>> public virtual void OnEvent(ScriptableEventArgs e)
>>> {
>>> Event(this, e);
>>> }
>>> }
>>>
>>> [ScriptableTypeAttribute]
>>> public class ScriptableEventArgs : EventArgs
>>> {
>>> private string _val;
>>>
>>> [ScriptableMemberAttribute]
>>> public string val
>>> {
>>> get { return _val; }
>>> set { _val = value; }
>>> }
>>> }
>>> }
>>>
>>> Can anyone explain what I'm doing wrong?
>>> Thanks for any help!
>>>
>>>
>>>
>> Hmmm... do you have any Javascript hooking up to the event? If you don't,
>> try hooking up to it and see if it makes any difference.
>>
>> The same code works for me. :-)
>>
>> Michael
>>
>> --
>> http://www.ironpythoninaction.com/
>> http://www.voidspace.org.uk/blog
>>
>>
>> _______________________________________________
>> Users mailing list
>> Users at lists.ironpython.com
>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>>
>>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
--
http://www.ironpythoninaction.com/
http://www.voidspace.org.uk/blog
More information about the Ironpython-users
mailing list