Skip to content

Commit 9f81788

Browse files
committed
JSB - Add Example to WinForms project
A very simple Multiply example
1 parent 0cab7a0 commit 9f81788

File tree

4 files changed

+78
-1
lines changed

4 files changed

+78
-1
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace CefSharp.MinimalExample.WinForms
2+
{
3+
public class AsyncJavascriptBindingClass
4+
{
5+
public double Multiply(double number1, double number2)
6+
{
7+
return number1 * number2;
8+
}
9+
}
10+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<!DOCTYPE html>
2+
3+
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
4+
<head>
5+
<meta charset="utf-8" />
6+
<title>Async Javascript Binding Example</title>
7+
</head>
8+
<body>
9+
<div>
10+
<h1>Async Binding Example</h1>
11+
<h2>Multiply</h2>
12+
<div>
13+
<table>
14+
<thead>
15+
<tr>
16+
<td>First Number</td>
17+
<td>Second Number</td>
18+
<td>Result</td>
19+
</tr>
20+
</thead>
21+
<tbody>
22+
<tr>
23+
<td><input id="firstNumber" type="text" value="4" /></td>
24+
<td><input id="secondNumber" type="text" value="5" /></td>
25+
<td><input id="result" type="text" readonly="readonly" /></td>
26+
</tr>
27+
<tr>
28+
<td colspan="3"><input type="button" id="calculate" value="Calculate" /></td>
29+
</tr>
30+
</tbody>
31+
</table>
32+
</div>
33+
</div>
34+
<script type="text/javascript">
35+
(async function ()
36+
{
37+
await CefSharp.BindObjectAsync("boundAsync");
38+
39+
document.getElementById('calculate').addEventListener('click', async function ()
40+
{
41+
var number1 = parseInt(document.getElementById('firstNumber').value, 10);
42+
var number2 = parseInt(document.getElementById('secondNumber').value, 10);
43+
44+
var result = await boundAsync.multiply(number1, number2);
45+
46+
document.getElementById('result').value = result;
47+
});
48+
})();
49+
</script>
50+
</body>
51+
</html>

CefSharp.MinimalExample.WinForms/BrowserForm.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
44

55
using System;
6+
using System.IO;
67
using System.Windows.Forms;
78
using CefSharp.MinimalExample.WinForms.Controls;
89
using CefSharp.WinForms;
@@ -20,10 +21,11 @@ public BrowserForm()
2021
Text = "CefSharp";
2122
WindowState = FormWindowState.Maximized;
2223

23-
browser = new ChromiumWebBrowser("www.google.com")
24+
browser = new ChromiumWebBrowser("http://cefsharp.demo/AsyncJavascriptBindingDemo.html")
2425
{
2526
Dock = DockStyle.Fill,
2627
};
28+
browser.RegisterResourceHandler("http://cefsharp.demo/AsyncJavascriptBindingDemo.html", File.OpenRead("AsyncJavascriptBindingDemo.html"));
2729
toolStripContainer.ContentPanel.Controls.Add(browser);
2830

2931
browser.IsBrowserInitializedChanged += OnIsBrowserInitializedChanged;
@@ -32,6 +34,14 @@ public BrowserForm()
3234
browser.StatusMessage += OnBrowserStatusMessage;
3335
browser.TitleChanged += OnBrowserTitleChanged;
3436
browser.AddressChanged += OnBrowserAddressChanged;
37+
browser.JavascriptObjectRepository.ResolveObject += (sender, e) =>
38+
{
39+
var repo = e.ObjectRepository;
40+
if (e.ObjectName == "boundAsync")
41+
{
42+
repo.Register("boundAsync", new AsyncJavascriptBindingClass(), isAsync: true);
43+
}
44+
};
3545

3646
var bitness = Environment.Is64BitProcess ? "x64" : "x86";
3747
var version = String.Format("Chromium: {0}, CEF: {1}, CefSharp: {2}, Environment: {3}", Cef.ChromiumVersion, Cef.CefVersion, Cef.CefSharpVersion, bitness);

CefSharp.MinimalExample.WinForms/CefSharp.MinimalExample.WinForms.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
<Reference Include="System.Windows.Forms" />
6565
</ItemGroup>
6666
<ItemGroup>
67+
<Compile Include="AsyncJavascriptBindingClass.cs" />
6768
<Compile Include="BrowserForm.cs">
6869
<SubType>Form</SubType>
6970
</Compile>
@@ -109,6 +110,11 @@
109110
<ItemGroup>
110111
<None Include="Resources\chromium-256.png" />
111112
</ItemGroup>
113+
<ItemGroup>
114+
<Content Include="AsyncJavascriptBindingDemo.html">
115+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
116+
</Content>
117+
</ItemGroup>
112118
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
113119
<Import Project="..\packages\CefSharp.Common.65.0.1\build\CefSharp.Common.targets" Condition="Exists('..\packages\CefSharp.Common.65.0.1\build\CefSharp.Common.targets')" />
114120
<Import Project="..\packages\CefSharp.WinForms.65.0.1\build\CefSharp.WinForms.targets" Condition="Exists('..\packages\CefSharp.WinForms.65.0.1\build\CefSharp.WinForms.targets')" />

0 commit comments

Comments
 (0)