Silverlight和JavaScript通信

Silverlight和JavaScript通信,javascript,silverlight,Javascript,Silverlight,我在Silverlight和JavaScript之间进行通信时玩得很开心 在page.xaml.cs文件中,我将该类标记为ScriptableType,并将两个方法标记为ScriptableMember。我随后宣布: HtmlPage.RegisterScriptableObject("Page", this); 当我尝试调用任一方法时,我得到的obj.Content.Page未定义: var obj = document.getElementById('silverlightControl'

我在Silverlight和JavaScript之间进行通信时玩得很开心

在page.xaml.cs文件中,我将该类标记为ScriptableType,并将两个方法标记为ScriptableMember。我随后宣布:

HtmlPage.RegisterScriptableObject("Page", this);
当我尝试调用任一方法时,我得到的obj.Content.Page未定义:

var obj = document.getElementById('silverlightControl');
alert(obj);               // [object HtmlObjectElement]
alert(obj.Content);       // Content
alert(obj.Content.Page);  // obj.Content.Page is undefined
alert(obj.Content.Page.GetRegion());
另一方面,在page.xaml.cs中的方法内部,我调用:

HtmlPage.Window.Invoke("mapRegionChanged", GetRegion());
我在JavaScript中有一个各自的方法,该方法从未被调用:

function mapRegionChanged(region) {
    alert("Region changed: " + region);
}
Silverlight对象声明如下:

<object data="data:application/x-silverlight-2," type="application/x-silverlight-2"
    width="151px" height="77px" id="silverlightControl">
    <param name="onload" value="regionsLoaded" />
    <param name="source" value="<%= Url.Content("~/ClientBin/Worldmap.xap") %>" />
    <param name="onerror" value="onSilverlightError" />
    <param name="background" value="white" />
    <param name="minRuntimeVersion" value="2.0.31005.0" />
    <param name="autoUpgrade" value="true" />
    <param name="enableHtmlAccess" value="true" />
    <a href="http://go.microsoft.com/fwlink/?LinkID=124807" style="text-decoration: none;">
        <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style: none" />
    </a>
</object>

UserControl在XAML中定义为:

<UserControl
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="Worldmap.Page"
Width="151" Height="77" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d">
<Grid x:Name="LayoutRoot" Background="White" Width="151" Height="77" Cursor="Hand" MouseMove="OnMouseMove" MouseLeftButtonUp="OnMouseUp" MouseLeave="OnMouseLeave">


如果您发现FireFox和IE之间存在不兼容,我们需要报告。可能是页面上JS中的其他内容,或者可能是FF3中的设置(比如flashblock类型的东西)

我遇到了这个问题。Firefox是我的默认浏览器,因此是VisualStudio(实际上是VWD,但无所谓)启动的浏览器。在开发过程中,我向客户端页面添加了JavaScript调用代码,发现Silverlight组件的“内容”子成员在FireFox的DOM中找不到——但在IE中可以找到。我重新启动了Visual Studio web服务器(系统托盘中的服务器),嘿,presto——它在FireFox中工作!这个问题似乎是由那些web服务器中的Silverlight组件缓存引起的。

几天前,我阅读了《php architect》杂志,其中有一篇关于Silverlight的文章,解释了如何与javascript通信,您可能想查看一下

去注册,这样你就可以下载杂志,然后你必须下载2009年5月号,所有其他的问题都必须付费,但我的帐户中的这一期是免费的,我希望你的也是免费的,如果你不能下载,请告诉我,我可以通过电子邮件向你发送PDF

这里是下载它的直接链接:

致以最良好的祝愿,
Alexandru

确保启用HTML访问:

在C#中:

在HTML中:

<param name="enablehtmlaccess" value="true" />


这对我来说很有效。

很显然,它在IE7中有效,但在Firefox 3.0.5中不起作用。我以为我们已经通过了Silverlight的浏览器大战?你在javascript控制台中看到错误了吗?没有,错误控制台中没有错误。@steve,请将互动程序编辑为“Silverlight和javascript通信-在FireFox中”。。更合适的做法是…在处理silverlight项目时,我使用WebDeveloper工具栏禁用Firefox中的缓存。我总是有缓存问题,否则。
<param name="enablehtmlaccess" value="true" />