
Inbound Calls from JavaScript to Silverlight
The ExternalCallersFromCrossDomain attribute controls JavaScript and DOM access to scriptable objects that are defined in cross-domain .xap files.
The attribute has no effect in same-domain .xap files. Access is disabled by default in cross-domain .xap files.
The attribute is set on the root element of the application manifest, as shown in the following Silverlight deployment code.
<Deployment xmlns="http://schemas.microsoft.com/client/2007"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
EntryPointAssembly="MyAppAssembly"
EntryPointType="MyNamespace.MyApplication"
ExternalCallersFromCrossDomain="ScriptableOnly"
>
<Deployment.Parts>
<AssemblyPart Source="MyAppAssembly.dll” />
<AssemblyPart Source="MyUserControl.dll" />
</Deployment.Parts>
</Deployment>
The ExternalCallersFromCrossDomain attribute accepts two values: ScriptableOnly and NoAccess.
When the attribute is set to ScriptableOnly, the host's native JavaScript can only access scriptable objects that the Silverlight application code registers with the runtime. It cannot query or set properties on any other objects. Additionally, it will not receive event notifications.
Caution: |
|---|
The ScriptableOnly value will allow a cross-domain .xap file to programmatically call any scriptable objects that are explicitly exposed by the application. |
When the ExternalCallersFromCrossDomain attribute is set to NoAccess, scriptable entry points and creatable types are not available to JavaScript or DOM through Content and createObject. Although managed calls to RegisterScriptableObject, RegisterCreateableType, and so on will appear to succeed (exceptions to managed code are not thrown), the registered entry points and types will not be available through Content or createObject.
Application Manifest
The application manifest is created by default when you build your Silverlight project. If you are using Visual Studio, you can disable manifest generation by clearing the Generate Silverlight manifest file check box in the Properties window, Silverlight tab. (To open the Properties window, click Properties on the Project menu.)
If manifest generation is disabled, you will not be able to set the ExternalCallersFromCrossDomain attribute.
ExternalCallersFromCrossDomain Workarounds
When the ExternalCallersFromCrossDomain attribute is set to NoAccess, direct access to Silverlight managed code is not possible. However, individual, specific access can be programmatically re-established if the following conditions are true:
The Silverlight plug-in's enableHtmlAccess property is set to true.
Silverlight managed code calls a JavaScript function and passes one or more managed objects as input parameters to the Invoke, InvokeSelf, and SetProperty methods.
The managed instances passed in the previous step have scriptable properties, methods, or events, and the objects have been registered for scriptable access by using the RegisterScriptableObject method.
Note: |
|---|
These conditions cannot occur by accident. They require explicit steps by the cross-domain Silverlight-based application to pass managed objects to the host's JavaScript. |
How to Retrieve the ExternalCallersFromCrossDomain Value
You can get the current value of the ExternalCallersFromCrossDomain attribute from the ExternalCallersFromCrossDomain read-only property. This property returns a CrossDomainAccess value that indicates the access level of cross-domain callers.