跨域可编写脚本的 Silverlight 应用程序
我在使用 Silverlight 托管跨域应用程序时遇到问题。
我有一个托管在不同域上的应用程序,并在页面中包含以下 HTML 代码:
<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"/>
</object>
但是,如果 app.xap
应用程序托管在与此 HTML 代码不同的域上,则 onLoad succ
函数是不带参数调用,因此它会记录以下行:
SUCCESS
undefined
undefined
如果我在同一域上托管,它将记录正确的行:
SUCCESS
UserControl {}
undefined
所以在第一种情况下,我无法从 javascript 访问 [ScriptableMember]
带注释的方法,因为我没有任何对应用程序的引用。
在 AppManifest.xml
文件中,我包含了 HtmlPage.RegisterScriptableObject
方法所需的属性,如下所示:
<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>
xap
文件通过 传输>application/x-silverlight-app
Content-Type
,所以这也不是问题。
我缺少什么?
谢谢!
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"/>
</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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设您已经注册了一个名为“Thingy”的外部对象。
您应该能够通过 html 对象元素的
Content
属性访问它。将id="mySL"
放在对象标记上。现在尝试以下代码:-只需确保在代码中尽早注册“Thingy”,就像在用作根视觉对象的类的构造函数中一样。
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 anid="mySL"
on the object tag. Now try this code:-Just be sure to register "Thingy" early in your code like in the constructor of the class you use as the root visual.
同样的问题,并按照主题启动者在评论中描述的方式解决
Same issue, and solved as topic starter described in his comment