Skip to content

Commit 7b83464

Browse files
authored
Merge pull request #61 from NeverMorewd/master
release 2.2.0
2 parents 9a4b340 + 0e2c6eb commit 7b83464

13 files changed

Lines changed: 96 additions & 64 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
<TabControl.ItemTemplate>
7373
<DataTemplate>
7474
<StackPanel Orientation="Horizontal" Spacing="2">
75-
<TextBlock Text="{Binding}" />
75+
<TextBlock Text="{Binding Alias}" />
7676
<Button
7777
Content="X"
7878
FontSize="11"

samples/Lemon.ModuleNavigation.SampleViewModel/BaseNavigationViewModel.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55

66
namespace Lemon.ModuleNavigation.SampleViewModel;
77

8-
public class BaseNavigationViewModel : ReactiveObject, INavigationAware
8+
public class BaseNavigationViewModel : ReactiveObject, INavigationAware, ICanUnload
99
{
1010
public virtual string Greeting => $"Welcome to {GetType().Name}[{Environment.ProcessId}][{Environment.CurrentManagedThreadId}]{Environment.NewLine}{DateTime.Now:yyyy-MM-dd HH-mm-ss.ffff}";
11-
11+
public virtual string? Alias => GetType().Name;
1212
public BaseNavigationViewModel()
1313
{
1414
UnloadViewCommand = ReactiveCommand.Create(() =>

samples/Lemon.ModuleNavigation.SampleViewModel/MainWindowViewModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using ReactiveUI;
44
using System.Diagnostics;
55
using System.Reactive;
6+
using System.Reflection;
67

78
namespace Lemon.ModuleNavigation.SampleViewModel;
89

samples/Lemon.ModuleNavigation.SampleViewModel/ViewAlphaViewModel.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public ViewAlphaViewModel()
1111
{
1212
}
1313

14+
public override string? Alias => "AlphaView";
1415
public string Title => nameof(ViewAlphaViewModel);
1516

1617
public event Action<IDialogResult>? RequestClose;

samples/Lemon.ModuleNavigation.SampleViewModel/ViewBetaViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Lemon.ModuleNavigation.SampleViewModel;
88
public class ViewBetaViewModel : BaseNavigationViewModel, IDialogAware
99
{
1010
public string Title => nameof(ViewBetaViewModel);
11-
11+
public override string? Alias => "BetaView";
1212
public event Action<IDialogResult>? RequestClose;
1313
public ReactiveCommand<Unit, Unit> CloseCommand => ReactiveCommand.Create(() =>
1414
{

samples/Lemon.ModuleNavigation.WpfSample/MainWindow.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
<TabControl.ItemTemplate>
5656
<DataTemplate>
5757
<StackPanel Orientation="Horizontal">
58-
<TextBlock Text="{Binding}" />
58+
<TextBlock Text="{Binding Alias}" />
5959
<Button
6060
Width="20"
6161
Height="20"

src/Lemon.ModuleNavigation.Avaloniaui/Regions/Region.cs

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -68,26 +68,40 @@ public virtual void ScrollIntoView(NavigationContext item)
6868
protected IView? ResolveView(NavigationContext context)
6969
{
7070
var view = context.View;
71+
INavigationAware? navigationAware = null;
72+
7173
if (view is null)
7274
{
7375
view = context.ServiceProvider.GetRequiredKeyedService<IView>(context.ViewName);
74-
var navigationAware = context.ServiceProvider.GetRequiredKeyedService<INavigationAware>(context.ViewName);
76+
navigationAware = context.ServiceProvider.GetRequiredKeyedService<INavigationAware>(context.ViewName);
77+
78+
view.DataContext = navigationAware;
7579

76-
if (Current.TryTakeData(out var previousData))
80+
if (navigationAware is ICanUnload canUnloadNavigationAware)
7781
{
78-
previousData.NavigationAware.OnNavigatedFrom(context);
82+
canUnloadNavigationAware.RequestUnload += () =>
83+
{
84+
DeActivate(context);
85+
};
7986
}
8087

81-
view.DataContext = navigationAware;
82-
navigationAware.OnNavigatedTo(context);
83-
navigationAware.RequestUnload += () =>
84-
{
85-
DeActivate(context);
86-
};
87-
Current.SetData((view, navigationAware));
8888
context.View = view;
8989
ViewCache.AddOrUpdate(context, view, (key, value) => view);
9090
}
91+
else
92+
{
93+
navigationAware = view.DataContext as INavigationAware
94+
?? context.ServiceProvider.GetRequiredKeyedService<INavigationAware>(context.ViewName);
95+
}
96+
if (Current.TryTakeData(out var previousData))
97+
{
98+
previousData.NavigationAware.OnNavigatedFrom(context);
99+
}
100+
101+
navigationAware.OnNavigatedTo(context);
102+
103+
Current.SetData((view, navigationAware));
104+
context.Alias = navigationAware.Alias;
91105
return view;
92106
}
93107

@@ -124,27 +138,7 @@ private IDataTemplate CreateRegionDataTemplate()
124138
{
125139
return null;
126140
}
127-
var view = context.View;
128-
if (view is null)
129-
{
130-
view = context.ServiceProvider.GetRequiredKeyedService<IView>(context.ViewName);
131-
var navigationAware = context.ServiceProvider.GetRequiredKeyedService<INavigationAware>(context.ViewName);
132-
133-
if (Current.TryTakeData(out var previousData))
134-
{
135-
previousData.NavigationAware.OnNavigatedFrom(context);
136-
}
137-
138-
view.DataContext = navigationAware;
139-
navigationAware.OnNavigatedTo(context);
140-
navigationAware.RequestUnload += () =>
141-
{
142-
DeActivate(context);
143-
};
144-
Current.SetData((view, navigationAware));
145-
context.View = view;
146-
ViewCache.AddOrUpdate(context, view, (key, value) => view);
147-
}
141+
var view = ResolveView(context);
148142
return view as Control;
149143
});
150144
}

src/Lemon.ModuleNavigation.Wpf/Regions/Region.cs

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,29 +68,44 @@ public virtual void ScrollIntoView(NavigationContext item)
6868
protected IView? ResolveView(NavigationContext context)
6969
{
7070
var view = context.View;
71+
INavigationAware? navigationAware;
72+
7173
if (view is null)
7274
{
7375
view = context.ServiceProvider.GetRequiredKeyedService<IView>(context.ViewName);
74-
var navigationAware = context.ServiceProvider.GetRequiredKeyedService<INavigationAware>(context.ViewName);
76+
navigationAware = context.ServiceProvider.GetRequiredKeyedService<INavigationAware>(context.ViewName);
77+
78+
view.DataContext = navigationAware;
7579

76-
if (Current.TryTakeData(out var previousData))
80+
if (navigationAware is ICanUnload canUnloadNavigationAware)
7781
{
78-
previousData.NavigationAware.OnNavigatedFrom(context);
82+
canUnloadNavigationAware.RequestUnload += () =>
83+
{
84+
DeActivate(context);
85+
};
7986
}
8087

81-
view.DataContext = navigationAware;
82-
navigationAware.OnNavigatedTo(context);
83-
navigationAware.RequestUnload += () =>
84-
{
85-
DeActivate(context);
86-
};
87-
Current.SetData((view, navigationAware));
8888
context.View = view;
8989
ViewCache.AddOrUpdate(context, view, (key, value) => view);
9090
}
91+
else
92+
{
93+
navigationAware = view.DataContext as INavigationAware
94+
?? context.ServiceProvider.GetRequiredKeyedService<INavigationAware>(context.ViewName);
95+
}
96+
97+
context.Alias = navigationAware?.Alias;
98+
if (Current.TryTakeData(out var previousData))
99+
{
100+
previousData.NavigationAware.OnNavigatedFrom(context);
101+
}
102+
navigationAware?.OnNavigatedTo(context);
103+
Current.SetData((view, navigationAware!));
104+
91105
return view;
92106
}
93107

108+
94109
protected virtual void WhenContextsAdded(IEnumerable<NavigationContext> contexts)
95110
{
96111

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace Lemon.ModuleNavigation.Abstractions;
2+
3+
public interface ICanUnload
4+
{
5+
event Action? RequestUnload;
6+
}

src/Lemon.ModuleNavigation/Abstractions/INavigationAware.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
public interface INavigationAware
44
{
5-
event Action? RequestUnload;
5+
string? Alias { get; }
66
void OnNavigatedTo(NavigationContext navigationContext);
77
bool IsNavigationTarget(NavigationContext navigationContext);
88
void OnNavigatedFrom(NavigationContext navigationContext);

0 commit comments

Comments
 (0)