跨域可编写脚本的 Silverlight 应用程序

发布于 2024-09-29 23:34:35 字数 1862 浏览 4 评论 0原文

我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦言归人 2024-10-06 23:34:35

假设您已经注册了一个名为“Thingy”的外部对象。

您应该能够通过 html 对象元素的 Content 属性访问它。将 id="mySL" 放在对象标记上。现在尝试以下代码:-

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

只需确保在代码中尽早注册“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 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.

嘿哥们儿 2024-10-06 23:34:35

同样的问题,并按照主题启动者在评论中描述的方式解决

我必须将所有可编写脚本的代码放入我的根视觉类

Same issue, and solved as topic starter described in his comment

I had to put all of my scriptable code to my root visual class

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文