Skip to content

Commit

Permalink
No longer require overriding Initialize() or RegisterTypes() in A…
Browse files Browse the repository at this point in the history
…pp.axaml.cs
  • Loading branch information
DamianSuess committed Aug 24, 2024
1 parent 30a4225 commit b3e2028
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 66 deletions.
2 changes: 1 addition & 1 deletion build/Base.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project DefaultTargets="Build"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Version>9.0.537.11130</Version>
<Version>9.0.537.11131</Version>
<PackageProjectUrl>https://github.com/AvaloniaCommunity/Prism.Avalonia</PackageProjectUrl>
<Copyright>Copyright (c) 2024 Xeno Innovations, Inc.</Copyright>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand Down
50 changes: 1 addition & 49 deletions e2e/SampleBaseApp/App.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using Avalonia;
using Avalonia.Markup.Xaml;
using Prism.DryIoc;
using Prism.Ioc;
using SampleBaseApp.Views;
Expand All @@ -9,56 +8,9 @@ namespace SampleBaseApp;

public partial class App : PrismApplication
{
public override void Initialize()
{
Console.WriteLine("Initialize()");

AvaloniaXamlLoader.Load(this);

// Required to initialize Prism.Avalonia - DO NOT REMOVE
base.Initialize();
}

protected override AvaloniaObject CreateShell()
{
Console.WriteLine("CreateShell()");

System.Diagnostics.Debug.WriteLine("CreateShell()");
return Container.Resolve<MainWindow>();
}

protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
// Add Services and ViewModel registrations here

Console.WriteLine("RegisterTypes()");

// -=[ Sample ]=-
//
// // Services
// containerRegistry.RegisterSingleton<INotificationService, NotificationService>();
//
// // Views - Region Navigation
// containerRegistry.RegisterForNavigation<DashboardView, DashboardViewModel>();
}

/**
* NOT NEEDED:
* The following is used by vanilla Avalonia. Prism.Avalonia only needs, `CreateShell()`
*
public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
// Line below is needed to remove Avalonia data validation.
// Without this line you will get duplicate validations from both Avalonia and CT
BindingPlugins.DataValidators.RemoveAt(0);
desktop.MainWindow = new MainWindow
{
DataContext = new MainWindowViewModel(),
};
}
base.OnFrameworkInitializationCompleted();
}
*/
}
7 changes: 4 additions & 3 deletions e2e/SampleDialogApp/App.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using Avalonia;
using Avalonia.Markup.Xaml;
using Prism.DryIoc;
using Prism.Ioc;
using SampleDialogApp.ViewModels;
Expand All @@ -10,11 +9,13 @@ namespace SampleDialogApp;

public partial class App : PrismApplication
{
/*
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
base.Initialize(); // Required to initialize Prism.Avalonia - DO NOT REMOVE
// Required when overriding Initialize()
base.Initialize();
}
*/

protected override AvaloniaObject CreateShell()
{
Expand Down
23 changes: 15 additions & 8 deletions e2e/SampleMvvmApp/App.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Diagnostics;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Prism.DryIoc;
using Prism.Ioc;
using Prism.Modularity;
Expand All @@ -20,10 +19,9 @@ namespace SampleMvvmApp;
/// </summary>
public class App : PrismApplication
{
/// <summary>App entry point.</summary>
public App()
{
Console.WriteLine("Constructor()");
Debug.WriteLine("Constructor()");
}

// Note:
Expand All @@ -32,8 +30,10 @@ public App()
// Therefore, we need this as a `public override void` in PrismApplicationBase.cs
public override void Initialize()
{
Console.WriteLine("Initialize()");
AvaloniaXamlLoader.Load(this);
Debug.WriteLine("Initialize()");

// Only required when a constructor is defined
Avalonia.Markup.Xaml.AvaloniaXamlLoader.Load(this);

// Initializes Prism.Avalonia - DO NOT REMOVE
base.Initialize();
Expand All @@ -43,7 +43,7 @@ public override void Initialize()
/// <param name="containerRegistry"></param>
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
Console.WriteLine("RegisterTypes()");
Debug.WriteLine("RegisterTypes()");

// Services
containerRegistry.RegisterSingleton<INotificationService, NotificationService>();
Expand All @@ -62,20 +62,25 @@ protected override void RegisterTypes(IContainerRegistry containerRegistry)
/// <param name="moduleCatalog">Module Catalog.</param>
protected override void ConfigureModuleCatalog(IModuleCatalog moduleCatalog)
{
Debug.WriteLine("ConfigureModuleCatalog()");

base.ConfigureModuleCatalog(moduleCatalog);
}

/// <summary>User interface entry point, called after Register and ConfigureModules.</summary>
/// <returns>Startup View.</returns>
protected override AvaloniaObject CreateShell()
{
Console.WriteLine("CreateShell()");
Debug.WriteLine("CreateShell()");

return Container.Resolve<MainWindow>();
}

/// <summary>Called after Initialize.</summary>
protected override void OnInitialized()
{
Debug.WriteLine("OnInitialized()");

// Register Views to the Region it will appear in. Don't register them in the ViewModel.
var regionManager = Container.Resolve<IRegionManager>();

Expand All @@ -94,6 +99,8 @@ protected override void OnInitialized()

protected override void ConfigureRegionAdapterMappings(RegionAdapterMappings regionAdapterMappings)
{
Debug.WriteLine("ConfigureRegionAdapterMappings()");

regionAdapterMappings.RegisterMapping<ItemsControl, SampleMvvmApp.RegionAdapters.ItemsControlRegionAdapter>();
regionAdapterMappings.RegisterMapping<ContentControl, ContentControlRegionAdapter>();
}
Expand Down
7 changes: 3 additions & 4 deletions e2e/ViewDiscovery/App.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using Avalonia;
using Avalonia;
using Avalonia.Markup.Xaml;
using Prism.DryIoc;
using Prism.Ioc;
Expand All @@ -17,8 +16,8 @@ public App()
public override void Initialize()
{
System.Diagnostics.Debug.WriteLine("Initialize()");
AvaloniaXamlLoader.Load(this);
base.Initialize();
AvaloniaXamlLoader.Load(this); // Only required when overriding constructor
base.Initialize(); // Must include when overriding
}

protected override void RegisterTypes(IContainerRegistry containerRegistry)
Expand Down
13 changes: 12 additions & 1 deletion src/Prism.Avalonia/PrismApplicationBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using Prism.Common;
using Prism.Ioc;
using Prism.Modularity;
Expand Down Expand Up @@ -48,6 +49,14 @@ public override void Initialize()
{
base.Initialize();

try
{
AvaloniaXamlLoader.Load(this);
}
catch
{
}

ConfigureViewModelLocator();

ContainerLocator.SetContainerExtension(CreateContainerExtension());
Expand Down Expand Up @@ -119,7 +128,9 @@ protected virtual void RegisterRequiredTypes(IContainerRegistry containerRegistr
/// <summary>
/// Used to register types with the container that will be used by your application.
/// </summary>
protected abstract void RegisterTypes(IContainerRegistry containerRegistry);
protected virtual void RegisterTypes(IContainerRegistry containerRegistry)
{
}

/// <summary>
/// Configures the <see cref="IRegionBehaviorFactory"/>.
Expand Down

0 comments on commit b3e2028

Please sign in to comment.