2

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&param2=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...

5
  • I have never used it but you can look for ObjectForScripting property on WebBrowser object.
    – Dmitry
    Commented Apr 13, 2014 at 19:46
  • Thanks, but I forgot to say that it should be multi-platform at the end (I will use Xamarin for IOS and android). Si i will have a "WebBrowser" on windows, a "WebView" on android, ... So I'm looking for a solution that will work everywhere...
    – Gab
    Commented Apr 13, 2014 at 20:09
  • 1
    Well i see only 2 possibilities (but sure there much more). Develop with phone gap or use websockets. There are some WebSocket implementations in C#. And you will be able to exchange data in json format. Here you have an article about Websockets and C# divyen.wordpress.com/2012/06/13/…
    – Dmitry
    Commented Apr 13, 2014 at 21:26
  • You can just build a low-end HTTP server into your code and host it on a local port. As Didier suggested, you could then use JSON to facilitate client/server data transfer. Commented Apr 13, 2014 at 23:42
  • Thanks again for the answers. In fact, I didn't want to use PhoneGap, but only implement a simple version of the PhoneGap principle. I will check Websockets
    – Gab
    Commented Apr 14, 2014 at 6:27

1 Answer 1

1

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");
    }
}
3
  • Thanks for the answer ! In fact, I didn't want to 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 needed a solution that works everywhere and the custom URL has a size limit
    – Gab
    Commented Apr 29, 2014 at 20:40
  • Ahh, I see. Yes, you are right for cross-platform this won't work. Commented Apr 30, 2014 at 2:15
  • No idea how I could do that ? Maybe by adding a custom header to the current page and read it in c#...
    – Gab
    Commented Apr 30, 2014 at 7:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.