Skip to content
Closed
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
2 changes: 1 addition & 1 deletion samples/Lemon.ModuleNavigation.Sample/Views/MainView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@
<TabControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Spacing="2">
<TextBlock Text="{Binding}" />
<TextBlock Text="{Binding Alias}" />
<Button
Content="X"
FontSize="11"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

namespace Lemon.ModuleNavigation.SampleViewModel;

public class BaseNavigationViewModel : ReactiveObject, INavigationAware
public class BaseNavigationViewModel : ReactiveObject, INavigationAware, ICanUnload
{
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 string? Alias => GetType().Name;
public BaseNavigationViewModel()
{
UnloadViewCommand = ReactiveCommand.Create(() =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using ReactiveUI;
using System.Diagnostics;
using System.Reactive;
using System.Reflection;

namespace Lemon.ModuleNavigation.SampleViewModel;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public ViewAlphaViewModel()
{
}

public override string? Alias => "AlphaView";
public string Title => nameof(ViewAlphaViewModel);

public event Action<IDialogResult>? RequestClose;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace Lemon.ModuleNavigation.SampleViewModel;
public class ViewBetaViewModel : BaseNavigationViewModel, IDialogAware
{
public string Title => nameof(ViewBetaViewModel);

public override string? Alias => "BetaView";
public event Action<IDialogResult>? RequestClose;
public ReactiveCommand<Unit, Unit> CloseCommand => ReactiveCommand.Create(() =>
{
Expand Down
2 changes: 1 addition & 1 deletion samples/Lemon.ModuleNavigation.WpfSample/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
<TabControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding}" />
<TextBlock Text="{Binding Alias}" />
<Button
Width="20"
Height="20"
Expand Down
56 changes: 25 additions & 31 deletions src/Lemon.ModuleNavigation.Avaloniaui/Regions/Region.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,26 +68,40 @@ public virtual void ScrollIntoView(NavigationContext item)
protected IView? ResolveView(NavigationContext context)
{
var view = context.View;
INavigationAware? navigationAware = null;

if (view is null)
{
view = context.ServiceProvider.GetRequiredKeyedService<IView>(context.ViewName);
var navigationAware = context.ServiceProvider.GetRequiredKeyedService<INavigationAware>(context.ViewName);
navigationAware = context.ServiceProvider.GetRequiredKeyedService<INavigationAware>(context.ViewName);

view.DataContext = navigationAware;

if (Current.TryTakeData(out var previousData))
if (navigationAware is ICanUnload canUnloadNavigationAware)
{
previousData.NavigationAware.OnNavigatedFrom(context);
canUnloadNavigationAware.RequestUnload += () =>
{
DeActivate(context);
};
}

view.DataContext = navigationAware;
navigationAware.OnNavigatedTo(context);
navigationAware.RequestUnload += () =>
{
DeActivate(context);
};
Current.SetData((view, navigationAware));
context.View = view;
ViewCache.AddOrUpdate(context, view, (key, value) => view);
}
else
{
navigationAware = view.DataContext as INavigationAware
?? context.ServiceProvider.GetRequiredKeyedService<INavigationAware>(context.ViewName);
}
if (Current.TryTakeData(out var previousData))
{
previousData.NavigationAware.OnNavigatedFrom(context);
}

navigationAware.OnNavigatedTo(context);

Current.SetData((view, navigationAware));
context.Alias = navigationAware.Alias;
return view;
}

Expand Down Expand Up @@ -124,27 +138,7 @@ private IDataTemplate CreateRegionDataTemplate()
{
return null;
}
var view = context.View;
if (view is null)
{
view = context.ServiceProvider.GetRequiredKeyedService<IView>(context.ViewName);
var navigationAware = context.ServiceProvider.GetRequiredKeyedService<INavigationAware>(context.ViewName);

if (Current.TryTakeData(out var previousData))
{
previousData.NavigationAware.OnNavigatedFrom(context);
}

view.DataContext = navigationAware;
navigationAware.OnNavigatedTo(context);
navigationAware.RequestUnload += () =>
{
DeActivate(context);
};
Current.SetData((view, navigationAware));
context.View = view;
ViewCache.AddOrUpdate(context, view, (key, value) => view);
}
var view = ResolveView(context);
return view as Control;
});
}
Expand Down
10 changes: 7 additions & 3 deletions src/Lemon.ModuleNavigation.Wpf/Regions/Region.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,14 @@ public virtual void ScrollIntoView(NavigationContext item)

view.DataContext = navigationAware;
navigationAware.OnNavigatedTo(context);
navigationAware.RequestUnload += () =>
context.Alias = navigationAware.Alias;
if (navigationAware is ICanUnload canUnloadNavigationAware)
{
DeActivate(context);
};
canUnloadNavigationAware.RequestUnload += () =>
{
DeActivate(context);
};
}
Current.SetData((view, navigationAware));
context.View = view;
ViewCache.AddOrUpdate(context, view, (key, value) => view);
Expand Down
6 changes: 6 additions & 0 deletions src/Lemon.ModuleNavigation/Abstractions/ICanUnload.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Lemon.ModuleNavigation.Abstractions;

public interface ICanUnload
{
event Action? RequestUnload;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public interface INavigationAware
{
event Action? RequestUnload;
string? Alias { get; }
void OnNavigatedTo(NavigationContext navigationContext);
bool IsNavigationTarget(NavigationContext navigationContext);
void OnNavigatedFrom(NavigationContext navigationContext);
Expand Down
3 changes: 0 additions & 3 deletions src/Lemon.ModuleNavigation/Abstractions/IRegionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ public interface IRegionManager : IObservable<NavigationContext>, IObservable<IR
{
void AddRegion(string regionName, IRegion region);
IRegion? GetRegion(string regionName);

void RequestViewNavigate(string regionName, string viewName, NavigationParameters? parameters = null);
[Obsolete("requestNew was obsolete.Consider IsNavigationTarget() in INavigationAware instead.")]
void RequestNavigate(string regionName, string viewName, bool requestNew, NavigationParameters? parameters = null);
void RequestViewUnload(string regionName, string viewName);
void RequestViewUnload(NavigationContext context);


void RequestModuleNavigate(string regionName, string moduleName, NavigationParameters? parameters);
void RequestModuleNavigate(string regionName, IModule module, NavigationParameters? parameters);
void RequestModuleUnload(string moduleName, string viewName);
Expand Down
44 changes: 31 additions & 13 deletions src/Lemon.ModuleNavigation/NavigationContext.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
using Lemon.ModuleNavigation.Abstractions;
using Lemon.ModuleNavigation.Core;
using System.ComponentModel;
using System.Runtime.CompilerServices;

namespace Lemon.ModuleNavigation;

public class NavigationContext
public class NavigationContext : INotifyPropertyChanged
{
[Obsolete("requestNew was obsolete.Consider IsNavigationTarget() in INavigationAware instead.")]
internal NavigationContext(string viewName,
internal NavigationContext(string viewName,
string regionName,
IServiceProvider serviceProvider,
bool requestNew,
Expand All @@ -30,15 +32,25 @@ internal NavigationContext(string viewName,
}
public static ViewNameComparer ViewNameComparer => new();
public static StrictComparer StrictComparer => new();
public string ViewName
{
get;
private set;
public string ViewName
{
get;
private set;
}
public NavigationParameters? Parameters
{
get;
private set;
private string? _alias;
public string? Alias
{
get => _alias;
set
{
_alias = value;
OnPropertyChanged();
}
}
public NavigationParameters? Parameters
{
get;
private set;
}
[Obsolete("requestNew was obsolete.Consider IsNavigationTarget() in INavigationAware instead.")]
public bool RequestNew
Expand All @@ -47,9 +59,9 @@ public bool RequestNew
private set;
}
public string RegionName
{
get;
private set;
{
get;
private set;
}
public int Key => GetHashCode();
public IServiceProvider ServiceProvider
Expand All @@ -65,6 +77,12 @@ public override string ToString()
{
return $"{RegionName}.{ViewName}";
}

public event PropertyChangedEventHandler? PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class ViewNameComparer : IEqualityComparer<NavigationContext>
{
Expand Down
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.1.2-rc</Version>
<Version>2.2.0</Version>
<Authors>Easley</Authors>
<RepositoryUrl>https://github.com/NeverMorewd/Lemon.ModuleNavigation</RepositoryUrl>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
Expand Down