问题
I've used an object tag to load my Silverlight control because I want to be able to input html into a Sharepoint page using the Rich Text Editor. It looks like this:
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" id="SilverlightObject"
width="850" height="600">
<param name="source" value="ClientBin/LabsSurvey.xap"/>
<param name="onError" value="onSilverlightError" />
<param name="minRuntimeVersion" value="3.0.40624.0" />
<param name="autoUpgrade" value="true" />
<img src="ClientBin/InstallSilverlightLabsBanner.jpg" alt="Please Install Silverlight" />
<br />
<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0" style="text-decoration:none">
<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style:none"/>
</a>
</object>
I've use the Javascript bridge in other Silverlight apps, and it works great for two-way communication between the web page and the Silverlight control.
BUT - it seems that in order for this to work, I have to instanciate my Silverlight control using the .NET Silverlight control.
I can not get a javascript call to a method within my Silverlight control to work when I've used the object tag. I set it up exactly the same way as in my other apps where it does work - the only difference is that the control was not embedded the same way in the html.
Does anyone have any tips for me?
回答1:
It's also key to include the windowless parameter in your object definition in addition to the information called out in the answer from Kelsey. Without windowless=true, the call to Content will always return undefined....
Sample object definition:
<div id="silverlightControlHost">
<object id="silverlightObject" data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
<param name="source" value="ClientBin/LabsCharts.xap"/>
<param name="onError" value="onSilverlightError" />
<param name="background" value="white" />
<param name="windowless" value="true" />
<param name="minRuntimeVersion" value="3.0.40624.0" />
<param name="autoUpgrade" value="true" />
<a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0" style="text-decoration:none">
<img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style:none"/>
</a>
</object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>
VB.Net class to register scriptable object:
Imports System.Windows.Browser
_ Partial Public Class MainPage Inherits UserControl
Public Sub New()
' Required to initialize variables
InitializeComponent()
AddHandler Loaded, AddressOf MainPage_Loaded
End Sub
Protected Sub MainPage_Loaded(ByVal sender As Object, ByVal e As EventArgs)
HtmlPage.RegisterScriptableObject("MainPage", Me) 'not working!
Dim so As ScriptObject = TryCast(HtmlPage.Window.Eval("charts"), ScriptObject)
so.Invoke("registerSilverlight")
End Sub
Public Sub SayHi() MessageBox.Show("HI!!!!!") End Sub
End Class
And the javascript function, registerSilverlight
var charts = {
registerSilverlight: function() {
var func = document.getElementById('silverlightObject');
var content1 = func.Content;
content1.MainPage.SayHi();
}
}
回答2:
You might want to check out this thread:
How to call javascript function on a Silverlight 3 object?
I had problems as well and got it all working using the object tag.
来源:https://stackoverflow.com/questions/1639862/javascript-to-silverlight-when-silverlight-instantiated-with-object-tag