-
Notifications
You must be signed in to change notification settings - Fork 42
Getting Started
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.
Prism.Avalonia requires the following Packages to be installed. Currently, only DryIoc is supported.
Package | NuGet |
---|---|
Prism.Avalonia | |
Prism.DryIoc.Avalonia |
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.
- Update your
App
class to inherit fromPrismApplication
instead of,Application
- Add an
Initialize()
method and addbase.Initialize();
-
base.Initialize();
is required to begin the initialization of Prism.Avalonia
-
- 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>();
}
}
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">
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);
}
}
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
{
}
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));
}
}
When your Prism.Avalonia App
starts, the following overridable methods are invoked inside your App
class in this order:
ConfigureViewModelLocator()
Initialize();
RegisterTypes(IContainerRegistry);
ConfigureModuleCatalog(IModuleCatalog)
ConfigureRegionAdapterMappings(RegionAdapterMappings)
RegisterFrameworkExceptionTypes()
CreateShell();
OnInitialized();