Skip to content

theolivenbaum/electron-sharp

Repository files navigation

ElectronSharp

Build cross platform desktop apps with .NET 7 and ASP.NET Core (Razor Pages, MVC), Blazor or h5.

ElectronSharp is a wrapper around a "normal" Electron application with an embedded ASP.NET Core application. It uses a socket-based IPC bridge we can invoke Electron APIs from .NET.

The CLI extensions hosts the toolset to build and start ElectronSharp-based applications.

ElectronSharp is a hard fork from the original Electron.NET project, mantained by me and used to build the Curiosity app.

📦 NuGet:

  • API NuGet
  • CLI NuGet

🛠 Requirements to run:

The current ElectronSharp CLI builds Windows/macOS/Linux binaries. The API uses .NET 7, so our minimum base OS is the same as .NET 7.

Also you should have installed:

👩‍🏫 Usage

To activate and communicate with the Electron API, include the ElectronSharp.API NuGet package in your ASP.NET Core app. Check out the Electron.SampleApp for an example of how to use ElectronSharp.

PM> Install-Package ElectronSharp.API

Program.cs

You start ElectronSharp up with an UseElectron WebHostBuilder-Extension.

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

Startup.cs

Open the Electron Window in the Startup.cs file:

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

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

🚀 Start the Application

To start the application make sure you have installed the "ElectronSharp.CLI" packages as global tool:

dotnet tool install ElectronSharp.CLI -g

At the first time, you need an ElectronSharp project initialization. Type the following command in your app folder:

electron-sharp init
  • Now a electronnet.manifest.json should appear in your ASP.NET Core project
  • Now run the following:
electron-sharp start

Hint

If invoking any of those commands gives you strange errors (like .NET 5 not installed, but your project is .NET 6), it means you've typed electronize instead of electron-sharp.

Note

Only the first electronize start is slow. The next will go on faster.

🔭 Develop ElectronSharp apps using a file watcher

The file watcher is included with version 8.31.1 of ElectronSharp. For example, a file change can trigger compilation, test execution, or deployment. The ElectronSharp window will automatically refresh and new code changes will be visible more quickly. The following ElectronSharp CLI command is required:

electron-sharp start /watch

Note

Only the first electronize start is slow. The next will go on faster.

🐞 Debug

Start your ElectronSharp application with the ElectronSharp CLI command. In Visual Studio attach to your running application instance. Go in the Debug Menu and click on Attach to Process.... Sort by your projectname on the right and select it on the list.

📔 Usage of the Electron-API

A complete documentation will follow. Until then take a look in the source code of the sample application:
ElectronSharp Sample App

⛏ Build

Here you need the ElectronSharp CLI as well. Type the following command in your ASP.NET Core folder:

electronize build /target win

There are additional platforms available:

electron-sharp build /target win
electron-sharp build /target osx
electron-sharp build /target osx-arm64
electron-sharp build /target linux

Those four "default" targets will produce packages for those platforms. Note that the osx-arm64 is for Apple Silicon Macs.

For certain NuGet packages or certain scenarios you may want to build a pure x86 application. To support those things you can define the desired .NET runtime, the electron platform and electron architecture like this:

electron-sharp build /target custom "win7-x86;win32" /electron-arch ia32 

The end result should be an electron app under your /bin/desktop folder.

Note

macOS builds can't be created on Windows machines because they require symlinks that aren't supported on Windows (per this Electron issue). macOS builds can be produced on either Linux or macOS machines.

👨‍💻 Original (Electron.NET) Authors

  • Gregor Biswanger - (Microsoft MVP, Intel Black Belt and Intel Software Innovator) is a freelance lecturer, consultant, trainer, author and speaker. He is a consultant for large and medium-sized companies, organizations and agencies for software architecture, web- and cross-platform development. You can find Gregor often on the road attending or speaking at international conferences. - Cross-Platform-Blog - Twitter @BFreakout
  • Robert Muehsig - Software Developer - from Dresden, Germany, now living & working in Switzerland. Microsoft MVP & Web Geek. - codeinside Blog - Twitter @robert0muehsig

See also the list of contributors who participated in the original project.

🙋‍♀️🙋‍♂ Contributing

Feel free to submit a pull request if you find any bugs (to see a list of active issues, visit the Issues section. Please make sure all commits are properly documented.

🎉 License

MIT-licensed