Created
December 3, 2015 20:28
-
-
Save podrezo/5c84eb6407e94fab80cf to your computer and use it in GitHub Desktop.
Interacting with Embedded Chromium and Embedded IE in .NET/WPF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<body> | |
<script> | |
function recieveMessage(message) { | |
document.write(message); | |
} | |
// Chrome | |
if(myObject && myObject.sendMessage) { | |
myObject.sendMessage('hello, I am Chromium!'); | |
} | |
// Internet Explorer | |
if(window.external && window.external.SendMessage) { | |
myObject.sendMessage('hello, I am Internet Explorer!'); | |
} | |
</script> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public partial class MainWindow : Window | |
{ | |
public MainWindow() | |
{ | |
InitializeComponent(); | |
} | |
private void Window_Loaded(object sender, RoutedEventArgs e) | |
{ | |
// Initialize Internet Explorer | |
var ieScriptingObject = new ScriptingObject(); | |
embeddedInternetExplorer.Navigate('/path/to/index.html'); | |
embeddedInternetExplorer.ObjectForScripting = ieScriptingObject; | |
// Initialize Chromium | |
var cefScriptingObject = new ScriptingObject(); | |
embeddedChromium.RegisterJsObject("myObject", cefScriptingObject); | |
embeddedChromium.Address = '/path/to/index.html'; | |
// Tell them both hello from WPF | |
embeddedInternetExplorer.InvokeScript("recieveMessage", "Hello from WPF!"); | |
CefSharp.WebBrowserExtensions.ExecuteScriptAsync(embeddedChromium, "recieveMessage('Hello from WPF!')"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ComVisible(true)] | |
public class ScriptingObject | |
{ | |
// This method is invoked from the webview | |
public void SendMessage(string message) | |
{ | |
// Do something here with the message | |
Debug.WriteLine(message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment