Skip to content

Commit c95897a

Browse files
committed
redundant view&viewModel of module
1 parent dd023d2 commit c95897a

4 files changed

Lines changed: 35 additions & 7 deletions

File tree

src/Lemon.ModuleNavigation.Avaloniaui/Core/NavigationExtension.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ private static void SetBinding(Control control,
213213
{
214214
return null;
215215
}
216-
return navigationHandler.ModuleManager.CreateView(m) as Control;
216+
return navigationHandler.ModuleManager.GetOrCreateView(m, regionName) as Control;
217217
});
218218
}
219219
else if (control is ItemsControl itemsControl)
@@ -248,7 +248,7 @@ private static void SetBinding(Control control,
248248
{
249249
return null;
250250
}
251-
return navigationHandler.ModuleManager.CreateView(m) as Control;
251+
return navigationHandler.ModuleManager.GetOrCreateView(m, regionName) as Control;
252252
});
253253
}
254254
else if (control is ContentControl contentControl)

src/Lemon.ModuleNavigation.Sample/Views/MainView.axaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
<Border Classes="HSeparator" />
6262
<ContentControl lm:NavigationExtension.ModuleContainerName="NContentControl" />
6363
</StackPanel>
64-
<!--<StackPanel Classes="ForContainerShow" Grid.Column="2">
64+
<StackPanel Classes="ForContainerShow" Grid.Column="2">
6565
<Label Content="TabControl" />
6666
<Border Classes="HSeparator" />
6767
<TabControl lm:NavigationExtension.ModuleContainerName="NTabControl">
@@ -93,7 +93,7 @@
9393
Margin="2"
9494
MaxHeight="600"
9595
lm:NavigationExtension.ModuleContainerName="NListBox">
96-
--><!-- https://github.com/AvaloniaUI/Avalonia/issues/17349 --><!--
96+
<!-- https://github.com/AvaloniaUI/Avalonia/issues/17349 -->
9797
<ListBox.ItemsPanel>
9898
<ItemsPanelTemplate>
9999
<StackPanel />
@@ -105,7 +105,7 @@
105105
<Label Content="TransitioningContentControl" />
106106
<Border Classes="HSeparator" />
107107
<TransitioningContentControl lm:NavigationExtension.ModuleContainerName="NTransitioningContentControl" />
108-
</StackPanel>-->
108+
</StackPanel>
109109
</Grid>
110110
</TabItem>
111111
<TabItem Header="View">

src/Lemon.ModuleNavigation/Abstracts/IModule.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ public Type ViewModelType
3939
{
4040
get;
4141
}
42+
public IView? View
43+
{
44+
get;
45+
}
46+
public IModuleNavigationAware? ViewModel
47+
{
48+
get;
49+
}
4250
public void Initialize();
4351
}
4452
}

src/Lemon.ModuleNavigation/Core/ModuleManager.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Lemon.ModuleNavigation.Core
1010
public class ModuleManager : IModuleManager, INotifyPropertyChanged
1111
{
1212
private readonly ConcurrentDictionary<string, IModule> _modulesCache;
13-
private readonly ConcurrentDictionary<(string, string), IView> _regionCache;
13+
private readonly ConcurrentDictionary<(string RegionName, string ModuleKey), IView> _regionCache;
1414
private readonly IServiceProvider _serviceProvider;
1515
private readonly IRegionManager _regionManager;
1616
public ModuleManager(IEnumerable<IModule> modules,
@@ -113,10 +113,30 @@ public IView GetOrCreateView(IModule module, string regionName)
113113
}
114114
else
115115
{
116-
var view = CreateView(module);
116+
IView view;
117+
if (!IsRenderedOnAnyRegion(module.Key))
118+
{
119+
view = module.View!;
120+
}
121+
else
122+
{
123+
view = CreateView(module);
124+
}
117125
_regionCache.TryAdd((regionName, module.Key), view);
118126
return view;
119127
}
120128
}
129+
130+
private bool IsRenderedOnAnyRegion(string moduleKey)
131+
{
132+
if (!_regionCache.IsEmpty)
133+
{
134+
foreach (var item in _regionCache)
135+
{
136+
return (item.Key.ModuleKey == moduleKey);
137+
}
138+
}
139+
return false;
140+
}
121141
}
122142
}

0 commit comments

Comments
 (0)