Search code examples
c#.net-6.0avaloniaui

AvaloniaUI to existing project, no runtime services configured [.NET 6]


I am trying to learn how to use AvaloniaUI in an existing .NET 6 project. I could not find any instructions on how to do this, the only provided instructions were for starting a new project, maybe I missed it somewhere in pages of duckduckgo.

What I did find were countless examples using this as the entry point:

class Program
{
    // Initialization code. Don't use any Avalonia, third-party APIs or any
    // SynchronizationContext-reliant code before AppMain is called: things aren't initialized
    // yet and stuff might break.
    [STAThread]
    public static void Main(string[] args) => BuildAvaloniaApp()
        .StartWithClassicDesktopLifetime(args);

    // Avalonia configuration, don't remove; also used by visual designer.
    public static AppBuilder BuildAvaloniaApp()
        => AppBuilder.Configure<App>()
            .UsePlatformDetect()
            .WithInterFont()
            .LogToTrace();
}

from Avalonia itself -> https://github.com/AvaloniaUI/AvaloniaUI.QuickGuides/blob/main/ButtonCustomize/Program.cs

However there is no UsePlatformDetect method, I tried to use one of the only other options in it's place: UseStandardRuntimePlatformSubsystem but I just get this error:

Unhandled exception. System.InvalidOperationException: No windowing system configured.

at Avalonia.AppBuilder.Setup()

My entry point code:

internal class Program
{
    // Initialization code. Don't use any Avalonia, third-party APIs or any
    // SynchronizationContext-reliant code before AppMain is called: things aren't initialized
    // yet and stuff might break.
    [STAThread]
    public static void Main(string[] args)
    {
        BuildAvaloniaApp()
            .StartWithClassicDesktopLifetime(args);
    }

    // Avalonia configuration, don't remove; also used by visual designer.
    public static AppBuilder BuildAvaloniaApp()
        => AppBuilder.Configure<Main>()
            .UseStandardRuntimePlatformSubsystem()
            .LogToTrace();
}

My "main" app (I do not plan to use the XML features)

internal class Main : Application
{
    public override void OnFrameworkInitializationCompleted()
    {
        if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
        {
            desktop.MainWindow = new MainWindow();
        }
    }
}

And finally the mainwindow:

internal class MainWindow : Window
{
    private TextBox imageLocation;

    public MainWindow()
    {...}
}

Solution

  • I had to also install Avalonia.Desktop from nuget in addition to Avalona to resolve this.