简体   繁体   中英

Sending data from C# to Javascript in a WPF application

I have a WPF application. In MainWindow.xaml.cs, there is an object named Person. Person has "Name" field. In MainWindow.xaml, I have a window.That window goes to a webpage that I created. I want to see the "Name" of the Person in that webpage. How can I pass the info in C# to JavaScript page? Thanks.

You can set the web browser "source" property dynamically in you C# code so you should be able to pass information to the website using the querystring.

For example if your xaml is:

<WebBrowser x:Name="webBrowser" Source="" />

You can set the source using:

webBrowser.Source = new Uri("http://www.google.com/?myval=value");
webBrowser1.InvokeScript("JavaScriptfunctionName", new Object[] { Parameter1, Parameter2});

You can also use Awesomium which even as a WPF control built-in. It's every easy to you. Check the this link: http://wiki.awesomium.net/

Then you can call any JS functions from C# and pass parameters to it. The call would then look like this:

JSValue param = new JSValue("a name");
webView.CallJavascriptFunction("test", "displayName", param);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM