-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
64 lines (59 loc) · 1.94 KB
/
Copy pathMainWindow.xaml.cs
File metadata and controls
64 lines (59 loc) · 1.94 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using System;
using System.Windows;
using Microsoft.Web.WebView2.Core;
using Microsoft.Web.WebView2.Wpf;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
namespace ColorCodeWebViewApp
{
public partial class MainWindow : Window
{
private ColorManager colorManager;
public MainWindow()
{
try
{
InitializeComponent();
InitializeAsync();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Initialization Exception");
}
}
private async void InitializeAsync()
{
try
{
await webView.EnsureCoreWebView2Async(null);
webView.CoreWebView2.WebMessageReceived += WebView_WebMessageReceived;
colorManager = new ColorManager();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Async Initialization Exception");
}
}
private void WebView_WebMessageReceived(object sender, CoreWebView2WebMessageReceivedEventArgs e)
{
var message = JsonSerializer.Deserialize<WebMessage>(e.WebMessageAsJson);
switch (message.Command)
{
case "saveTemplates":
colorManager.SaveTemplates(message.Templates);
break;
case "loadTemplates":
var templates = colorManager.LoadTemplates();
var json = JsonSerializer.Serialize(new { command = "loadTemplates", templates });
webView.CoreWebView2.PostWebMessageAsJson(json);
break;
}
}
}
public class WebMessage
{
public string Command { get; set; }
public List<ColorTemplate> Templates { get; set; }
}
}