2

The instructions at Electron.NET instruct to add the following snippets to my .NET 6 project for Electron to run.

Add to Program.cs:

public static IHostBuilder CreateHostBuilder(string[] args) =>
    Host.CreateDefaultBuilder(args)
        .ConfigureWebHostDefaults(webBuilder =>
        {
            webBuilder.UseElectron(args);
            webBuilder.UseStartup<Startup>();
        });

Add to Startup.cs:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    ...

    // Open the Electron-Window here
    Task.Run(async () => await Electron.WindowManager.CreateWindowAsync());
}

Since there is no longer a Startup.cs file, how do I convert this code to a usable state for a modern Program.cs file?

Obviously it's not as simple as putting all the code into a modern Program.cs file. I scoured google for an answer to this issue and didn't find anything. I also poured through the documentation in the github repository and Electron website. I don't have the experience to figure this out on my own and would love some help.

Jamiu S.
  • 5,257
  • 5
  • 12
  • 34
patshea26
  • 21
  • 3

1 Answers1

1

This works for me:

Program.cs

using ElectronNET.API;
using ElectronNET.API.Entities;

namespace tests;

public class Program
{
    public static async Task Main(string[] args)
    {
        var builder = WebApplication.CreateBuilder(args);
        builder.Services.AddElectron();
        builder.WebHost.UseElectron(args);

        if (HybridSupport.IsElectronActive)
        {
            var window = await Electron.WindowManager.CreateWindowAsync(
                new BrowserWindowOptions
                {
                    Width = 500,
                    Height = 250
                });

            window.OnClosed += () =>
            {
                Electron.App.Quit();
            };
        }
    }
}


Pre-requisites

  • .NET 6
  • .NET 5 framework installed (at least runtime)
  • Node installed (for npm)
  • VS running as admin (I am running 2022 at time of writing)
  • Electron-API nuget package installed
  • Electron-CLI installed (dotnettool)


Result

enter image description here

evilmandarine
  • 4,241
  • 4
  • 17
  • 40