-
-
Save perf2711/e96e69abad24c254d0192e88eb705709 to your computer and use it in GitHub Desktop.
Async method calling in CefSharp through bound objects
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
<html> | |
<body> | |
<script src='index.js'></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
(async function() { | |
await CefSharp.BindObjectAsync("TestBoundObject"); | |
})(); |
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
<Window x:Class="CefTests.MainWindow" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | |
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | |
xmlns:local="clr-namespace:CefTests" | |
xmlns:cefSharp="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf" | |
mc:Ignorable="d" | |
Title="MainWindow" Height="450" Width="800" KeyUp="Window_KeyUp"> | |
<Grid> | |
<cefSharp:ChromiumWebBrowser Name="TestBrowser" Address="http://localhost:8000" /> | |
</Grid> | |
</Window> |
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(); | |
TestBrowser.JavascriptObjectRepository.Register("TestBoundObject", new TestBoundObject(), true); | |
} | |
private void Window_KeyUp(object sender, System.Windows.Input.KeyEventArgs e) | |
{ | |
if (e.Key == Key.F12) | |
{ | |
TestBrowser.ShowDevTools(); | |
} | |
} | |
} |
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 class TestBoundObject | |
{ | |
public string SyncMethod() | |
{ | |
return nameof(SyncMethod); | |
} | |
public Task<string> AsyncMethod1() | |
{ | |
return Task.FromResult(nameof(AsyncMethod1)); | |
} | |
public async Task<string> AsyncMethod2() | |
{ | |
return nameof(AsyncMethod2); | |
} | |
public async Task<string> AsyncMethod3() | |
{ | |
await Task.Delay(1000); | |
return nameof(AsyncMethod3); | |
} | |
public string SyncMethod1() | |
{ | |
return AsyncMethod1().Result; | |
} | |
public string SyncMethod2() | |
{ | |
return AsyncMethod2().Result; | |
} | |
public string SyncMethod3() | |
{ | |
return AsyncMethod3().Result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment