-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
ContentPage.cs
142 lines (118 loc) · 4.29 KB
/
ContentPage.cs
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
#nullable disable
using System;
using Microsoft.Maui.Graphics;
using Microsoft.Maui.HotReload;
using Microsoft.Maui.Layouts;
namespace Microsoft.Maui.Controls
{
/// <include file="../../docs/Microsoft.Maui.Controls/ContentPage.xml" path="Type[@FullName='Microsoft.Maui.Controls.ContentPage']/Docs/*" />
[ContentProperty("Content")]
public partial class ContentPage : TemplatedPage, IContentView, HotReload.IHotReloadableView
{
/// <summary>Bindable property for <see cref="Content"/>.</summary>
public static readonly BindableProperty ContentProperty = BindableProperty.Create(nameof(Content), typeof(View), typeof(ContentPage), null, propertyChanged: TemplateUtilities.OnContentChanged);
/// <include file="../../docs/Microsoft.Maui.Controls/ContentPage.xml" path="//Member[@MemberName='Content']/Docs/*" />
public View Content
{
get { return (View)GetValue(ContentProperty); }
set { SetValue(ContentProperty, value); }
}
/// <summary>Bindable property for <see cref="HideSoftInputOnTapped"/>.</summary>
public static readonly BindableProperty HideSoftInputOnTappedProperty
= BindableProperty.Create(nameof(HideSoftInputOnTapped), typeof(bool), typeof(ContentPage), false);
/// <summary>
/// Gets or sets a value that indicates whether tapping anywhere on the page will cause the soft input to hide.
/// </summary>
public bool HideSoftInputOnTapped
{
get { return (bool)GetValue(HideSoftInputOnTappedProperty); }
set { SetValue(HideSoftInputOnTappedProperty, value); }
}
public ContentPage()
{
this.NavigatedTo += (_, _) => UpdateHideSoftInputOnTapped();
}
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
View content = Content;
ControlTemplate controlTemplate = ControlTemplate;
if (content != null && controlTemplate != null)
{
SetInheritedBindingContext(content, BindingContext);
}
}
internal override void OnControlTemplateChanged(ControlTemplate oldValue, ControlTemplate newValue)
{
if (oldValue == null)
return;
base.OnControlTemplateChanged(oldValue, newValue);
View content = Content;
ControlTemplate controlTemplate = ControlTemplate;
if (content != null && controlTemplate != null)
{
SetInheritedBindingContext(content, BindingContext);
}
}
object IContentView.Content => Content;
IView IContentView.PresentedContent => ((this as IControlTemplated).TemplateRoot as IView) ?? Content;
protected override Size MeasureOverride(double widthConstraint, double heightConstraint)
{
return this.ComputeDesiredSize(widthConstraint, heightConstraint);
}
protected override Size ArrangeOverride(Rect bounds)
{
Frame = this.ComputeFrame(bounds);
Handler?.PlatformArrange(Frame);
return Frame.Size;
}
Size ICrossPlatformLayout.CrossPlatformMeasure(double widthConstraint, double heightConstraint)
{
_ = this.MeasureContent(widthConstraint, heightConstraint);
return new Size(widthConstraint, heightConstraint);
}
Size ICrossPlatformLayout.CrossPlatformArrange(Rect bounds)
{
Frame = bounds;
this.ArrangeContent(bounds);
return bounds.Size;
}
protected override void InvalidateMeasureOverride()
{
base.InvalidateMeasureOverride();
if (Content is IView view)
{
view.InvalidateMeasure();
}
}
#region HotReload
IView IReplaceableView.ReplacedView => HotReload.MauiHotReloadHelper.GetReplacedView(this) ?? this;
HotReload.IReloadHandler HotReload.IHotReloadableView.ReloadHandler { get; set; }
void HotReload.IHotReloadableView.TransferState(IView newView)
{
//TODO: Let you hot reload the the ViewModel
//TODO: Lets do a real state transfer
if (newView is View v)
v.BindingContext = BindingContext;
}
void HotReload.IHotReloadableView.Reload()
{
Dispatcher.Dispatch(() =>
{
this.CheckHandlers();
var reloadHandler = ((IHotReloadableView)this).ReloadHandler;
reloadHandler?.Reload();
//TODO: if reload handler is null, Do a manual reload?
});
}
#endregion
Size IContentView.CrossPlatformArrange(Rect bounds)
{
return (this as ICrossPlatformLayout).CrossPlatformArrange(bounds);
}
Size IContentView.CrossPlatformMeasure(double widthConstraint, double heightConstraint)
{
return (this as ICrossPlatformLayout).CrossPlatformMeasure(widthConstraint, heightConstraint);
}
}
}