views:

16

answers:

1

I'm having problems with cross-domain application hosting with Silverlight.

I have an application hosted on a different domain and included with the following HTML code to the page:

<script type="text/javascript">
function succ( sender, args ) {
    console.log("SUCCESS");
    console.log(sender);
    console.log(args);
}
function err( sender, args ) {
    console.log("FAILURE");
    console.log(sender);
    console.log(args);
}
</script>
<object width="400" height="20" id="app" type="application/x-silverlight-2" data="data:application/x-silverlight-2,">
    <param name="minruntimeversion" value="4.0.41108.0"/>
    <param name="autoupgrade" value="false"/>
    <param name="onerror" value="err"/>
    <param name="onload" value="succ"/>
    <param name="enablehtmlaccess" value="true"/>
    <param name="source" value="http://example.com/app.xap"/&gt;
</object>

But if the app.xap application is hosted on a different domain from this HTML code, the onLoad succ function is called without arguments, so it logs the following lines:

SUCCESS
undefined
undefined

If i host on the same domain it logs the correct lines:

SUCCESS
UserControl {}
undefined

So in the first case I could not reach the [ScriptableMember] annotated methods from javascript because I don't have any reference to the application.

In the AppManifest.xml file, I included the attribute needed by the HtmlPage.RegisterScriptableObject method as this:

<Deployment xmlns="http://schemas.microsoft.com/client/2007/deployment"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            ExternalCallersFromCrossDomain="ScriptableOnly"
>
    <Deployment.Parts>
    </Deployment.Parts>
</Deployment>

The xap file is transferred with application/x-silverlight-app Content-Type, so this isn't the problem either.

What am I missing?

Thanks!

+1  A: 

Lets assume you've registered an external object named "Thingy".

You should be able to access it via the Content property of the html object element. Place an id="mySL" on the object tag. Now try this code:-

function succ( sender, args ) { 
    console.log("SUCCESS"); 
    console.log(document.getElementById("mySL").Content.Thingy); 
    console.log(args); 
} 

Just be sure to register "Thingy" early in your code like in the constructor of the class you use as the root visual.

AnthonyWJones