WebBrowser control to invoke silverlight

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.
 
M

miher

Youraputz said:
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.

Hi,

When I faced this I done the following:

- JavaScript is :
function execute(func) {
eval(func);
}
- managed calling code is :
webBrowser.Document.InvokeScript("execute", new object[]
{"document.getElementById(\"SL\").Content.Bridge.ChangeBackground();"});
The parameter string can be assembled by some helper function.

Hope You find this useful.
-Zsolt
 
G

Gregory A. Beamer

Is it
possible to bypass the java script portion entirely?

Silverlight 3 reduces some dependency on JavaScript, as you can code more
in the app code, but it is not a complete removal for all interactivity,
especially automated interactivity. You can reduce and/or eliminate
JavaScript for user interactivity, however, and not necessarily for
automation from the web browser control.

There might be a third party "browser" control that allows you better
access to "acting like a user", but I have never investigated this problem,
so I would not know where to begin.

You might find a full XBAP can solve the problem, but I would not be the
person to say it would definitely work, as I dropped XBAP after playing
with the first release. if something needs that much code with XAML
frontend, I would personally aim for WPF. Your mileage may vary. ;-)

Peace and Grace,

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top