Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ private static void SetBinding(Control control,
{
return null;
}
return navigationHandler.ModuleManager.CreateView(m) as Control;
return navigationHandler.ModuleManager.GetOrCreateView(m, regionName) as Control;
});
}
else if (control is ItemsControl itemsControl)
Expand Down Expand Up @@ -248,7 +248,7 @@ private static void SetBinding(Control control,
{
return null;
}
return navigationHandler.ModuleManager.CreateView(m) as Control;
return navigationHandler.ModuleManager.GetOrCreateView(m, regionName) as Control;
});
}
else if (control is ContentControl contentControl)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public static void Main(string[] args)
// views
hostBuilder.Services.AddView<ViewAlpha, ViewAlphaViewModel>(nameof(ViewAlpha));
hostBuilder.Services.AddView<ViewBeta, ViewBetaViewModel>(nameof(ViewBeta));
hostBuilder.Services.AddAvaDialogWindow<CustomDialogWindow>(nameof(CustomDialogWindow));

hostBuilder.Services.AddAvaloniauiDesktopApplication<App>(BuildAvaloniaApp);
hostBuilder.Services.AddMainWindow<MainWindow, MainViewModel>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"profiles": {
"Lemon.ModuleNavigation.Sample.DesktopHosting": {
"commandName": "Project"
},
"WSL": {
"commandName": "WSL2",
"distributionName": ""
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Lemon.ModuleNavigation.Sample.ViewModels;

public class SampleViewModelBase : ReactiveObject, IDisposable
{
public virtual string Greeting => $"Welcome to {GetType().Name}{Environment.NewLine}{DateTime.Now:yyyy-MM-dd HH-mm-ss.ffff}";
public virtual string Greeting => $"Welcome to {GetType().Name}[{Environment.ProcessId}][{Environment.CurrentManagedThreadId}]{Environment.NewLine}{DateTime.Now:yyyy-MM-dd HH-mm-ss.ffff}";
public virtual void Dispose()
{

Expand Down
8 changes: 8 additions & 0 deletions src/Lemon.ModuleNavigation/Abstracts/IModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ public Type ViewModelType
{
get;
}
public IView? View
{
get;
}
public IModuleNavigationAware? ViewModel
{
get;
}
public void Initialize();
}
}
32 changes: 27 additions & 5 deletions src/Lemon.ModuleNavigation/Core/ModuleManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Lemon.ModuleNavigation.Abstracts;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Concurrent;
using System.Collections.ObjectModel;
using System.ComponentModel;
Expand All @@ -11,11 +10,15 @@ namespace Lemon.ModuleNavigation.Core
public class ModuleManager : IModuleManager, INotifyPropertyChanged
{
private readonly ConcurrentDictionary<string, IModule> _modulesCache;
private readonly ConcurrentDictionary<(string, string), IView> _regionCache;
private readonly ConcurrentDictionary<(string RegionName, string ModuleKey), IView> _regionCache;
private readonly IServiceProvider _serviceProvider;
public ModuleManager(IEnumerable<IModule> modules, IServiceProvider serviceProvider)
private readonly IRegionManager _regionManager;
public ModuleManager(IEnumerable<IModule> modules,
IRegionManager regionManager,
IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
_regionManager = regionManager;
_regionCache = [];
_modulesCache = new ConcurrentDictionary<string, IModule>(modules.ToDictionary(m => m.Key, m => m));
Modules = _modulesCache.Values;
Expand Down Expand Up @@ -85,7 +88,6 @@ public void RequestNavigate(IModule module, NavigationParameters parameters)
ActiveModules.Add(module);
}
}

///TODO:Consider an async implementation
module.Initialize();
module.IsActivated = true;
Expand All @@ -111,10 +113,30 @@ public IView GetOrCreateView(IModule module, string regionName)
}
else
{
var view = CreateView(module);
IView view;
if (!IsRenderedOnAnyRegion(module.Key))
{
view = module.View!;
}
else
{
view = CreateView(module);
}
_regionCache.TryAdd((regionName, module.Key), view);
return view;
}
}

private bool IsRenderedOnAnyRegion(string moduleKey)
{
if (!_regionCache.IsEmpty)
{
foreach (var item in _regionCache)
{
return (item.Key.ModuleKey == moduleKey);
}
}
return false;
}
}
}
2 changes: 1 addition & 1 deletion src/Package.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>2.0.7-beta</Version>
<Version>2.0.8-beta</Version>
<Authors>Easley</Authors>
<RepositoryUrl>https://github.com/NeverMorewd/Lemon.ModuleNavigation</RepositoryUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand Down
Loading