Skip to content

Getting Started

Damian edited this page Aug 24, 2024 · 15 revisions

Getting Started

Basic Configuration

This section steps you through the basic requirements to get your Prism.Avalonia project up and running.

If you're familiar with Prism.WPF, you're already ahead of the game! Prism.Avalonia is nearly a 1-to-1 implementation.

Install NuGet Packages

Prism.Avalonia requires the following Packages to be installed. Currently, only DryIoc is supported.

Package NuGet
Prism.Avalonia Prism.Avalonia NuGet Badge
Prism.DryIoc.Avalonia Prism.DryIoc.Avalonia NuGet Badge

Create Shell

The Main entry point of your Avalonia application does not require changes.

Inside the App class (App.xaml.cs), a few minor changes are needed.

  1. Update your App class to inherit from PrismApplication instead of, Application
  2. Add an Initialize() method and add base.Initialize();
    • base.Initialize(); is required to begin the initialization of Prism.Avalonia
  3. Remove OnFrameworkInitializationCompleted() method if it exists. Prism.Avalonia takes care of this for you.

App.xaml.cs:

using System;
using Avalonia;
using Avalonia.Markup.Xaml;
using Prism.DryIoc;
using Prism.Ioc;
using Prism.Regions;

namespace SampleMvvmApp;

/// <summary>Application entry point.</summary>
public class App : PrismApplication
{
    public override void Initialize()
    {
        AvaloniaXamlLoader.Load(this);

        // Initializes Prism.Avalonia
        base.Initialize();
    }

    /// <summary>User interface entry point, called after Register and ConfigureModules.</summary>
    /// <returns>Startup View.</returns>
    protected override IAvaloniaObject CreateShell()
    {
        // Input your main shell window name
        return Container.Resolve<MainWindow>();
    }
}

Defining a View

Whether it's the shell Window or a UserControl a few base properties must be set in the root element. In an upcoming release, you no longer need to specify the AutoWireViewModel property, as it will be automatically detected.

  • xmlns:prism="http://prismlibrary.com/"
  • prism:ViewModelLocator.AutoWireViewModel="True"
  • x:Class="NAMESPACE.CLASSNAME"

Sample:

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:core="clr-namespace:SampleMvvmApp;assembly=SampleMvvmApp"
        xmlns:views="using:SampleMvvmApp.Views"
        xmlns:prism="http://prismlibrary.com/"
        prism:ViewModelLocator.AutoWireViewModel="True"
        x:Class="SampleMvvmApp.Views.MainWindow"
        Title="Sample Application"
        Height="500" Width="700">

RegisterTypes

To register your Services and/or Views to use Dependency Injection, you'll need to add the RegisterTypes(IContainerRegistry containerRegistry) method in your App class. In the next section, we'll use this method again for registering views for navigation.

    /// <summary>Register Services and Views.</summary>
    /// <param name="containerRegistry"></param>
    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
        // Services
        containerRegistry.RegisterSingleton<ILogService, LogService>();
    }

Sample Service:

public interface ILogService
{
    void Debug(string message);
}

public class LogService : ILogService
{
    public Debug(string message)
    {
        Console.WriteLine(message);
    }
}

Registering Views and ViewModels

In Avalonia, the UserControl is used for your View/Page. To get started, you're going to include the following in your RegisterTypes method in your App.xaml.cs file.

Next, you

App.xaml.cs

    /// <summary>Register Services and Views.</summary>
    /// <param name="containerRegistry"></param>
    protected override void RegisterTypes(IContainerRegistry containerRegistry)
    {
        ...

        // Views - Region Navigation
        containerRegistry.RegisterForNavigation<DashboardView, DashboardViewModel>();
    }

DashboardView.axaml

<UserControl xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:prism="http://prismlibrary.com/"
             xmlns:system="clr-namespace:System;assembly=mscorlib"
             xmlns:vm="using:SampleMvvmApp.ViewModels"
             x:Class="SampleMvvmApp.Views.DashboardView"
             x:CompileBindings="True"
             x:DataType="vm:DashboardViewModel">
  <StackPanel>
    <Label Content="Welcome to the Dashboard!" FontSize="32" />
    <Button Content="Test" Command="{Binding CmdTest}" />
  </StackPanel>
<UserControl>

DashboardView.axml.cs

using Avalonia.Controls;
using Avalonia.Markup.Xaml;

namespace SampleMvvmApp.Views;

/// <summary>DashboardView.</summary>
public partial class DashboardView : UserControl
{
    public DashboardView()
    {
        this.InitializeComponent();
    }

    private void InitializeComponent()
    {
        AvaloniaXamlLoader.Load(this);
    }
}

DashboardViewModel.cs

public class DashboardViewModel : BindableBase
{
}

Register For Navigation and RegionManager

To register a View for Navigation, we'll utilize Prism's Region feature.

    /// <summary>User interface entry point, called after Register and ConfigureModules.</summary>
    /// <returns>Startup View.</returns>
    protected override IAvaloniaObject CreateShell()
    {
        // Input your main shell window name
        return Container.Resolve<MainWindow>();
    }

    /// <summary>Called after Initialize.</summary>
    protected override void OnInitialized()
    {
        // Register Views to the Region it will appear in. Don't register them in the ViewModel.
        var regionManager = Container.Resolve<IRegionManager>();

        // Default views for Prism's Regions
        regionManager.RegisterViewWithRegion("ContentRegion", typeof(DashboardView));
    }
}

BONUS - PrismApplication Overridable Methods

When your Prism.Avalonia App starts, the following overridable methods are invoked inside your App class in this order:

  1. ConfigureViewModelLocator()
  2. Initialize();
  3. RegisterTypes(IContainerRegistry);
  4. ConfigureModuleCatalog(IModuleCatalog)
  5. ConfigureRegionAdapterMappings(RegionAdapterMappings)
  6. RegisterFrameworkExceptionTypes()
  7. CreateShell();
  8. OnInitialized();