Skip to content

Commit a60c816

Browse files
committed
Add two example projects
1 parent 9edf70b commit a60c816

File tree

10 files changed

+162
-0
lines changed

10 files changed

+162
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# sharpWebview
2+

examples/DesktopApp/DesktopApp.csproj

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
<ApplicationIcon>icon.ico</ApplicationIcon>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="SharpWebview" Version="0.5.2" />
11+
</ItemGroup>
12+
<ItemGroup>
13+
<None Update="icon.ico">
14+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
15+
</None>
16+
<None Update="wwwroot\app.css">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
<None Update="wwwroot\app.js">
20+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
21+
</None>
22+
<None Update="wwwroot\index.html">
23+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
24+
</None>
25+
</ItemGroup>
26+
</Project>

examples/DesktopApp/Program.cs

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System;
2+
using SharpWebview;
3+
using SharpWebview.Content;
4+
5+
namespace DesktopApp
6+
{
7+
class Program
8+
{
9+
// Set the application to Single Threaded Apartment
10+
// Otherwise the webview won't work on windows at least
11+
[STAThread]
12+
static void Main(string[] args)
13+
{
14+
// For a better startup expierience
15+
// I recommend to init the HostedContent in advance
16+
var hostedContent = new HostedContent();
17+
18+
// Wrap the usage of the webview into a using block
19+
// Otherwise the native window will not get disposed correctly
20+
using (var webview = new Webview(true))
21+
{
22+
webview
23+
// Set the title of the application window
24+
.SetTitle("The Lost Hitchhicker")
25+
// Set the start size of the window
26+
.SetSize(1024, 768, WebviewHint.None)
27+
// Set the minimum size of the window
28+
.SetSize(800, 600, WebviewHint.Min)
29+
// Bind a c# function to the webview - Accessible with the name "evalTest"
30+
.Bind("evalTest", (id, req) =>
31+
{
32+
// Req contains the parameters of the javascript call
33+
Console.WriteLine(req);
34+
// And returns a successful promise result to the javascript function, which executed the 'evalTest'
35+
webview.Return(id, RPCResult.Success, "{ result: 42 }");
36+
})
37+
// Navigate to this url on start
38+
.Navigate(hostedContent)
39+
// Run the webview loop
40+
.Run();
41+
}
42+
}
43+
}
44+
}

examples/DesktopApp/icon.ico

169 KB
Binary file not shown.

examples/DesktopApp/wwwroot/app.css

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
body {
2+
background: black;
3+
}
4+
5+
#answer {
6+
margin: 15px;
7+
padding: 25px;
8+
background: white;
9+
}

examples/DesktopApp/wwwroot/app.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
var sendAnswer = function() {
2+
evalTest("What", "is", "the", "answer?")
3+
.then((x) => document.getElementById('answer').innerHTML = x.result);
4+
}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta http-equiv="x-ua-compatible" content="ie=edge" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1" />
7+
8+
<title>Answer Machine</title>
9+
10+
<link rel="stylesheet" href="app.css" />
11+
</head>
12+
13+
<body>
14+
<button onclick="sendAnswer()">What is the answer?</button>
15+
<div id="answer"></div>
16+
<script src="app.js"></script>
17+
</body>
18+
</html>

examples/Minimal/Minimal.csproj

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>WinExe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
<ApplicationIcon>icon.ico</ApplicationIcon>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="SharpWebview" Version="0.5.2" />
11+
</ItemGroup>
12+
<ItemGroup>
13+
<None Update="icon.ico">
14+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
15+
</None>
16+
</ItemGroup>
17+
</Project>

examples/Minimal/Program.cs

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System;
2+
using SharpWebview;
3+
using SharpWebview.Content;
4+
5+
namespace Minimal
6+
{
7+
class Program
8+
{
9+
// Set the application to Single Threaded Apartment
10+
// Otherwise the webview won't work on windows at least
11+
[STAThread]
12+
static void Main(string[] args)
13+
{
14+
// Wrap the usage of the webview into a using block
15+
// Otherwise the native window will not get disposed correctly
16+
using(var webview = new Webview(true))
17+
{
18+
webview
19+
// Set the title of the application window
20+
.SetTitle("The Hitchhicker")
21+
// Set the start size of the window
22+
.SetSize(1024, 768, WebviewHint.None)
23+
// Set the minimum size of the window
24+
.SetSize(800, 600, WebviewHint.Min)
25+
// This script gets executed after navigating to the url
26+
.InitScript("window.x = 42;")
27+
// Bind a c# function to the webview - Accessible with the name "evalTest"
28+
.Bind("evalTest", (id, req) =>
29+
{
30+
// Executes the javascript on the webview
31+
webview.Evaluate("console.log('The anwser is ' + window.x);");
32+
// And returns a successful promise result to the javascript function, which executed the 'evalTest'
33+
webview.Return(id, RPCResult.Success, "{ result: 'We always knew it!' }");
34+
})
35+
// Navigate to this url on start
36+
.Navigate(new UrlContent("https://en.wikipedia.org/wiki/The_Hitchhiker%27s_Guide_to_the_Galaxy_(novel)"))
37+
// Run the webview loop
38+
.Run();
39+
}
40+
}
41+
}
42+
}

examples/Minimal/icon.ico

169 KB
Binary file not shown.

0 commit comments

Comments
 (0)