forked from microsoft/node-api-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.xaml.cs
65 lines (53 loc) · 2.07 KB
/
App.xaml.cs
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
65
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Diagnostics;
using System.IO;
using Microsoft.JavaScript.NodeApi.Runtimes;
namespace Microsoft.JavaScript.NodeApi.Examples;
/// <summary>
/// Provides application-specific behavior to supplement the default Application class.
/// </summary>
public partial class App : Application
{
private Window? window;
/// <summary>
/// Initializes the singleton application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
/// </summary>
public App()
{
// Node.js require() searches for modules/packages relative to the CWD.
Environment.CurrentDirectory = Path.GetDirectoryName(typeof(App).Assembly.Location)!;
string libnodePath = Path.Combine(
Path.GetDirectoryName(typeof(App).Assembly.Location)!,
"libnode.dll");
NodejsPlatform nodejsPlatform = new(libnodePath);
Nodejs = nodejsPlatform.CreateEnvironment();
if (Debugger.IsAttached)
{
int pid = Process.GetCurrentProcess().Id;
Uri inspectionUri = Nodejs.StartInspector();
Debug.WriteLine(
$"Node.js ({pid}) inspector listening at {inspectionUri.AbsoluteUri}");
}
this.InitializeComponent();
}
/// <summary>
/// Invoked when the application is launched normally by the end user. Other entry points
/// will be used such as when the application is launched to open a specific file.
/// </summary>
/// <param name="args">Details about the launch request and process.</param>
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
window = new MainWindow();
window.Activate();
window.Closed += OnMainWindowClosed;
}
private void OnMainWindowClosed(object sender, WindowEventArgs args)
{
Nodejs.Dispose();
}
public static new App Current => (App)Application.Current;
public NodejsEnvironment Nodejs { get; }
}