Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
FadeView.cs
2{
7 public class FadeView : ContentView
8 {
9 private const uint durationInMs = 250 / 2;
10 private readonly Easing easingType = Easing.Linear;
11
12 private readonly Grid contentHolder;
13 private View? frontView;
14 private View? backView;
15 private bool isFading;
16
20 public FadeView()
21 {
22 this.contentHolder = new Grid { BackgroundColor = Colors.Transparent };
23 this.Content = this.contentHolder;
24 this.BackgroundColor = Colors.Transparent;
25 }
26
30 public static readonly BindableProperty IsFrontViewProperty =
31 BindableProperty.Create(nameof(IsFrontView), typeof(bool), typeof(FadeView), true, propertyChanged: (b, oldValue, newValue) =>
32 {
33 FadeView fv = (FadeView)b;
34
35 if (!fv.isFading)
36 {
37 if (fv.IsFrontView)
38 fv.FadeFromBackToFront();
39 else
40 fv.FadeFromFrontToBack();
41 }
42 });
43
44
48 public bool IsFrontView
49 {
50 get => (bool)this.GetValue(IsFrontViewProperty);
51 set => this.SetValue(IsFrontViewProperty, value);
52 }
53
57 public static readonly BindableProperty FrontViewProperty =
58 BindableProperty.Create(nameof(FrontView), typeof(View), typeof(FadeView), default(View), propertyChanged: (b, oldValue, newValue) =>
59 {
60 FadeView fv = (FadeView)b;
61 if (newValue is not null)
62 {
63 if (fv.frontView is not null)
64 {
65 fv.contentHolder.Children.Remove(fv.frontView);
66 }
67 fv.frontView = (View)newValue;
68
69 fv.AddChild(fv.frontView);
70 }
71 else
72 {
73 if (fv.frontView is not null)
74 {
75 fv.contentHolder.Children.Remove(fv.frontView);
76 }
77
78 fv.frontView = null;
79 }
80
81 fv.OrganizeViews();
82 });
83
87 public View? FrontView
88 {
89 get => (View?)this.GetValue(FrontViewProperty);
90 set => this.SetValue(FrontViewProperty, value);
91 }
92
96 public static readonly BindableProperty BackViewProperty =
97 BindableProperty.Create(nameof(BackView), typeof(View), typeof(FadeView), default(View), propertyChanged: (b, oldValue, newValue) =>
98 {
99 FadeView fv = (FadeView)b;
100 if (newValue is not null)
101 {
102 if (fv.backView is not null)
103 {
104 fv.contentHolder.Children.Remove(fv.backView);
105 }
106
107 fv.backView = (View)newValue;
108 fv.AddChild(fv.backView);
109 }
110 else
111 {
112 if (fv.backView is not null)
113 {
114 fv.contentHolder.Children.Remove(fv.backView);
115 }
116
117 fv.backView = null;
118 }
119
120 fv.OrganizeViews();
121 });
122
126 public View? BackView
127 {
128 get => (View?)this.GetValue(BackViewProperty);
129 set => this.SetValue(BackViewProperty, value);
130 }
131
132 private void AddChild(View childView)
133 {
134 this.contentHolder.Children.Add(childView);
135 }
136
137 private void OrganizeViews()
138 {
139 if (this.backView is not null && this.frontView is not null)
140 {
141 //this.contentHolder.RaiseChild(this.IsFrontView ? this.frontView : this.backView);
142 }
143 }
144
145 private async void FadeFromFrontToBack()
146 {
147 this.isFading = true;
148
149 await this.FadeFrontToBack1();
150
151 // Change the visible content
152 this.OrganizeViews();
153
154 await this.FadeFrontToBack2();
155
156 this.isFading = false;
157 }
158
159 private async void FadeFromBackToFront()
160 {
161 this.isFading = true;
162
163 await this.FadeBackToFront1();
164
165 // Change the visible content
166 this.OrganizeViews();
167
168 await this.FadeBackToFront2();
169
170 this.isFading = false;
171 }
172
173 #region Animation
174
175 private async Task FadeFrontToBack1()
176 {
177 this.FrontView!.CancelAnimations();
178 this.BackView!.CancelAnimations();
179
180 this.FrontView!.Opacity = 1;
181 this.BackView!.Opacity = 0;
182
183 List<Task> tasks =
184 [
185 Task.Run(async () => await this.FrontView.FadeTo(0.5, durationInMs, this.easingType)),
186 Task.Run(async () => await this.BackView.FadeTo(0.5, durationInMs, this.easingType))
187 ];
188
189 await Task.WhenAll(tasks);
190 }
191
192 private async Task FadeFrontToBack2()
193 {
194 this.FrontView!.CancelAnimations();
195 this.BackView!.CancelAnimations();
196
197 this.FrontView!.Opacity = 0.5;
198 this.BackView!.Opacity = 0.5;
199
200 List<Task> tasks =
201 [
202 Task.Run(async () => await this.FrontView.FadeTo(0, durationInMs, this.easingType)),
203 Task.Run(async () => await this.BackView.FadeTo(1, durationInMs, this.easingType))
204 ];
205
206 await Task.WhenAll(tasks);
207 }
208
209
210 private async Task FadeBackToFront1()
211 {
212 this.FrontView!.CancelAnimations();
213 this.BackView!.CancelAnimations();
214
215 this.FrontView!.Opacity = 0;
216 this.BackView!.Opacity = 1;
217
218 List<Task> tasks =
219 [
220 Task.Run(async () => await this.FrontView.FadeTo(0.5, durationInMs, this.easingType)),
221 Task.Run(async () => await this.BackView.FadeTo(0.5, durationInMs, this.easingType))
222 ];
223
224 await Task.WhenAll(tasks);
225 }
226
227 private async Task FadeBackToFront2()
228 {
229 this.FrontView!.CancelAnimations();
230 this.BackView!.CancelAnimations();
231
232 this.FrontView!.Opacity = 0.5;
233 this.BackView!.Opacity = 0.5;
234
235 List<Task> tasks =
236 [
237 Task.Run(async () => await this.FrontView.FadeTo(1, durationInMs, this.easingType)),
238 Task.Run(async () => await this.BackView.FadeTo(0, durationInMs, this.easingType))
239 ];
240
241 await Task.WhenAll(tasks);
242 }
243
244 #endregion
245 }
246}
The FadeView is a user control that holds two child controls: a FrontView and a BackView....
Definition: FadeView.cs:8
FadeView()
Creates an instance of the FadeView control.
Definition: FadeView.cs:20
View? BackView
The view displayed on the 'back' side of this user control.
Definition: FadeView.cs:127
bool IsFrontView
Set to true is the front vew must be shown.
Definition: FadeView.cs:49
static readonly BindableProperty IsFrontViewProperty
Definition: FadeView.cs:30
View? FrontView
The view displayed on the 'front' side of this user control.
Definition: FadeView.cs:88
static readonly BindableProperty FrontViewProperty
Definition: FadeView.cs:57
static readonly BindableProperty BackViewProperty
Definition: FadeView.cs:96