-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRegion.cs
More file actions
157 lines (144 loc) · 4.76 KB
/
Region.cs
File metadata and controls
157 lines (144 loc) · 4.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using Lemon.ModuleNavigation.Abstractions;
using Lemon.ModuleNavigation.Core;
using Microsoft.Extensions.DependencyInjection;
using System.Collections.Concurrent;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Windows;
using System.Windows.Controls;
namespace Lemon.ModuleNavigation.Wpf.Regions;
public abstract class Region : IRegion
{
public Region(string name)
{
Name = name;
Current = new();
ViewCache = [];
Contexts = [];
RegionContentTemplate = CreateRegionDataTemplate();
Contexts.CollectionChanged += Contexts_CollectionChanged;
}
protected ConcurrentItem<(IView View, INavigationAware NavigationAware)> Current
{
get;
}
public string Name
{
get;
}
protected ConcurrentDictionary<NavigationContext, IView> ViewCache
{
get;
}
protected ConcurrentDictionary<string, IView> ViewNameCache
{
get
{
return new(Contexts
.GroupBy(kv => kv.ViewName)
.Select(group => new KeyValuePair<string, IView>(
group.Key,
group.Last().View!)));
}
}
public ObservableCollection<NavigationContext> Contexts
{
get;
}
public DataTemplate? RegionContentTemplate
{
get;
}
public virtual void ScrollIntoView(int index)
{
throw new NotImplementedException();
}
public virtual void ScrollIntoView(NavigationContext item)
{
throw new NotImplementedException();
}
public abstract void Activate(NavigationContext target);
public abstract void DeActivate(string viewName);
public abstract void DeActivate(NavigationContext target);
protected IView? ResolveView(NavigationContext context)
{
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);
context.Alias = navigationAware.Alias;
if (navigationAware is ICanUnload canUnloadNavigationAware)
{
canUnloadNavigationAware.RequestUnload += () =>
{
DeActivate(context);
};
}
Current.SetData((view, navigationAware));
context.View = view;
ViewCache.AddOrUpdate(context, view, (key, value) => view);
}
return view;
}
protected virtual void WhenContextsAdded(IEnumerable<NavigationContext> contexts)
{
}
protected virtual void WhenContextsRemoved(IEnumerable<NavigationContext> contexts)
{
}
private void Contexts_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action == NotifyCollectionChangedAction.Add)
{
if (e.NewItems is not null)
{
WhenContextsAdded(e.NewItems.Cast<NavigationContext>());
}
}
if (e.Action == NotifyCollectionChangedAction.Remove)
{
if (e.OldItems is not null)
{
WhenContextsRemoved(e.OldItems.Cast<NavigationContext>());
}
}
}
private DataTemplate CreateRegionDataTemplate()
{
var dataTemplate = new DataTemplate(typeof(NavigationContext));
FrameworkElementFactory factory = new(typeof(ContentPresenter));
factory.SetBinding(ContentPresenter.ContentProperty, new System.Windows.Data.Binding()
{
Converter = new NavigationContextToViewConverter(this)
});
dataTemplate.VisualTree = factory;
return dataTemplate;
}
private class NavigationContextToViewConverter : System.Windows.Data.IValueConverter
{
private readonly Region _region;
public NavigationContextToViewConverter(Region region)
{
_region = region;
}
public object? Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value is NavigationContext context)
{
return _region.ResolveView(context);
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
}