javascript call silverlight
-
Wednesday, November 11, 2009 3:29 AM
hi
I want javascript code call silverlight method.but my code dosen't work."window.frames["find"].document.all[objectId].content" is an object. but then "window.frames["find"].document.all[objectId].content.image" ,unknown exception is coming here .
here is my code.
javascript like this:
use javascript to embed silverlight to aspx
function loadSilverlight(parentObject,objectId) {
Silverlight.createObject(
"/ClientBin/SilverlightApplication2.xap",
parentObject, objectId,
{ width: "100%", height: "100%", background: "white", version: "3.0.40620.0" }, "", "page=1", "");
}function setImage(data,num) {
for (var i = 0; i < num; i++) {
var objectId = "object" + i.toString();
window.frames["find"].document.all[objectId].content.image.setImage(height, width, i, data);}
}APP.xmal.cs like this
private void Application_Startup(object sender, StartupEventArgs e)
{
if (e.InitParams.Count != 0)
{
if (e.InitParams["page"] == "1")
{
this.RootVisual = new page1();
HtmlPage.RegisterScriptableObject("image", this.RootVisual);
}
}
else
{
this.RootVisual = new MainPage();
HtmlPage.RegisterScriptableObject("test", this.RootVisual);
}
}thank you.
All Replies
-
Thursday, November 12, 2009 2:53 AM
Hi my understanding is tou want to call silver light code-behind from javascript code.
Here is the sample
In javascript function
function test
{
var SLPlugin = document.getElementById(your plugin id);
SLPlugin.Content.SilverlightPlugin.Sayhello("Hello");
}
In page.xaml.cs
HtmlPage.RegisterScriptableObject("SilverlightPlugin", this); - inside constructor
[ScriptableMember]
public void Sayhello(string yourvalue)
{
//your logic
}
so on call of test javascript function, from there it will trigger sayhello function
-
Thursday, November 12, 2009 3:19 AM
Hi,
Check this tutorial as well-
http://msdn.microsoft.com/en-us/library/cc221414(VS.95).aspx
Thanks,
Pravin
"Please mark as answered, if this answers your question"
-
Thursday, November 12, 2009 9:28 AM
thanks again!
I find another question,when i embed silverlight Plugin to aspx page using tag <object> like follow, javascript function trigger silverlight code,but when using javascript to embed silverlight Plugin to aspx page ,javascript function does not work.
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="25%" height="25%" name="test1">
<param name="source" value="ClientBin/SilverlightApplication1.xap"/>
<param name="onerror" value="onSilverlightError" />
<param name="background" value="white" />
<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="获取 Microsoft Silverlight" style="border-style: none"/>
</a>
</object> -
Friday, November 13, 2009 12:39 AM
Hi,
Can you share that JavaScript function which embed Silverlight PlugIn?
Thanks,
Pravin
-
Friday, November 13, 2009 12:43 AM
Also take a look on "How to: Add Silverlight to a Web Page by Using JavaScript". Below is a Link for the same-
http://msdn.microsoft.com/en-us/library/cc265155(VS.95).aspx
Thanks,
Pravin
"Please mark as answered, if this answers your question"
-
Friday, November 13, 2009 3:41 AM
here is function,but aspx page must Load ,before function works.
function loadSilverlight(parentObject, objectId, context) {
Silverlight.createObject("/ClientBin/SilverlightApplication2.xap",
parentObject, objectId, { width: "100%", height: "100%" }, null, "page=1", null
);
}thanks
-
Friday, November 13, 2009 4:43 AM
Well,
In this case you can call the Javascript function on some event. For example - Button Click event or Body load event. Something like that.
Also try debugging the page - http://msdn.microsoft.com/en-us/library/cc838267(VS.95).aspx
Thanks,
Pravin
"Please mark as answered, if this answers your question"
-
Friday, November 13, 2009 6:52 AM
In fact ,I want to pass data from one control to another.local connection is not applicable.
public partial class MainPage : UserControl
{public MainPage()
{
InitializeComponent();
}[ScriptableMember]
public void imageset()
{HtmlPage.Window.Eval("test("+test+")");
}
}
other control
[ScriptableType]
public partial class MainPage : UserControl
{[ScriptableMember]
public void setImage()
{}
}
fuction test(test){
loadSilverlight(parentObject,objectId);
}
function loadSilverlight(parentObject,objectId) {
Silverlight.createObject(
"/ClientBin/SilverlightApplication2.xap",
parentObject, objectId,
{ width: "100%", height: "100%", background: "white", version: "3.0.40620.0" }, "", "page=1", "");
}function setImage(data,num) {
for (var i = 0; i < num; i++) {
var objectId = "object" + i.toString();
window.frames["find"].document.all[objectId].content.image.setImage(height, width, i, data); (here Application_Startup is triggered ,but I can't use image to call setImage method)
}
}thank you again.
-
Friday, November 13, 2009 7:09 AM
Hi,
Check the answer of this thread. This is what I was talking about -
http://forums.silverlight.net/forums/p/131775/297967.aspx
Thanks,
Pravin
"Please mark as answered, if this answers your question"
-
Sunday, November 15, 2009 8:16 PM
yes I do as you say .sliverlight plug-in can embed to page,but management code still can't be called by javascript instance which registed in xmal.cs. Please give me some advice。
yu
thank you!
-
Monday, November 16, 2009 12:29 AM
Hi,
I recommend you to register the object after it's initialized. Here is how i do it:
[ScriptableType] public partial class MainPage : UserControl {public MainPage() { InitializeComponent(); HtmlPage.RegisterScriptableObject("Person", this); } [ScriptableMember] public void setColor() { rec.Fill = new SolidColorBrush(Colors.Blue); // rec is a rectangle in my xaml } }
My JS function:
function bclick() {
var sl = document.getElementById('sltest');
sl.Content.Person.setColor();
}
function createSL(s) {
Silverlight.createObjectEx({
source: s,
parentElement: document.getElementById("silverlightControlHost"),
id: "sltest",
properties: {
width: "640",
height: "480",
background: "white",
alt: "<!--not installed-->",
version: "3.0.40624.0"
},
events: {
},
initParams: "param1=1",
context: "row4"
});
}My Aspx page:
<form id="form1" runat="server" style="height:100%"> <input type="button" onclick="bclick()" /> <input type="button" onclick="createSL('ClientBin/TabControl.xap')" /> <div id="silverlightControlHost" style="width:640px; height:500px" ></div> </form>
I have tested this program, it worked fine for me.
Best Regards

