Running GUI apps as scripts using .NET and C#

1 day ago 4

NuGet NuGet

Creating minimal Avalonia next generation (NXUI, next-gen UI) application using C# 10 and .NET 8

s6WC0Uol0x.mp4
<PackageReference Include="NXUI" Version="11.1.0" />

or for F# support:

<PackageReference Include="NXUI.FSharp" Version="11.1.0" />

Additionally, depending on the application type:

For Desktop extensions:

<PackageReference Include="NXUI.Desktop" Version="11.1.0" />

or using plain Avalonia:

<PackageReference Include="Avalonia.Desktop" Version="11.1.0" />
<PackageReference Include="Avalonia.Browser" Version="11.0.0" />
dotnet workload install wasm-tools
Window Build() => Window().Content(Label().Content("NXUI")); AppBuilder.Configure<Application>() .UsePlatformDetect() .UseFluentTheme() .StartWithClassicDesktopLifetime(Build, args);
var count = 0; Window Build() => Window(out var window) .Title("NXUI").Width(400).Height(300) .Content( StackPanel() .Children( Button(out var button) .Content("Welcome to Avalonia, please click me!"), TextBox(out var tb1) .Text("NXUI"), TextBox() .Text(window.BindTitle()), Label() .Content(button.ObserveOnClick().Select(_ => ++count).Select(x => $"You clicked {x} times.")))) .Title(tb1.ObserveText().Select(x => x?.ToUpper())); AppBuilder.Configure<Application>() .UsePlatformDetect() .UseFluentTheme() .WithApplicationName("NXUI") .StartWithClassicDesktopLifetime(Build, args);

Minimalistic Desktop app:

Run( () => Window().Content(Label().Content("NXUI")), "NXUI", args, ThemeVariant.Dark);

C#

cd src/Generator dotnet run -- ../NXUI/Generated

F#

cd src/Generator dotnet run -- ../NXUI.FSharp/Generated -fsharp

Using .NET 10 you can run GUI apps using scripts: https://devblogs.microsoft.com/dotnet/announcing-dotnet-run-app/#using-shebang-lines-for-shell-scripts

Note: You might need to adjust shebang line to #!/usr/bin/dotnet run

App.cs

#!/usr/local/share/dotnet/dotnet run #:package NXUI.Desktop@11.1.0.1 NXUI.Desktop.NXUI.Run( () => Window().Content(Label().Content("NXUI")), "NXUI", args, ThemeVariant.Dark, ShutdownMode.OnLastWindowClose);

image

More complex app:

#!/usr/local/share/dotnet/dotnet run #:package NXUI.Desktop@11.1.0.1 var count = 0; Window Build() => Window(out var window) .Title("NXUI").Width(400).Height(300) .Content( StackPanel() .Children( Button(out var button) .Content("Welcome to Avalonia, please click me!"), TextBox(out var tb1) .Text("NXUI"), TextBox() .Text(window.BindTitle()), Label() .Content(button.ObserveOnClick().Select(_ => ++count).Select(x => $"You clicked {x} times.")))) .Title(tb1.ObserveText().Select(x => x?.ToUpper())); AppBuilder.Configure<Application>() .UsePlatformDetect() .UseFluentTheme() .WithApplicationName("NXUI") .StartWithClassicDesktopLifetime(Build, args);

image

Read Entire Article