-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainForm.cs
More file actions
40 lines (32 loc) · 1.14 KB
/
MainForm.cs
File metadata and controls
40 lines (32 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System.Dynamic;
using System.Text.Json;
using System.Text.Json.Nodes;
using Microsoft.Web.WebView2.Core;
namespace webviewforms;
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
// Change the title of the form
this.Text = "Placeholder Title";
// Point the webview to the index.html file
this.MainWebView.Source = new Uri(System.IO.Path.Combine(Application.StartupPath, "web", "dist", "index.html"));
// Attach WebMessageReceived handler once initalisation is complete
this.MainWebView.CoreWebView2InitializationCompleted += AttachMessageHandler;
}
/// <summary>
/// Attach message handler to the WebMessageReceived event
/// </summary>
private void AttachMessageHandler(object? sender, EventArgs e)
{
this.MainWebView.CoreWebView2.WebMessageReceived += async (s, e) =>
{
var json = e.TryGetWebMessageAsString();
Console.WriteLine(json);
//deserialize json into Record object
var record = JsonSerializer.Deserialize<JsonNode>(json);
await Router.handleMessage(this, record?["type"]?.GetValue<string>(), record?["payload"]);
};
}
}