-2

In my Silverlight app, I have aWebBrowser component displaying random HTML content.

I would like to call C# methods from there (for example when I click on a [a href...] link).

Edit [add details] :

I will try to clarify my problem, sorry for the initial question badly asked :

In my XAML file, I have my webbrowser component :

<WebBrowser  x:Name="HtmlMail" Grid.Row="1" Visibility="{Binding Visibility}" />

In the code behind, I have a method that load HTML in the webbrowser (HTML is from an external source) :

void _viewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{
string encoded = SilverMUT.Common.Tools.CString.ToHtml(_viewModel.MailSelected.Message);
HtmlMail.NavigateToString(encoded);
}

What I would like to do is add for example a button to the html view (this part is ok) and that a click on the button trigger a C# function.

Edit : if I can trap the click on links from within the WebBrowser and handle them in the code behind, this would be ok too

I've try in : System.Windows.Navigation.LoadCompletedEventHandler but can't make it works

Geoffrey
  • 145
  • 9
  • [maybe this is of use](http://stackoverflow.com/questions/8800764/how-to-intercept-when-user-click-on-a-link-in-a-webbrowser), I found it by doing a Google search - something I suggest you learn to do yourself – musefan Mar 11 '13 at 16:20
  • 2
    Not sure about SL but from a forms app you can use objectforscripting+window.external; http://msdn.microsoft.com/en-us/library/a0746166.aspx – Alex K. Mar 11 '13 at 16:20
  • Please clarify if you want to call host's method (likely Alex K suggestion would work) or server side code. (Also I've changed title - feel free to revert) – Alexei Levenkov Mar 11 '13 at 16:28
  • @musefan : this example is for Windows Phone, but I could use the same mechanism to solve my problem (but there is no such thing as NavigatingEventArgs in Silverlight) – Geoffrey Mar 12 '13 at 10:15
  • @AlexK. : objectforscripting is not available – Geoffrey Mar 12 '13 at 10:21

1 Answers1

0

I used the following code:

WebBrowser.ScriptNotify += new EventHandler(WebBrowser_ScriptNotify);

And in the javascript I added:

window.external.notify(param);

Thanks @musefan !

Storm
  • 684
  • 9
  • 20
Geoffrey
  • 145
  • 9