r/AvaloniaUI

▲ 237 r/AvaloniaUI+1 crossposts

C# is known for its boilerplate and verbosity. Most of the time, it's reasonable, and MS does a good thing by teaching you to follow the structure (so others can maintain your code, lol).

But sometimes you really want a simple IDisposable app, like university coursework or a small utility. In this case, people use Python. But now C# is a great candidate too!

Here's the code sample. It uses Avalonia, so it is cross-platform, and it uses no XAML for simplicity (my friends were surprised it's possible).

It has 3 NuGet references at the top, then here goes the class with AppBuilder, when we start an app, we:

  1. Add a theme (optional, but it looks good)
  2. Create a window object
  3. Create a Label
  4. Show the window we just created and run the app!
#:package Avalonia@*
#:package Avalonia.Desktop@*
#:package Avalonia.Themes.Simple@*

using Avalonia;
using Avalonia.Controls;

class MainClass {
    public static void Main(string[] args) {
        AppBuilder
            .Configure<Application>()
            .UsePlatformDetect()
            .Start(AppMain, args);
    }

    public static void AppMain(Application app, string[] args) {
        // Application needs a theme to render window content
        app.Styles.Add(new Avalonia.Themes.Simple.SimpleTheme());
        app.RequestedThemeVariant = Avalonia.Styling.ThemeVariant.Default; // Default, Dark, Light

        // Create window
        var win = new Window();
        win.Title = "Avalonia no XAML";
        win.Width = 800;
        win.Height = 600;

        var text = new Label();
        win.Content = text;

        text.Content = "Hello from C#!";
        text.HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Center;
        text.VerticalAlignment = Avalonia.Layout.VerticalAlignment.Center;
        text.FontSize = 72;

        win.Show();
        app.Run(win);
    }
}
u/gameplayer55055 — 8 days ago

When I make a template project using Avalonia extension for Visual Studio, it doesn't even use Avalonia 12 (though the release notes talk about it's release). I then manually update .csproj files from Avalonia version 11.3.12 to 12.0.2, build and run Desktop project. That works fine.

But when I run the Browser project I get:

'AppBuilder' does not contain a definition for 'StartBrowserAppAsync' and no accessible extension method 'StartBrowserAppAsync' accepting a first argument of type 'AppBuilder' could be found (are you missing a using directive or an assembly reference?)'

I understand v12 took away Avalonia.Browser namespace, but they don't seem to point or provide the 'new way' of building the browser project.

FYI reason I use .NET8 is cause my job won't mass deploy .NET10 right now.

Any help is extremely appreciated.

reddit.com
u/IHURLEN — 9 days ago