[IronPython] IronPython to JavaScript
Kristian Jaksch
kristian.jaksch at gmail.com
Wed Jan 14 13:15:16 CET 2009
Thanks again for the reply.
I must be getting something seriously wrong. I did what you said and I
don't get any Javascript error but I still get the same error as
before from Ironpython:
"SystemError: Object reference not set to an instance of an object."
I looked at the source code of your Web IDE and tried to understand
how you did it there. You are hooking up a button click with the
SetExample() function and that works just fine but if I try to call
the SetExample() directly from the "_method()" function in the
RefreshExamples class, then I get the same error as above. Below I
show what I mean (it's your code a bit modified):
setCode = ScriptableEvent()
load = Load()
def SetExample(name):
args = ScriptableEventArgs()
args.code = 'TESTING'
setCode.OnEvent(args) #SystemError: Object reference not set to
an instance of an object.
class RefreshExamples(Scriptable):
def _method(self):
SetExample('browserDOM') #THIS DOESN'T WORK (It makes the
call but gives the error above)
refresh = RefreshExamples()
HtmlPage.RegisterScriptableObject("codeChange", codeChange)
HtmlPage.RegisterScriptableObject("refresh", refresh)
HtmlPage.RegisterScriptableObject("load", load)
HtmlPage.RegisterScriptableObject("setCode", setCode)
# Default example
HtmlPage.Document.code.value = examples.browserDOM
root = Canvas()
refresh._method()
Application.Current.RootVisual = root
Have I misunderstood this completely or shouldn't it be possible to
directly call the SetExample()? Please bear in mind that I've only
been programming Ironpython a couple of months :-)
Mustn't these errors only depend on IronPython and have nothing to do
with the JavaScript? I mean, first is the Silverlight app built, and
this error occurs at that time so Javascript can't have any part in
the errors?
I know it's much to ask for but would it be possible for me to send
you the minimalistic test website I have so you maybe can have a quick
look at it? Or if you maybe have a as small as possible project I can
have a look at.
Again, thanks for all your help!
2009/1/14 Michael Foord <fuzzyman at voidspace.org.uk>:
> 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
>
>
> _______________________________________________
> Users mailing list
> Users at lists.ironpython.com
> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
>
More information about the Ironpython-users
mailing list