Skip to main content
Home Forums Silverlight Programming Programming with .NET - General Copying HtmlBridge Example - "Trying to get unsupported property on scriptable plugin object!"
10 replies. Latest Post by luisabreu on July 20, 2007.
(0)
fuzzyman
Member
44 points
19 Posts
07-19-2007 5:20 PM |
Hello all,
Sorry for a long post, but I've been banging my head against this one and can't see what I'm doing wrong. I'm trying to access managed code from Javascript (I'm actually trying to do it from IronPython and have a *very* odd error, so trying to get a working C# example first).
I'm trying to understand the HtmlBridge2 example and reproduce the bare essentials of creating a Scriptable object that I can call from BLOCKED SCRIPT
http://silverlight.net/QuickStarts/Dom/ManagedCodeAccess.aspx
My C# is a simple class, marked as Scriptable with a Scriptable method that returns a string. Like the example I extend Canvas (is this necessary for a Scriptable object ?).
When I call my method from C# I get the following error: "Trying to get unsupported property on scriptable plugin object!"
I can't find the source of the problem :-(
Many thanks in advance for any help.
The C# is:
using System;using System.Windows;using System.Windows.Media;using System.Windows.Controls;using System.Windows.Browser;namespace ScriptableClass{ [Scriptable] public class Class : Canvas { public Class() {} [Scriptable] public string method() { return this._method(); } public string _method() { return "hello"; } public void OnLoad(object sender, EventArgs e) { try { WebApplication.Current.RegisterScriptableObject("basic", this); } catch (Exception ex) { string err = ex.Message; } } }}
The Xaml is (nice and red!):
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" x:Class="ScriptableClass.Class;assembly=CSharpScriptable.dll" x:Name="rootCanvas" Height="500" Width="500" Loaded="OnLoad" Background="Red" ></Canvas>
The Javascript is:
function createSilverlight(){ Sys.Silverlight.createObjectEx( { source: 'Page2.xaml', parentElement: test_element, id: 'agControl1', properties: {width: '100', height: '100', background: 'white', isWindowless: 'false', framerate: '24', version: '0.95.0'}, events: {onError: null, onLoad: null}, context: null } );}
The relevant bit of the HTML is:
<div id="SilverlightControlHost" class="silverlightHost"> <script type="text/javascript"> test_element = document.getElementById("SilverlightControlHost"); createSilverlight(); </script> </div> <div id="test2">Random text</div> <div id="test3">Random text 2</div> <script type="text/javascript"> test_element = document.getElementById("test2"); control = document.getElementById("agControl1"); test_element.innerHtml = control.Content.basic.method(); </script>
All the best,
Michael Foord
huanwu
152 points
32 Posts
07-19-2007 6:09 PM |
luisabreu
Participant
1676 points
612 Posts
07-19-2007 6:15 PM |
hello.
well, i think you should keep in mind 2 thins:
1. you should only call those script lines (the last of your post) after the load event of the silverlight control has been fired. if you need to get that info during the rendering of the page, add a handler to the onload event of the silverlight control (take a look at the help for the silverlight.js file that is on the sdk)
2. i believe that you're after innerHTML and not innerHtml since js is case-sensitive.
07-19-2007 6:21 PM |
That doesn't seem to make any difference. Also the HTMLBridge2 example has a class name of "ScriptingCanvas" and registers with the name "basic".
Unfortunately the HTMLBridge examples doen't ship with a project (just prebuilt dlls) so I haven't (yet) built them - I should try that I guess. I'm starting to suspect my compilation environment, if you get a chance to test my code it would be really appreciated (if you have the time of course - I know what it's like).
07-19-2007 6:31 PM |
Hello luisabreu,
Thanks for the reply. Thanks for the heads-up about 'innerHTML'. That on its own isn't sufficient to fix the problem of course.
As for adding a handler - can you give me a link to the help file. I'm afraid I'm being brain dead and can't find the appropriate part. The tutorial page makes no mention of doing this - although I guess it makes sense that you can't call into managed code until the SilverLight Control has loaded and marked registered the scriptable method.
Michael
07-19-2007 6:37 PM |
Yes, luisabreu is right. If you put it in a seperate function, and call it after silverlight control has been loaded, it works.
07-19-2007 6:41 PM |
Cool. Sorry to be a pain - could you give me a brief example (or link) of how to hook up to that event on the Javascript side. The tutorial pages don't mention because they all trigger code from user actions (like a button).
Thanks
07-19-2007 6:44 PM |
Ah wait - I pass it in in the createSilverlight function! I'll try that. Sorry to be a nuisance and thanks for your help guys.
07-19-2007 6:47 PM |
<input id="button01" type="button" value="Call Managed Code From Java Script" onclick = "test()"/>
<script type="text/javascript"> function test() { var control = document.getElementById('AgControl1'); var s = control.Content.basic.method(); alert(s);
}
</script>
07-19-2007 6:49 PM |
And it works - great (that was a quick response).
Now to try the same thing from the IronPython side - although I'm completely failing to add a reference to the assembly there so I think the problem is different... (it is failing before it gets to any of that code...)
07-20-2007 4:01 AM |
hello again.
i'm late as usual :)
well, you just need to be sure that yuo're calling the method after the onload event of the control is generated. if you need to call it at the end of the load, then you just need to add a valid method to the onLoad property you're passing on the events to the createObjectEx.