Reputation: 6510
I'm trying to access properties on the RootVisual object:
[ScriptableType]
public class ApplicationInfo
{
public string Name { get; set; }
public string Version { get; set; }
}
[ScriptableType]
public partial class MainPage : UserControl
{
[ScriptableMember]
public ApplicationInfo ApplicationInfo { get; set; }
public MainPage()
{
InitializeComponent();
this.ApplicationInfo = new ApplicationInfo();
this.ApplicationInfo.Name = "My Application";
this.ApplicationInfo.Version = "0.1";
HtmlPage.RegisterScriptableObject("myapp", this);
}
}
In my ASPX hosting page I've the following JavaScript snippet:
<script type="text/javascript">
function onPluginLoaded(plugin) {
alert('in plugin');
alert(plugin.Name); //This gives me the x:Name of the RootVisual object
var appInfo = plugin.ApplicationInfo;
alert(appInfo);
alert(plugin.myapp);
document.title = appInfo.Name + " " + appInfo.Version;
}
</script>
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%" OnPluginLoaded="onPluginLoaded" >
<param name="source" value="ClientBin/SLVersion.xap"/>
<param name="onload" value="onPluginLoaded" />
This doesn't work. I'd like to know why! Thanks in advance.
Upvotes: 2
Views: 807
Reputation: 189437
There are two things wrong.
Silverlight Documentation
The Silverlight documentation about this area of functionality is really quite confused. Here is thing, the object provided as the sender parameter in the onLoad method isn't what the documentation says that it is, it's not the silverlight plugin.
At least its not the plugin as seen by the HTML DOM / Javascript. It seems to be some form of the Javascript API version of a Framework element. In order to get the plugin object that is useful to us we need to call the getHost
method on it.
function onPluginLoaded(sender) {
var plugin = sender.getHost();
}
That gets us one step closer.
Accessing Registered Scriptable Objects
Scriptable objects that have been registered on HTMLPage
are accessed as properties of the Plugin's Content
property. Hence to access the ApplicationInfo
object you would need:-
function onPluginLoaded(sender) {
var plugin = sender.getHost();
var appInfo = plugin.Content.myapp.ApplicationInfo;
alert(appInfo.Name + " " + appInfo.Version);
}
That'll get you going.
Remove [ScriptableType]
from MainPage
, in this case you only want to mark specific members as available to script hence you use the [ScriptableMember]
. By using [ScriptableType]
you expose all public members automatically as scriptable. You are correctly doing that on your ApplicationInfo
.
Upvotes: 4