Y
Youraputz
Is it possible to invoke silverlight functions directly from a
WinForm, via the WebBrowser control? The short answer is yes, and
here is the code I'm using as a reference:
Form1.cs (contains WebBrowser with its url set to the silverlight
page)
button_Click(object sender, EventArgs e)
{
// invoke the javascript function on the page
webBrowser.Document.InvokeScript("jsChangeBackground");
}
SilverlightApp.aspx (contains the javascript and silverlight program)
function jsChangeBackground(){
document.getElementById("SL").Content.Bridge.ChangeBackground
();
}
...
<body>
...
<object data="data:application/x-silverlight-2,"
type="application/x-silverlight-2" width="100%" height="100%" id="SL">
...
</body> etc.
and finally MainPage.xaml.cs which exposes the silverlight method to
java script
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
// register a scriptable object
HtmlPage.RegisterScriptableObject("Bridge", this);
}
[ScriptableMember]
public void ChangeBackground()
{
this.LayoutRoot.Background = new SolidColorBrush
(Colors.Red);
}
Now all of this works perfectly, When I click a button on my form the
background color of the silverlight grid changes to red, the problem I
have with it is the need to write a new java script function for every
silverlight method I want my webBrowser control to invoke. Is it
possible to bypass the java script portion entirely? I've been trying
things like:
webBrowser.Document.GetElementById("SL").InvokeMember
("Bridge.ChangeBackground");
but nothing seems to work.
WinForm, via the WebBrowser control? The short answer is yes, and
here is the code I'm using as a reference:
Form1.cs (contains WebBrowser with its url set to the silverlight
page)
button_Click(object sender, EventArgs e)
{
// invoke the javascript function on the page
webBrowser.Document.InvokeScript("jsChangeBackground");
}
SilverlightApp.aspx (contains the javascript and silverlight program)
function jsChangeBackground(){
document.getElementById("SL").Content.Bridge.ChangeBackground
();
}
...
<body>
...
<object data="data:application/x-silverlight-2,"
type="application/x-silverlight-2" width="100%" height="100%" id="SL">
...
</body> etc.
and finally MainPage.xaml.cs which exposes the silverlight method to
java script
private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
// register a scriptable object
HtmlPage.RegisterScriptableObject("Bridge", this);
}
[ScriptableMember]
public void ChangeBackground()
{
this.LayoutRoot.Background = new SolidColorBrush
(Colors.Red);
}
Now all of this works perfectly, When I click a button on my form the
background color of the silverlight grid changes to red, the problem I
have with it is the need to write a new java script function for every
silverlight method I want my webBrowser control to invoke. Is it
possible to bypass the java script portion entirely? I've been trying
things like:
webBrowser.Document.GetElementById("SL").InvokeMember
("Bridge.ChangeBackground");
but nothing seems to work.