Reputation: 1910
I'm developing a WPF application with only a WebBrowser that launch a HTML page with JavasCript. I also have some C# methods.
I'm looking for a good way to launch my C# methods in JavaScript, with parameters and if possible callbacks.
For now, I have JavaScript methods that navigate to special URL like "myApp://mymethod?param1=value¶m2=value" and in the "Navigating" event of the webBrowser, I check the URL and call the method that I need with the parameters.
This solution works but the size of the URL is limited. How can I send heavier data ? (custom headers data, POST, cookies, local storage or anything else I don't know... :) )
I can't use window.external because in the end I will use Xamarin to do a cross-platforms app. WebBrowser will be for Windows, but IOS use UIWebView that dosen't support window.external ( stevesaxon.me/posts/2011/… ) so i need a solution that works everywhere and the custom URL has a size limit.
If somebody has a solution...
Upvotes: 2
Views: 3043
Reputation: 89499
Please take a look at How to: Implement Two-Way Communication Between DHTML Code and Client Application Code
Basically you need to use window.external
object in your scripting code to access public properties and methods
HTML Code:
<button onclick="window.external.Test('called from script code')">
call client code from script code
</button>
C# Code in your WPF code:
public void Test(String message)
{
MessageBox.Show(message, "client code");
}
Communication between WPF and JavaScript requires full trust, so you will need to add following code:
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
[ComVisible(true)]
public class MainWindow
{
public void Test(String message)
{
MessageBox.Show(message, "client code");
}
}
Upvotes: 1