-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathContentRegion.cs
More file actions
117 lines (109 loc) · 3.33 KB
/
ContentRegion.cs
File metadata and controls
117 lines (109 loc) · 3.33 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
using Avalonia.Controls;
using Avalonia.Controls.Templates;
using Avalonia.Data;
using Avalonia.Markup.Xaml.Templates;
using Lemon.ModuleNavigation.Abstractions;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace Lemon.ModuleNavigation.Avaloniaui.Regions;
public class ContentRegion : Region, IContentRegionContext<IDataTemplate>
{
private readonly ContentControl _contentControl;
public ContentRegion(string name, ContentControl contentControl) : base(name)
{
_contentControl = contentControl;
SetBindingContentTemplate();
SetBindingContent();
}
private object? _content;
public object? Content
{
get => _content;
set
{
_content = value;
OnPropertyChanged();
}
}
private IDataTemplate? _contentTemplate;
public IDataTemplate? ContentTemplate
{
get => _contentTemplate;
set
{
_contentTemplate = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler? PropertyChanged;
/// <summary>
/// When Views with same ViewName were found, the latest one will be picked.
/// </summary>
/// <param name="target"></param>
public override void Activate(NavigationContext target)
{
if (ViewCache.TryGetValue(target, out IView? accurateView))
{
target.View = accurateView;
Content = target;
}
else if (ViewNameCache.TryGetValue(target.ViewName, out IView? view)
&& view.DataContext is INavigationAware navigationAware
&& navigationAware.IsNavigationTarget(target))
{
var context = Contexts.First(c => c.ViewName == target.ViewName);
context.View = view;
Content = context;
}
else
{
Contexts.Add(target);
Content = target;
}
}
public override void DeActivate(string regionName)
{
if (Content is NavigationContext current)
{
if (current.ViewName == regionName)
{
Contexts.Remove(current);
Content = null;
}
}
}
public override void DeActivate(NavigationContext navigationContext)
{
if (Content is NavigationContext current)
{
if (NavigationContext.ViewNameComparer.Equals(current, navigationContext))
{
Contexts.Remove(current);
Content = null;
}
}
}
protected virtual void SetBindingContentTemplate()
{
ContentTemplate = RegionContentTemplate;
_contentControl.Bind(ContentControl.ContentTemplateProperty,
new Binding(nameof(ContentTemplate))
{
Source = this,
Mode = BindingMode.TwoWay
});
}
protected virtual void SetBindingContent()
{
_contentControl.Bind(ContentControl.ContentProperty,
new Binding(nameof(Content))
{
Source = this,
Mode = BindingMode.TwoWay
});
}
protected void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}