Skip to content

PrismApplication Entry Point

Damian edited this page Jul 15, 2022 · 4 revisions

To get started using Prism.Avalonia we simply need to add a few pieces to your base Avalonia program's entry point. This example comes directly from the SampleDialogApp in the repository.

Basic Entry Point

You may choose to separate your Program.cs and App.axml.cs or just throw everything into your App.axml.cs file. Below is example of them split apart.

As per the Application Lifetimes documentation, it is preferred to use Program.cs since Things aren't ready yet, so at this point, you shouldn't use any Avalonia types or anything that expects a SynchronizationContext to be ready.

Program.cs

using System;
using Avalonia;

namespace SampleDialogApp
{
    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<App>()
                      .UsePlatformDetect()
                      .With(new X11PlatformOptions { EnableMultiTouch = true, UseDBusMenu = true, })
                      .With(new Win32PlatformOptions { EnableMultitouch = true, AllowEglInitialization = true, })
                      .UseSkia()
                      .LogToTrace();
    }
}

App.axml.cs

using System;
using Avalonia;
using Avalonia.Markup.Xaml;
using Prism.DryIoc;
using Prism.Ioc;
using SampleDialogApp.ViewModels;
using SampleDialogApp.Views;

namespace SampleDialogApp
{
    public partial class App : PrismApplication
    {
        public override void Initialize()
        {
            AvaloniaXamlLoader.Load(this);
            base.Initialize();
        }

        protected override IAvaloniaObject CreateShell()
        {
            Console.WriteLine("CreateShell()");
            return Container.Resolve<MainWindow>();
        }

        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            // Main shell window
            containerRegistry.Register<MainWindow>();

            // Optional IDialogService
            // containerRegistry.RegisterDialog<NotificationDialogView, NotificationDialogViewModel>();
        }
    }
}