Skip to content

Commit

Permalink
redundant view&viewModel of module
Browse files Browse the repository at this point in the history
  • Loading branch information
NeverMorewd committed Feb 9, 2025
1 parent dd023d2 commit c95897a
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
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
6 changes: 3 additions & 3 deletions src/Lemon.ModuleNavigation.Sample/Views/MainView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
<Border Classes="HSeparator" />
<ContentControl lm:NavigationExtension.ModuleContainerName="NContentControl" />
</StackPanel>
<!--<StackPanel Classes="ForContainerShow" Grid.Column="2">
<StackPanel Classes="ForContainerShow" Grid.Column="2">
<Label Content="TabControl" />
<Border Classes="HSeparator" />
<TabControl lm:NavigationExtension.ModuleContainerName="NTabControl">
Expand Down Expand Up @@ -93,7 +93,7 @@
Margin="2"
MaxHeight="600"
lm:NavigationExtension.ModuleContainerName="NListBox">
--><!-- https://github.com/AvaloniaUI/Avalonia/issues/17349 --><!--
<!-- https://github.com/AvaloniaUI/Avalonia/issues/17349 -->
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel />
Expand All @@ -105,7 +105,7 @@
<Label Content="TransitioningContentControl" />
<Border Classes="HSeparator" />
<TransitioningContentControl lm:NavigationExtension.ModuleContainerName="NTransitioningContentControl" />
</StackPanel>-->
</StackPanel>
</Grid>
</TabItem>
<TabItem Header="View">
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();
}
}
24 changes: 22 additions & 2 deletions src/Lemon.ModuleNavigation/Core/ModuleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ 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;
private readonly IRegionManager _regionManager;
public ModuleManager(IEnumerable<IModule> modules,
Expand Down Expand Up @@ -113,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;
}
}
}

0 comments on commit c95897a

Please sign in to comment.