Skip to content

Commit

Permalink
JSB - Add Example to WinForms project
Browse files Browse the repository at this point in the history
A very simple Multiply example
  • Loading branch information
amaitland committed Sep 5, 2018
1 parent 0cab7a0 commit 9f81788
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
10 changes: 10 additions & 0 deletions CefSharp.MinimalExample.WinForms/AsyncJavascriptBindingClass.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace CefSharp.MinimalExample.WinForms
{
public class AsyncJavascriptBindingClass
{
public double Multiply(double number1, double number2)
{
return number1 * number2;
}
}
}
51 changes: 51 additions & 0 deletions CefSharp.MinimalExample.WinForms/AsyncJavascriptBindingDemo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Async Javascript Binding Example</title>
</head>
<body>
<div>
<h1>Async Binding Example</h1>
<h2>Multiply</h2>
<div>
<table>
<thead>
<tr>
<td>First Number</td>
<td>Second Number</td>
<td>Result</td>
</tr>
</thead>
<tbody>
<tr>
<td><input id="firstNumber" type="text" value="4" /></td>
<td><input id="secondNumber" type="text" value="5" /></td>
<td><input id="result" type="text" readonly="readonly" /></td>
</tr>
<tr>
<td colspan="3"><input type="button" id="calculate" value="Calculate" /></td>
</tr>
</tbody>
</table>
</div>
</div>
<script type="text/javascript">
(async function ()
{
await CefSharp.BindObjectAsync("boundAsync");

document.getElementById('calculate').addEventListener('click', async function ()
{
var number1 = parseInt(document.getElementById('firstNumber').value, 10);
var number2 = parseInt(document.getElementById('secondNumber').value, 10);

var result = await boundAsync.multiply(number1, number2);

document.getElementById('result').value = result;
});
})();
</script>
</body>
</html>
12 changes: 11 additions & 1 deletion CefSharp.MinimalExample.WinForms/BrowserForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.

using System;
using System.IO;
using System.Windows.Forms;
using CefSharp.MinimalExample.WinForms.Controls;
using CefSharp.WinForms;
Expand All @@ -20,10 +21,11 @@ public BrowserForm()
Text = "CefSharp";
WindowState = FormWindowState.Maximized;

browser = new ChromiumWebBrowser("www.google.com")
browser = new ChromiumWebBrowser("http://cefsharp.demo/AsyncJavascriptBindingDemo.html")
{
Dock = DockStyle.Fill,
};
browser.RegisterResourceHandler("http://cefsharp.demo/AsyncJavascriptBindingDemo.html", File.OpenRead("AsyncJavascriptBindingDemo.html"));
toolStripContainer.ContentPanel.Controls.Add(browser);

browser.IsBrowserInitializedChanged += OnIsBrowserInitializedChanged;
Expand All @@ -32,6 +34,14 @@ public BrowserForm()
browser.StatusMessage += OnBrowserStatusMessage;
browser.TitleChanged += OnBrowserTitleChanged;
browser.AddressChanged += OnBrowserAddressChanged;
browser.JavascriptObjectRepository.ResolveObject += (sender, e) =>
{
var repo = e.ObjectRepository;
if (e.ObjectName == "boundAsync")
{
repo.Register("boundAsync", new AsyncJavascriptBindingClass(), isAsync: true);
}
};

var bitness = Environment.Is64BitProcess ? "x64" : "x86";
var version = String.Format("Chromium: {0}, CEF: {1}, CefSharp: {2}, Environment: {3}", Cef.ChromiumVersion, Cef.CefVersion, Cef.CefSharpVersion, bitness);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<Reference Include="System.Windows.Forms" />
</ItemGroup>
<ItemGroup>
<Compile Include="AsyncJavascriptBindingClass.cs" />
<Compile Include="BrowserForm.cs">
<SubType>Form</SubType>
</Compile>
Expand Down Expand Up @@ -109,6 +110,11 @@
<ItemGroup>
<None Include="Resources\chromium-256.png" />
</ItemGroup>
<ItemGroup>
<Content Include="AsyncJavascriptBindingDemo.html">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="..\packages\CefSharp.Common.65.0.1\build\CefSharp.Common.targets" Condition="Exists('..\packages\CefSharp.Common.65.0.1\build\CefSharp.Common.targets')" />
<Import Project="..\packages\CefSharp.WinForms.65.0.1\build\CefSharp.WinForms.targets" Condition="Exists('..\packages\CefSharp.WinForms.65.0.1\build\CefSharp.WinForms.targets')" />
Expand Down

0 comments on commit 9f81788

Please sign in to comment.