../ Software Development
-rw-r--r--aguang1.9 KiB1 min read

modified

Develop Cross Platform Desktop App with Blazor and Electron.NET

  • System: macOS Big Sur 11.6
  • Dotnet: 5

Install Electron.NET CLI Tool

First, we need to install the Electron.NET tool.

bash
dotnet tool install ElectronNET.CLI -g

Initialize a Blazor Server Project

Then, we initialize a Blazor Server project named example-app.

bash
dotnet new blazorserver -o example-app --no-https

Also, we use a tool called electronize to setup Electron related settings.

bash
cd ./ExampleApp && electronize init

Run Blazor Server with Electron

To make the server run together with Electron, we need to install the ElectronNET.API package and make some necessary code change:

Program.cs

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

Startup.cs

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

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

Start the Development

Now, we can start the project with file watcher.

bash
electronize start /watch

References

COMMAND INTERFACE

↑ ↓ navigate · Enter execute · Esc exit · try “whoami”

Loading filesystem…

:help aguang.xyz

A personal website disguised as an operating system.

Ctrl / Cmd + K
Command palette + full-text search
Tab / Shift + Tab
Move between links and controls
Enter
Open a focused link or search result
↑ / ↓ in search
Navigate search results
In a directory filter
Enter/Done or ↓ focuses a result; Esc clears the filter
Esc
Clear a search or close a window