Hi i want to interop WPF and silverlight inside WebBrowser control, explicitly passing data from silverlight control to WPF application how i do this? thanks
Hello, you must use JavaScript as a bridge. Here's a simple sample:
In the Silverlight Page's code behind, create a SriptableMember. Here we'll simply change the root Grid's color:
[ScriptableMember()]
public void ChangeColor(byte a, byte r, byte g, byte b)
{
try
{
Color c = Color.FromArgb(a, r, g, b);
LayoutRoot.Background = new SolidColorBrush(c);
}
catch
{
}
}
Modify Application_Startup, and regiester the Page instance as a ScriptableObject:
Something to note: If you're writing an XBAP, you must publish it to a server, so you run the XBAP from the http protocol. If you run an XBAP from local file system, you can't point WebBrowser to an http address. XBAP has similar security sandbox as Silverlight.
If you're writing a stand alone WPF application, this is not a problem.
If you're running a x64 OS, by default, a 64 bit browser will launch when you run your WPF application. Silverlight currently doesn't support 64 bit browsers (On x64 Windows, by default IE will run in the 32 bit mode, and there's no 64 bit Firefox at all,
so normally there's no problem). You must make your WPF application to target 32 bit platform (which can still run on a 64 bit OS). In the Properties Page, click the Build tab, and choose x86 for Platform target.
Finally, make sure your users have .NET 3.5 SP1. WebBrowser doesn't exist in earlier frameworks. You may also consider using a Windows Forms WebBrowser so the users only need to have .NET 3.0. But that version of WebBrowser can't be used in XBAP.
shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.
Hi, thanks for your answer, however i have a problem, the xap is a component i dont have control of, it's third party, i can handle some of the events of this control with javascript but i cant bubble them to WPF app with the WebBrowser, so is there any
other solution for this? thanks
Unfortunately no. You have to contact the third party vendor to modify their Silverlight application according to your needs.
shanaolanxing - I'll transfer to the Windows Azure team, and will have limited time to participate in the Silverlight forum. Apologize if I don't answer your questions in time.
WPF Interop? I am guessing you mean a COM exposed WPF application?
If this is what you mean, you should be able to instantiate your COM object on the page, and expose functionality to JScript. Once this is done, you can create scriptable members in SL aswell as create a HTML Bridge directly from SL.
I have done this using a custom class, allowing me to talk to the "page" frm within Silverlight.
using
System.Windows.Browser;
public
class
JScriptBridge
keoz2707
Member
1 Points
3 Posts
WebBrowser hosting XAP and WPF interop
Aug 05, 2008 06:27 PM | LINK
Hi i want to interop WPF and silverlight inside WebBrowser control, explicitly passing data from silverlight control to WPF application how i do this? thanks
Yi-Lun Luo -...
All-Star
25149 Points
2759 Posts
Microsoft
Re: WebBrowser hosting XAP and WPF interop
Aug 07, 2008 08:16 AM | LINK
Hello, you must use JavaScript as a bridge. Here's a simple sample:
In the Silverlight Page's code behind, create a SriptableMember. Here we'll simply change the root Grid's color:
[ScriptableMember()]
public void ChangeColor(byte a, byte r, byte g, byte b)
{
try
{
Color c = Color.FromArgb(a, r, g, b);
LayoutRoot.Background = new SolidColorBrush(c);
}
catch
{
}
}
Modify Application_Startup, and regiester the Page instance as a ScriptableObject:
private void Application_Startup(object sender, StartupEventArgs e)
{
Page page = new Page();
HtmlPage.RegisterScriptableObject("Page", page);
this.RootVisual = page;
}
In the hosting aspx page, create a JavaScript event handler for OnPluginLoaded:
<asp:Silverlight ... OnPluginLoaded="pluginLoaded" />
var slCtl = null;
function pluginLoaded(sender)
{
slCtl = sender.get_element();
}
Create a JavaScript function which in turn invokes the C# method in your Silverlight appliation:
function changeColor(a, r, g, b)
{
slCtl.Content.Page.ChangeColor(a, r, g, b);
}
Now in the WPF application, build a simple UI:
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="0.492*"/>
<RowDefinition Height="0.296*"/>
<RowDefinition Height="0.212*"/>
</Grid.RowDefinitions>
<WebBrowser x:Name="browser" Source="http://yourhostpage.aspx"/>
<Grid Grid.Row="1">
<Grid.RowDefinitions>
<RowDefinition Height="0.552*"/>
<RowDefinition Height="0.448*"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="0.137*"/>
<ColumnDefinition Width="0.363*"/>
<ColumnDefinition Width="0.144*"/>
<ColumnDefinition Width="0.356*"/>
</Grid.ColumnDefinitions>
<TextBox x:Name="TextA" Margin="0,10,0,10" Grid.Column="1"/>
<TextBlock HorizontalAlignment="Right" Margin="0,0,5,0" VerticalAlignment="Center" Text="A:" TextWrapping="Wrap"/>
<TextBlock HorizontalAlignment="Right" Margin="0,13.588,5,13.587" Text="R:" TextWrapping="Wrap" Grid.Column="2" d:LayoutOverrides="Height"/>
<TextBox x:Name="TextR" Margin="0,10,8,10" Grid.Column="3"/>
<TextBlock HorizontalAlignment="Right" Margin="0,13.588,5,13.587" Text="G:" TextWrapping="Wrap" Grid.Row="1" d:LayoutOverrides="Height"/>
<TextBox x:Name="TextG" Margin="0,3.874,0,8" Grid.Column="1" Grid.Row="1"/>
<TextBlock Margin="0,11.049,5,8" Text="B:" TextWrapping="Wrap" HorizontalAlignment="Right" Grid.Column="2" Grid.Row="1" d:LayoutOverrides="Width, Height"/>
<TextBox x:Name="TextB" Margin="0,3.874,8,8" Grid.Column="3" Grid.Row="1"/>
</Grid>
<Button Content="ChangeColor" Click="Button_Click" Width="100" Height="25" Grid.Row="2"/>
</Grid>
In the Button's Click event handler, you call the JavaScript function:
private void Button_Click(object sender, RoutedEventArgs e)
{
browser.InvokeScript("changeColor", TextA.Text, TextR.Text, TextG.Text, TextB.Text);
}
Something to note: If you're writing an XBAP, you must publish it to a server, so you run the XBAP from the http protocol. If you run an XBAP from local file system, you can't point WebBrowser to an http address. XBAP has similar security sandbox as Silverlight. If you're writing a stand alone WPF application, this is not a problem.
If you're running a x64 OS, by default, a 64 bit browser will launch when you run your WPF application. Silverlight currently doesn't support 64 bit browsers (On x64 Windows, by default IE will run in the 32 bit mode, and there's no 64 bit Firefox at all, so normally there's no problem). You must make your WPF application to target 32 bit platform (which can still run on a 64 bit OS). In the Properties Page, click the Build tab, and choose x86 for Platform target.
Finally, make sure your users have .NET 3.5 SP1. WebBrowser doesn't exist in earlier frameworks. You may also consider using a Windows Forms WebBrowser so the users only need to have .NET 3.0. But that version of WebBrowser can't be used in XBAP.
keoz2707
Member
1 Points
3 Posts
Re: WebBrowser hosting XAP and WPF interop
Aug 07, 2008 09:26 PM | LINK
Hi, thanks for your answer, however i have a problem, the xap is a component i dont have control of, it's third party, i can handle some of the events of this control with javascript but i cant bubble them to WPF app with the WebBrowser, so is there any other solution for this? thanks
Yi-Lun Luo -...
All-Star
25149 Points
2759 Posts
Microsoft
Re: Re: WebBrowser hosting XAP and WPF interop
Aug 11, 2008 12:47 PM | LINK
Unfortunately no. You have to contact the third party vendor to modify their Silverlight application according to your needs.
jparth
Member
28 Points
15 Posts
Re: Re: WebBrowser hosting XAP and WPF interop
Sep 22, 2008 01:53 PM | LINK
Hello,
I need to receive some event from the Silverlight application.
Is there any solution to communicate by this path SL-->JScript-->WPF ?
(I know how to make SL--> Jscript part [;)])
Thanks.
jparth
Member
28 Points
15 Posts
Re: Re: WebBrowser hosting XAP and WPF interop
Sep 23, 2008 07:34 AM | LINK
up [Y]
I don't find how to send data from Jscript in a HTML page in a WebBrowser control to the WPF application.
I want to avoid to make a polling process.
dotnutshell
Member
118 Points
64 Posts
Re: Re: WebBrowser hosting XAP and WPF interop
Sep 23, 2008 08:09 AM | LINK
WPF Interop? I am guessing you mean a COM exposed WPF application?
If this is what you mean, you should be able to instantiate your COM object on the page, and expose functionality to JScript. Once this is done, you can create scriptable members in SL aswell as create a HTML Bridge directly from SL.
I have done this using a custom class, allowing me to talk to the "page" frm within Silverlight.
using
System.Windows.Browser; public class JScriptBridge{
ScriptObject
root; private JScriptBridge(HtmlWindow window, System.Windows.Controls.UserControl control){
this.window = window; this.control = control; System.Windows.Browser.HtmlPage.RegisterScriptableObject("bridge", this); //next bit is a Jscript Objectroot =
this.window.GetProperty("myjscriptobject") as ScriptObject;//next bit is me invoking a Jscript function inside the abobe object
string _version = root.Invoke(
"GetVersion", "") as string;}
}
jparth
Member
28 Points
15 Posts
Re: Re: Re: WebBrowser hosting XAP and WPF interop
Sep 23, 2008 08:20 AM | LINK
Thank for the answer.
But It's not exactly what I mean (I Think my English is not good enought [:$])
I need Silverlight/WPF "two way" interrop.
I know how to send data this way : WPF --> JScript in wpf webBrowser control --> Silverlight control
I know how to send data this way Silverlight --> Jscript, but I don't know how doing the last part of my need : Jscript --> WPF
dotnutshell
Member
118 Points
64 Posts
Re: Re: Re: Re: WebBrowser hosting XAP and WPF interop
Sep 23, 2008 08:32 AM | LINK
The WPF application, is it a standard desktop application?
jparth
Member
28 Points
15 Posts
Re: Re: Re: Re: WebBrowser hosting XAP and WPF interop
Sep 23, 2008 09:54 AM | LINK
Yes.
As explain here http://dedjo.blogspot.com/2008/07/quick-silverlight-tip-how-to-access.html
and above, we can make an wpf--> jsscipt --> silverlight interrop,
and a silverlight --> jscript interrop.
it's missing a jscript-->wpf interrop