-
Notifications
You must be signed in to change notification settings - Fork 45
Getting Started
Coming Soon
Until we get our own Visual Studio Template extension completed, it's recommended to use the Avalonia Template to make adding Window and UserControl views easier - VS 2019/2017, VS 2022.
Though the Main
entry point of your Avalonia application remains the same, you must make a few minor changes to your App.xaml.cs
file.
When your Prism.Avalonia app starts up, the following 3 methods are invoked inside your App
class in this order:
ConfigureViewModelLocator()
Initialize();
OnInitialized();
-
partial class App
- Inherit fromPrismApplication
instead of,Application
-
Initialize()
method, addbase.Initialize();
- Remove
OnFrameworkInitializationCompleted()
method. Prism.Avalonia takes care of this for you.
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>Register Services and Views.</summary>
/// <param name="containerRegistry"></param>
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
// Services
containerRegistry.RegisterSingleton<INotificationService, NotificationService>();
// Views - Region Navigation
containerRegistry.RegisterForNavigation<DashboardView, DashboardViewModel>(); }
/// <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(RegionNames.ContentRegion, typeof(DashboardView));
}
}
In Avalonia, the UserControl
is used for your View/Page. To get started, you
Whether it's the main Window
or a UserControl
the following base properties must be set in the root element.
In upcoming releases, 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">