Using web browser
control we can display the web content.If we want to call wpf function from the
javascript we have to use web browser’s ObjectForScripting property.In
the following example InvokeMeFromJavascript function is called from the javascript.
->Create new wpf
application and add web browser control.
<Grid>
<WebBrowser Name="wbMain"/>
</Grid>
[ComVisible(true)]
public class JavascriptInterface
{
//MainWindow object.
MainWindow objMainWindow;
/// </summary>
/// <param name="mainWindow">MainWindow object.</param>
public JavascriptInterface(MainWindow mainWindow)
{
//Get the MainWindow object.
this.objMainWindow = mainWindow;
}
/// <summary>
/// Called from the javascript.
/// </summary>
/// <param name="result"></param>
public void InvokeMeFromJavascript(string result)
{
//Display the result.
MessageBox.Show("Message from javascript: " + result);
}
}
->Create new ASP.Net application and add new html page.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
function invoke() {
//Call the web browser function.
window.external.InvokeMeFromJavascript('Hai...');
}
</script>
</head>
<body>
<input type="button" onclick="invoke();" value="Invoke" />
</body>
</html>
//Create the JavascriptInterface object.
JavascriptInterface objJavascriptInterface = new JavascriptInterface(this);
}
<Grid>
<WebBrowser Name="wbMain"/>
</Grid>
->Create new class JavascriptInterface and add new method InvokeMeFromJavascript.
[ComVisible(true)]
public class JavascriptInterface
{
//MainWindow object.
MainWindow objMainWindow;
/// <summary>
/// Constructor./// </summary>
/// <param name="mainWindow">MainWindow object.</param>
public JavascriptInterface(MainWindow mainWindow)
{
//Get the MainWindow object.
this.objMainWindow = mainWindow;
}
/// <summary>
/// Called from the javascript.
/// </summary>
/// <param name="result"></param>
public void InvokeMeFromJavascript(string result)
{
//Display the result.
MessageBox.Show("Message from javascript: " + result);
}
}
->Create new ASP.Net application and add new html page.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
function invoke() {
//Call the web browser function.
window.external.InvokeMeFromJavascript('Hai...');
}
</script>
</head>
<body>
<input type="button" onclick="invoke();" value="Invoke" />
</body>
</html>
->In window load event set the web
browser url to the above html page and set the ObjectForScripting
property.
private void Window_Loaded(object sender, RoutedEventArgs
e)
{//Create the JavascriptInterface object.
JavascriptInterface objJavascriptInterface = new JavascriptInterface(this);
//Set the
JavascriptInterface object to the web browser control.
this.wbMain.ObjectForScripting
= objJavascriptInterface;
//Set the
url to web browser control.
wbMain.Source = new Uri("Your page url…");}
Now you can call the wpf functions
from javascript.
Let me know, if you have any feedback. Mail me for source code. Enjoy reading my articles…
sekhartechblog@gmail.com
With your code can we navigate to html url and invoke from there instead of using browser control
ReplyDelete