@devscholar/node-ps1-dotnet 0.0.0 → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +9 -40
- package/package.json +5 -3
- package/examples/console/await-delay/await-delay.ts +0 -14
- package/examples/console/console-input/console-input.ts +0 -18
- package/examples/winforms/counter/counter.ts +0 -47
- package/examples/winforms/drag-box/drag-box.ts +0 -62
- package/examples/wpf/counter/counter.ts +0 -55
- package/examples/wpf/drag-box/drag-box.ts +0 -84
- package/examples/wpf/webview2-browser/WebView2Libs/Microsoft.Web.WebView2.Core.dll +0 -0
- package/examples/wpf/webview2-browser/WebView2Libs/Microsoft.Web.WebView2.Core.dll.backup +0 -0
- package/examples/wpf/webview2-browser/WebView2Libs/Microsoft.Web.WebView2.Wpf.dll +0 -0
- package/examples/wpf/webview2-browser/WebView2Libs/Microsoft.Web.WebView2.Wpf.dll.backup +0 -0
- package/examples/wpf/webview2-browser/WebView2Libs/WebView2License.txt +0 -27
- package/examples/wpf/webview2-browser/counter.html +0 -31
- package/examples/wpf/webview2-browser/webview2-browser.ts +0 -90
package/README.md
CHANGED
|
@@ -14,52 +14,21 @@ This is a project that mimics the [Node API for .NET](https://github.com/microso
|
|
|
14
14
|
|
|
15
15
|
# Examples
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
Please visit the [node-ps1-dotnet-examples](https://github.com/devscholar/node-ps1-dotnet-examples) repository for working examples.
|
|
18
18
|
|
|
19
|
-
|
|
20
|
-
node start.js examples/console/console-input/console-input.ts --runtime=deno
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
## Console Apps
|
|
24
|
-
|
|
25
|
-
### Console Input App
|
|
26
|
-
|
|
27
|
-
```bat
|
|
28
|
-
node start.js examples/console/console-input/console-input.ts
|
|
29
|
-
```
|
|
30
|
-
### Await Delay App
|
|
31
|
-
|
|
32
|
-
```bat
|
|
33
|
-
node start.js examples/console/await-delay/await-delay.ts
|
|
34
|
-
```
|
|
19
|
+
## Quick Start with Examples
|
|
35
20
|
|
|
36
|
-
|
|
21
|
+
Clone the examples repository alongside this project:
|
|
37
22
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
### WinForms Drag Box App
|
|
44
|
-
|
|
45
|
-
```bat
|
|
46
|
-
node start.js examples/winforms/drag-box/drag-box.ts
|
|
23
|
+
```bash
|
|
24
|
+
git clone https://github.com/devscholar/node-ps1-dotnet-examples.git
|
|
25
|
+
cd node-ps1-dotnet-examples
|
|
26
|
+
npm install
|
|
27
|
+
npm run winforms-counter
|
|
47
28
|
```
|
|
48
|
-
### WPF Counter App
|
|
49
29
|
|
|
50
|
-
|
|
51
|
-
node start.js examples/wpf/counter/counter.ts
|
|
52
|
-
```
|
|
53
|
-
### WPF Drag Box App
|
|
30
|
+
For more examples and details, see the [node-ps1-dotnet-examples README](https://github.com/devscholar/node-ps1-dotnet-examples).
|
|
54
31
|
|
|
55
|
-
```bat
|
|
56
|
-
node start.js examples/wpf/drag-box/drag-box.ts
|
|
57
|
-
```
|
|
58
|
-
### WPF WebView2 Browser
|
|
59
|
-
|
|
60
|
-
```bat
|
|
61
|
-
node start.js examples/wpf/webview2-browser/webview2-browser.ts
|
|
62
|
-
```
|
|
63
32
|
# License
|
|
64
33
|
|
|
65
34
|
This project is licensed under the MIT License. See the [LICENSE](LICENSE.md) file for details.
|
package/package.json
CHANGED
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@devscholar/node-ps1-dotnet",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.1",
|
|
4
4
|
"description": "Node.js to .NET interop via IPC",
|
|
5
|
-
"main": "
|
|
5
|
+
"main": "./src/index.ts",
|
|
6
6
|
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./src/index.ts"
|
|
9
|
+
},
|
|
7
10
|
"scripts": {
|
|
8
11
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
9
|
-
"start": "node start.js examples/winforms/counter/counter.ts",
|
|
10
12
|
"build": "tsc"
|
|
11
13
|
},
|
|
12
14
|
"keywords": ["dotnet", "winforms", "interop", "bridge", "ipc"],
|
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
// examples/console/await-delay/await-delay.ts
|
|
2
|
-
import dotnet from '../../../src/index.ts';
|
|
3
|
-
|
|
4
|
-
const System = dotnet.System as any;
|
|
5
|
-
const Console = System.Console;
|
|
6
|
-
const Task = System.Threading.Tasks.Task;
|
|
7
|
-
const Path = System.IO.Path;
|
|
8
|
-
|
|
9
|
-
Console.WriteLine('0s');
|
|
10
|
-
await Task.Delay(1000);
|
|
11
|
-
await Console.Out.WriteLineAsync("1s");
|
|
12
|
-
await Task.Delay(1000);
|
|
13
|
-
await Console.Out.WriteLineAsync("2s");
|
|
14
|
-
await Task.Delay(1000);
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
// examples/console/console-input/console-input.ts
|
|
2
|
-
import dotnet from '../../../src/index.ts';
|
|
3
|
-
|
|
4
|
-
const System = dotnet.System as any;
|
|
5
|
-
const Console = System.Console;
|
|
6
|
-
|
|
7
|
-
Console.WriteLine("=== Greeting Program ===");
|
|
8
|
-
Console.Write("Please enter your name: ");
|
|
9
|
-
|
|
10
|
-
const name = Console.ReadLine();
|
|
11
|
-
|
|
12
|
-
if (name && name.trim() !== "") {
|
|
13
|
-
Console.WriteLine(`Hello, ${name}! Welcome to this program!`);
|
|
14
|
-
} else {
|
|
15
|
-
Console.WriteLine("Hello, friend! Welcome to this program!");
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
Console.WriteLine("Program ended.");
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
// examples/winforms/counter/counter.ts
|
|
2
|
-
import dotnet from '../../../src/index.ts';
|
|
3
|
-
|
|
4
|
-
dotnet.load('System.Windows.Forms');
|
|
5
|
-
dotnet.load('System.Drawing');
|
|
6
|
-
|
|
7
|
-
const System = dotnet.System as any;
|
|
8
|
-
const Forms = System.Windows.Forms;
|
|
9
|
-
const Drawing = System.Drawing;
|
|
10
|
-
|
|
11
|
-
let clickCount = 0;
|
|
12
|
-
|
|
13
|
-
console.log("--- WinForms Counter (XP Style) ---");
|
|
14
|
-
|
|
15
|
-
Forms.Application.EnableVisualStyles();
|
|
16
|
-
Forms.Application.SetCompatibleTextRenderingDefault(false);
|
|
17
|
-
|
|
18
|
-
const form = new Forms.Form();
|
|
19
|
-
form.Text = "Counter App";
|
|
20
|
-
form.Width = 640;
|
|
21
|
-
form.Height = 480;
|
|
22
|
-
form.StartPosition = 1;
|
|
23
|
-
|
|
24
|
-
const label = new Forms.Label();
|
|
25
|
-
label.Text = "Clicks: 0";
|
|
26
|
-
label.Font = new Drawing.Font("Arial", 24);
|
|
27
|
-
label.AutoSize = true;
|
|
28
|
-
label.Location = new Drawing.Point(90, 30);
|
|
29
|
-
form.Controls.Add(label);
|
|
30
|
-
|
|
31
|
-
const button = new Forms.Button();
|
|
32
|
-
button.Text = "Click to Add";
|
|
33
|
-
button.Font = new Drawing.Font("Arial", 14);
|
|
34
|
-
button.AutoSize = true;
|
|
35
|
-
button.Location = new Drawing.Point(100, 90);
|
|
36
|
-
|
|
37
|
-
button.add_Click(() => {
|
|
38
|
-
clickCount++;
|
|
39
|
-
const message = `Clicked ${clickCount} times`;
|
|
40
|
-
label.Text = message;
|
|
41
|
-
console.log(message);
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
form.Controls.Add(button);
|
|
45
|
-
|
|
46
|
-
console.log("Click the button to increase the counter...");
|
|
47
|
-
Forms.Application.Run(form);
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
// examples/winforms/drag-box/drag-box.ts
|
|
2
|
-
import dotnet from '../../../src/index.ts';
|
|
3
|
-
|
|
4
|
-
dotnet.load('System.Windows.Forms');
|
|
5
|
-
dotnet.load('System.Drawing');
|
|
6
|
-
|
|
7
|
-
const System = dotnet.System as any;
|
|
8
|
-
const Forms = System.Windows.Forms;
|
|
9
|
-
const Drawing = System.Drawing;
|
|
10
|
-
|
|
11
|
-
console.log("--- WinForms Draggable Box ---");
|
|
12
|
-
|
|
13
|
-
Forms.Application.EnableVisualStyles();
|
|
14
|
-
Forms.Application.SetCompatibleTextRenderingDefault(false);
|
|
15
|
-
|
|
16
|
-
const form = new Forms.Form();
|
|
17
|
-
form.Text = "Draggable Box Example (High Frequency IPC)";
|
|
18
|
-
form.Width = 800;
|
|
19
|
-
form.Height = 600;
|
|
20
|
-
form.StartPosition = 1; // CenterScreen
|
|
21
|
-
|
|
22
|
-
const box = new Forms.Panel();
|
|
23
|
-
box.BackColor = Drawing.Color.Red;
|
|
24
|
-
box.Width = 100;
|
|
25
|
-
box.Height = 100;
|
|
26
|
-
|
|
27
|
-
// Track position locally to avoid synchronous read overhead over IPC
|
|
28
|
-
let currentX = 350;
|
|
29
|
-
let currentY = 250;
|
|
30
|
-
box.Location = new Drawing.Point(currentX, currentY);
|
|
31
|
-
|
|
32
|
-
form.Controls.Add(box);
|
|
33
|
-
|
|
34
|
-
// State tracking for drag and drop
|
|
35
|
-
let isDragging = false;
|
|
36
|
-
let startDragOffsetX = 0;
|
|
37
|
-
let startDragOffsetY = 0;
|
|
38
|
-
|
|
39
|
-
box.add_MouseDown((sender: any, e: any) => {
|
|
40
|
-
isDragging = true;
|
|
41
|
-
startDragOffsetX = e.X;
|
|
42
|
-
startDragOffsetY = e.Y;
|
|
43
|
-
box.BackColor = Drawing.Color.DarkRed; // Visual feedback
|
|
44
|
-
});
|
|
45
|
-
|
|
46
|
-
box.add_MouseUp((sender: any, e: any) => {
|
|
47
|
-
isDragging = false;
|
|
48
|
-
box.BackColor = Drawing.Color.Red;
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
box.add_MouseMove((sender: any, e: any) => {
|
|
52
|
-
if (isDragging) {
|
|
53
|
-
currentX = currentX + e.X - startDragOffsetX;
|
|
54
|
-
currentY = currentY + e.Y - startDragOffsetY;
|
|
55
|
-
|
|
56
|
-
box.Left = currentX;
|
|
57
|
-
box.Top = currentY;
|
|
58
|
-
}
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
console.log("Initialization complete. Try dragging the red box!");
|
|
62
|
-
Forms.Application.Run(form);
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import dotnet from '../../../src/index.ts';
|
|
2
|
-
|
|
3
|
-
dotnet.load('PresentationFramework');
|
|
4
|
-
dotnet.load('PresentationCore');
|
|
5
|
-
dotnet.load('WindowsBase');
|
|
6
|
-
|
|
7
|
-
const System = dotnet.System as any;
|
|
8
|
-
const Windows = System.Windows;
|
|
9
|
-
const Controls = System.Windows.Controls;
|
|
10
|
-
const Media = System.Windows.Media;
|
|
11
|
-
|
|
12
|
-
let clickCount = 0;
|
|
13
|
-
|
|
14
|
-
console.log("--- WPF Counter ---");
|
|
15
|
-
|
|
16
|
-
const mainWindow = new Windows.Window();
|
|
17
|
-
mainWindow.Title = "WPF Counter App";
|
|
18
|
-
mainWindow.Width = 400;
|
|
19
|
-
mainWindow.Height = 300;
|
|
20
|
-
mainWindow.WindowStartupLocation = Windows.WindowStartupLocation.CenterScreen;
|
|
21
|
-
|
|
22
|
-
const stackPanel = new Controls.StackPanel();
|
|
23
|
-
stackPanel.Margin = new Windows.Thickness(20);
|
|
24
|
-
stackPanel.HorizontalAlignment = Windows.HorizontalAlignment.Center;
|
|
25
|
-
stackPanel.VerticalAlignment = Windows.VerticalAlignment.Center;
|
|
26
|
-
|
|
27
|
-
const label = new Controls.Label();
|
|
28
|
-
label.Content = "Clicks: 0";
|
|
29
|
-
label.FontSize = 32;
|
|
30
|
-
label.FontFamily = new Media.FontFamily("Arial");
|
|
31
|
-
label.HorizontalContentAlignment = Windows.HorizontalAlignment.Center;
|
|
32
|
-
label.Margin = new Windows.Thickness(0, 0, 0, 20);
|
|
33
|
-
stackPanel.Children.Add(label);
|
|
34
|
-
|
|
35
|
-
const button = new Controls.Button();
|
|
36
|
-
button.Content = "Click to Add";
|
|
37
|
-
button.FontSize = 18;
|
|
38
|
-
button.Padding = new Windows.Thickness(20, 10, 20, 10);
|
|
39
|
-
button.HorizontalAlignment = Windows.HorizontalAlignment.Center;
|
|
40
|
-
|
|
41
|
-
button.add_Click((sender: any, e: any) => {
|
|
42
|
-
clickCount++;
|
|
43
|
-
const message = `Clicked ${clickCount} times`;
|
|
44
|
-
label.Content = message;
|
|
45
|
-
console.log(message);
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
stackPanel.Children.Add(button);
|
|
49
|
-
|
|
50
|
-
mainWindow.Content = stackPanel;
|
|
51
|
-
|
|
52
|
-
console.log("Click the button to increase the counter...");
|
|
53
|
-
|
|
54
|
-
const app = new Windows.Application();
|
|
55
|
-
app.Run(mainWindow);
|
|
@@ -1,84 +0,0 @@
|
|
|
1
|
-
// examples/wpf/drag-box/drag-box.ts
|
|
2
|
-
import dotnet from '../../../src/index.ts';
|
|
3
|
-
|
|
4
|
-
dotnet.load('PresentationFramework');
|
|
5
|
-
dotnet.load('PresentationCore');
|
|
6
|
-
dotnet.load('WindowsBase');
|
|
7
|
-
|
|
8
|
-
const System = dotnet.System as any;
|
|
9
|
-
const Windows = System.Windows;
|
|
10
|
-
const Controls = System.Windows.Controls;
|
|
11
|
-
const Media = System.Windows.Media;
|
|
12
|
-
const Input = System.Windows.Input;
|
|
13
|
-
|
|
14
|
-
console.log("--- WPF Draggable Box ---");
|
|
15
|
-
|
|
16
|
-
const mainWindow = new Windows.Window();
|
|
17
|
-
mainWindow.Title = "WPF Draggable Box Example (High Frequency IPC)";
|
|
18
|
-
mainWindow.Width = 800;
|
|
19
|
-
mainWindow.Height = 600;
|
|
20
|
-
mainWindow.WindowStartupLocation = Windows.WindowStartupLocation.CenterScreen;
|
|
21
|
-
|
|
22
|
-
// Create a canvas for absolute positioning
|
|
23
|
-
const canvas = new Controls.Canvas();
|
|
24
|
-
canvas.Background = Media.Brushes.LightGray;
|
|
25
|
-
|
|
26
|
-
// Create the draggable box
|
|
27
|
-
const box = new Controls.Border();
|
|
28
|
-
box.Width = 100;
|
|
29
|
-
box.Height = 100;
|
|
30
|
-
box.Background = Media.Brushes.Red;
|
|
31
|
-
box.BorderBrush = Media.Brushes.DarkRed;
|
|
32
|
-
box.BorderThickness = new Windows.Thickness(2);
|
|
33
|
-
|
|
34
|
-
const transform = new Media.TranslateTransform();
|
|
35
|
-
box.RenderTransform = transform;
|
|
36
|
-
|
|
37
|
-
transform.X = 350;
|
|
38
|
-
transform.Y = 250;
|
|
39
|
-
|
|
40
|
-
canvas.Children.Add(box);
|
|
41
|
-
|
|
42
|
-
// State tracking for drag and drop
|
|
43
|
-
let isDragging = false;
|
|
44
|
-
let startMouseX = 0;
|
|
45
|
-
let startMouseY = 0;
|
|
46
|
-
let startBoxX = 0;
|
|
47
|
-
let startBoxY = 0;
|
|
48
|
-
|
|
49
|
-
box.add_MouseDown((sender: any, e: any) => {
|
|
50
|
-
console.log('[DEBUG] MouseDown');
|
|
51
|
-
isDragging = true;
|
|
52
|
-
const pos = e.GetPosition(canvas);
|
|
53
|
-
startMouseX = pos.X;
|
|
54
|
-
startMouseY = pos.Y;
|
|
55
|
-
startBoxX = transform.X;
|
|
56
|
-
startBoxY = transform.Y;
|
|
57
|
-
box.Background = Media.Brushes.DarkRed;
|
|
58
|
-
box.CaptureMouse();
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
// Mouse up event - stop dragging
|
|
62
|
-
box.add_MouseUp((sender: any, e: any) => {
|
|
63
|
-
console.log('[DEBUG] MouseUp');
|
|
64
|
-
isDragging = false;
|
|
65
|
-
box.Background = Media.Brushes.Red;
|
|
66
|
-
box.ReleaseMouseCapture();
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
// Mouse move event - handle dragging
|
|
70
|
-
box.add_MouseMove((sender: any, e: any) => {
|
|
71
|
-
if (isDragging) {
|
|
72
|
-
const currentPos = e.GetPosition(canvas);
|
|
73
|
-
|
|
74
|
-
transform.X = startBoxX + (currentPos.X - startMouseX);
|
|
75
|
-
transform.Y = startBoxY + (currentPos.Y - startMouseY);
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
mainWindow.Content = canvas;
|
|
80
|
-
|
|
81
|
-
console.log("Initialization complete. Try dragging the red box!");
|
|
82
|
-
|
|
83
|
-
const app = new Windows.Application();
|
|
84
|
-
app.Run(mainWindow);
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
Copyright (C) Microsoft Corporation. All rights reserved.
|
|
2
|
-
|
|
3
|
-
Redistribution and use in source and binary forms, with or without
|
|
4
|
-
modification, are permitted provided that the following conditions are
|
|
5
|
-
met:
|
|
6
|
-
|
|
7
|
-
* Redistributions of source code must retain the above copyright
|
|
8
|
-
notice, this list of conditions and the following disclaimer.
|
|
9
|
-
* Redistributions in binary form must reproduce the above
|
|
10
|
-
copyright notice, this list of conditions and the following disclaimer
|
|
11
|
-
in the documentation and/or other materials provided with the
|
|
12
|
-
distribution.
|
|
13
|
-
* The name of Microsoft Corporation, or the names of its contributors
|
|
14
|
-
may not be used to endorse or promote products derived from this
|
|
15
|
-
software without specific prior written permission.
|
|
16
|
-
|
|
17
|
-
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
18
|
-
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
19
|
-
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
20
|
-
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
21
|
-
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
22
|
-
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
23
|
-
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
24
|
-
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
25
|
-
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
26
|
-
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
27
|
-
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
<!DOCTYPE html>
|
|
2
|
-
<html lang="en">
|
|
3
|
-
<head>
|
|
4
|
-
<title>Counter</title>
|
|
5
|
-
<meta charset="UTF-8">
|
|
6
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
7
|
-
</head>
|
|
8
|
-
<body>
|
|
9
|
-
<h1>Counter App</h1>
|
|
10
|
-
<p id="display">Clicks: 0</p>
|
|
11
|
-
<button id="btn">Click to Add</button>
|
|
12
|
-
|
|
13
|
-
<script>
|
|
14
|
-
let clickCount = 0;
|
|
15
|
-
const display = document.getElementById('display');
|
|
16
|
-
const button = document.getElementById('btn');
|
|
17
|
-
|
|
18
|
-
button.addEventListener('click', function() {
|
|
19
|
-
clickCount++;
|
|
20
|
-
const message = 'Button clicked ' + clickCount + ' times';
|
|
21
|
-
display.textContent = message;
|
|
22
|
-
|
|
23
|
-
if (window.chrome && window.chrome.webview) {
|
|
24
|
-
window.chrome.webview.postMessage(message);
|
|
25
|
-
} else {
|
|
26
|
-
alert('chrome.webview not available! Click: ' + message);
|
|
27
|
-
}
|
|
28
|
-
});
|
|
29
|
-
</script>
|
|
30
|
-
</body>
|
|
31
|
-
</html>
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import dotnet from '../../../src/index.ts';
|
|
2
|
-
import * as fs from 'node:fs';
|
|
3
|
-
import * as path from 'node:path';
|
|
4
|
-
import { fileURLToPath } from 'node:url';
|
|
5
|
-
|
|
6
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
7
|
-
const __dirname = path.dirname(__filename);
|
|
8
|
-
|
|
9
|
-
const System = dotnet.System as any;
|
|
10
|
-
const Windows = System.Windows;
|
|
11
|
-
const Controls = System.Windows.Controls;
|
|
12
|
-
|
|
13
|
-
const webview2LibPath = path.join(__dirname, 'WebView2Libs');
|
|
14
|
-
|
|
15
|
-
try {
|
|
16
|
-
System.Reflection.Assembly.LoadFrom(path.join(webview2LibPath, 'Microsoft.Web.WebView2.Core.dll'));
|
|
17
|
-
System.Reflection.Assembly.LoadFrom(path.join(webview2LibPath, 'Microsoft.Web.WebView2.Wpf.dll'));
|
|
18
|
-
} catch (e) {
|
|
19
|
-
console.error("Core Dll Load Failed:", e);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const WebView2WpfAssembly = System.Reflection.Assembly.LoadFrom(path.join(webview2LibPath, 'Microsoft.Web.WebView2.Wpf.dll'));
|
|
23
|
-
|
|
24
|
-
const USER_DATA_FOLDER = path.join(__dirname, 'WebView2_Data');
|
|
25
|
-
const COUNTER_HTML_PATH = path.join(__dirname, 'counter.html');
|
|
26
|
-
|
|
27
|
-
console.log('--- Initializing WebView2 (Counter App) ---');
|
|
28
|
-
|
|
29
|
-
const WebView2Type = WebView2WpfAssembly.GetType('Microsoft.Web.WebView2.Wpf.WebView2');
|
|
30
|
-
const webView = new WebView2Type();
|
|
31
|
-
|
|
32
|
-
const CreationPropertiesType = WebView2WpfAssembly.GetType('Microsoft.Web.WebView2.Wpf.CoreWebView2CreationProperties');
|
|
33
|
-
const props = new CreationPropertiesType();
|
|
34
|
-
if (!fs.existsSync(USER_DATA_FOLDER)) fs.mkdirSync(USER_DATA_FOLDER, { recursive: true });
|
|
35
|
-
props.UserDataFolder = USER_DATA_FOLDER;
|
|
36
|
-
props.Language = "zh-CN";
|
|
37
|
-
webView.CreationProperties = props;
|
|
38
|
-
|
|
39
|
-
const browserWindow = new Windows.Window();
|
|
40
|
-
browserWindow.Title = 'Counter App - WebView2';
|
|
41
|
-
browserWindow.Width = 500;
|
|
42
|
-
browserWindow.Height = 400;
|
|
43
|
-
browserWindow.WindowStartupLocation = Windows.WindowStartupLocation.CenterScreen;
|
|
44
|
-
|
|
45
|
-
const grid = new Controls.Grid();
|
|
46
|
-
browserWindow.Content = grid;
|
|
47
|
-
grid.Children.Add(webView);
|
|
48
|
-
|
|
49
|
-
webView.add_CoreWebView2InitializationCompleted((sender: any, e: any) => {
|
|
50
|
-
if (e.IsSuccess) {
|
|
51
|
-
console.log('WebView2 Initialized Successfully');
|
|
52
|
-
|
|
53
|
-
const coreWebView2 = webView.CoreWebView2;
|
|
54
|
-
|
|
55
|
-
coreWebView2.add_WebMessageReceived((sender2: any, e2: any) => {
|
|
56
|
-
const message = e2.TryGetWebMessageAsString();
|
|
57
|
-
if (message) {
|
|
58
|
-
console.log('[WebView2] ' + message);
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
const script = `
|
|
63
|
-
(function() {
|
|
64
|
-
var originalLog = console.log;
|
|
65
|
-
console.log = function(msg) {
|
|
66
|
-
originalLog(msg);
|
|
67
|
-
if (window.chrome && window.chrome.webview) {
|
|
68
|
-
window.chrome.webview.postMessage(msg);
|
|
69
|
-
}
|
|
70
|
-
};
|
|
71
|
-
})();
|
|
72
|
-
`;
|
|
73
|
-
coreWebView2.ExecuteJavaScript(script);
|
|
74
|
-
} else {
|
|
75
|
-
console.error('FAILURE: Init failed', e.InitializationException?.Message);
|
|
76
|
-
}
|
|
77
|
-
});
|
|
78
|
-
|
|
79
|
-
webView.add_NavigationCompleted((sender: any, e: any) => {
|
|
80
|
-
console.log('Page Loaded Successfully');
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
webView.Source = new System.Uri(COUNTER_HTML_PATH);
|
|
84
|
-
|
|
85
|
-
browserWindow.add_Closed((sender: any, e: any) => {
|
|
86
|
-
process.exit(0);
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
const app = new Windows.Application();
|
|
90
|
-
app.Run(browserWindow);
|