8
8
9
9
namespace Xamarin . Forms
10
10
{
11
- [ ContentProperty ( " Children" ) ]
11
+ [ ContentProperty ( nameof ( Children ) ) ]
12
12
public abstract class Layout < T > : Layout , IViewContainer < T > where T : View
13
13
{
14
14
readonly ElementCollection < T > _children ;
15
15
16
- protected Layout ( )
17
- {
18
- _children = new ElementCollection < T > ( InternalChildren ) ;
19
- }
16
+ protected Layout ( ) => _children = new ElementCollection < T > ( InternalChildren ) ;
20
17
21
- public new IList < T > Children
22
- {
23
- get { return _children ; }
24
- }
25
-
26
- protected virtual void OnAdded ( T view )
27
- {
28
- }
18
+ public new IList < T > Children => _children ;
29
19
30
20
protected override void OnChildAdded ( Element child )
31
21
{
32
22
base . OnChildAdded ( child ) ;
33
23
34
- var typedChild = child as T ;
35
- if ( typedChild != null )
24
+ if ( child is T typedChild )
36
25
OnAdded ( typedChild ) ;
37
26
}
38
27
39
- protected override void OnChildRemoved ( Element child )
28
+ [ Obsolete ( "OnChildRemoved(Element) is obsolete as of version 4.8.0. Please use OnChildRemoved(Element, int) instead." ) ]
29
+ protected override void OnChildRemoved ( Element child ) => OnChildRemoved ( child , - 1 ) ;
30
+
31
+ protected override void OnChildRemoved ( Element child , int oldLogicalIndex )
40
32
{
41
- base . OnChildRemoved ( child ) ;
33
+ base . OnChildRemoved ( child , oldLogicalIndex ) ;
42
34
43
- var typedChild = child as T ;
44
- if ( typedChild != null )
35
+ if ( child is T typedChild )
45
36
OnRemoved ( typedChild ) ;
46
37
}
47
38
39
+ protected virtual void OnAdded ( T view )
40
+ {
41
+ }
42
+
48
43
protected virtual void OnRemoved ( T view )
49
44
{
50
45
}
51
46
}
52
47
53
48
public abstract class Layout : View , ILayout , ILayoutController , IPaddingElement
54
49
{
55
- public static readonly BindableProperty IsClippedToBoundsProperty = BindableProperty . Create ( "IsClippedToBounds" , typeof ( bool ) , typeof ( Layout ) , false ) ;
50
+ public static readonly BindableProperty IsClippedToBoundsProperty =
51
+ BindableProperty . Create ( nameof ( IsClippedToBounds ) , typeof ( bool ) , typeof ( Layout ) , false ) ;
56
52
57
- public static readonly BindableProperty CascadeInputTransparentProperty = BindableProperty . Create (
58
- nameof ( CascadeInputTransparent ) , typeof ( bool ) , typeof ( Layout ) , true ) ;
53
+ public static readonly BindableProperty CascadeInputTransparentProperty =
54
+ BindableProperty . Create ( nameof ( CascadeInputTransparent ) , typeof ( bool ) , typeof ( Layout ) , true ) ;
59
55
60
56
public static readonly BindableProperty PaddingProperty = PaddingElement . PaddingProperty ;
61
57
@@ -79,51 +75,36 @@ protected Layout()
79
75
80
76
public bool IsClippedToBounds
81
77
{
82
- get { return ( bool ) GetValue ( IsClippedToBoundsProperty ) ; }
83
- set { SetValue ( IsClippedToBoundsProperty , value ) ; }
78
+ get => ( bool ) GetValue ( IsClippedToBoundsProperty ) ;
79
+ set => SetValue ( IsClippedToBoundsProperty , value ) ;
84
80
}
85
81
86
82
public Thickness Padding
87
83
{
88
- get { return ( Thickness ) GetValue ( PaddingElement . PaddingProperty ) ; }
89
- set { SetValue ( PaddingElement . PaddingProperty , value ) ; }
84
+ get => ( Thickness ) GetValue ( PaddingElement . PaddingProperty ) ;
85
+ set => SetValue ( PaddingElement . PaddingProperty , value ) ;
90
86
}
91
87
92
88
public bool CascadeInputTransparent
93
89
{
94
- get { return ( bool ) GetValue ( CascadeInputTransparentProperty ) ; }
95
- set { SetValue ( CascadeInputTransparentProperty , value ) ; }
90
+ get => ( bool ) GetValue ( CascadeInputTransparentProperty ) ;
91
+ set => SetValue ( CascadeInputTransparentProperty , value ) ;
96
92
}
97
93
98
- Thickness IPaddingElement . PaddingDefaultValueCreator ( )
99
- {
100
- return default ( Thickness ) ;
101
- }
94
+ Thickness IPaddingElement . PaddingDefaultValueCreator ( ) => default ( Thickness ) ;
102
95
103
- void IPaddingElement . OnPaddingPropertyChanged ( Thickness oldValue , Thickness newValue )
104
- {
105
- InvalidateLayout ( ) ;
106
- }
96
+ void IPaddingElement . OnPaddingPropertyChanged ( Thickness oldValue , Thickness newValue ) => InvalidateLayout ( ) ;
107
97
108
98
internal ObservableCollection < Element > InternalChildren { get ; } = new ObservableCollection < Element > ( ) ;
109
99
110
- internal override ReadOnlyCollection < Element > LogicalChildrenInternal
111
- {
112
- get { return _logicalChildren ?? ( _logicalChildren = new ReadOnlyCollection < Element > ( InternalChildren ) ) ; }
113
- }
100
+ internal override ReadOnlyCollection < Element > LogicalChildrenInternal => _logicalChildren ?? ( _logicalChildren = new ReadOnlyCollection < Element > ( InternalChildren ) ) ;
114
101
115
102
public event EventHandler LayoutChanged ;
116
103
117
104
[ EditorBrowsable ( EditorBrowsableState . Never ) ]
118
- public IReadOnlyList < Element > Children
119
- {
120
- get { return InternalChildren ; }
121
- }
105
+ public IReadOnlyList < Element > Children => InternalChildren ;
122
106
123
- public void ForceLayout ( )
124
- {
125
- SizeAllocated ( Width , Height ) ;
126
- }
107
+ public void ForceLayout ( ) => SizeAllocated ( Width , Height ) ;
127
108
128
109
[ Obsolete ( "OnSizeRequest is obsolete as of version 2.2.0. Please use OnMeasure instead." ) ]
129
110
[ EditorBrowsable ( EditorBrowsableState . Never ) ]
@@ -136,13 +117,11 @@ public sealed override SizeRequest GetSizeRequest(double widthConstraint, double
136
117
137
118
public static void LayoutChildIntoBoundingRegion ( VisualElement child , Rectangle region )
138
119
{
139
- var parent = child . Parent as IFlowDirectionController ;
140
120
bool isRightToLeft = false ;
141
- if ( parent != null && ( isRightToLeft = parent . ApplyEffectiveFlowDirectionToChildContainer && parent . EffectiveFlowDirection . IsRightToLeft ( ) ) )
121
+ if ( child . Parent is IFlowDirectionController parent && ( isRightToLeft = parent . ApplyEffectiveFlowDirectionToChildContainer && parent . EffectiveFlowDirection . IsRightToLeft ( ) ) )
142
122
region = new Rectangle ( parent . Width - region . Right , region . Y , region . Width , region . Height ) ;
143
123
144
- var view = child as View ;
145
- if ( view == null )
124
+ if ( ! ( child is View view ) )
146
125
{
147
126
child . Layout ( region ) ;
148
127
return ;
@@ -224,15 +203,9 @@ protected override void OnSizeAllocated(double width, double height)
224
203
UpdateChildrenLayout ( ) ;
225
204
}
226
205
227
- protected virtual bool ShouldInvalidateOnChildAdded ( View child )
228
- {
229
- return true ;
230
- }
206
+ protected virtual bool ShouldInvalidateOnChildAdded ( View child ) => true ;
231
207
232
- protected virtual bool ShouldInvalidateOnChildRemoved ( View child )
233
- {
234
- return true ;
235
- }
208
+ protected virtual bool ShouldInvalidateOnChildRemoved ( View child ) => true ;
236
209
237
210
protected void UpdateChildrenLayout ( )
238
211
{
@@ -279,9 +252,8 @@ protected void UpdateChildrenLayout()
279
252
280
253
internal static void LayoutChildIntoBoundingRegion ( View child , Rectangle region , SizeRequest childSizeRequest )
281
254
{
282
- var parent = child . Parent as IFlowDirectionController ;
283
255
bool isRightToLeft = false ;
284
- if ( parent != null && ( isRightToLeft = parent . ApplyEffectiveFlowDirectionToChildContainer && parent . EffectiveFlowDirection . IsRightToLeft ( ) ) )
256
+ if ( child . Parent is IFlowDirectionController parent && ( isRightToLeft = parent . ApplyEffectiveFlowDirectionToChildContainer && parent . EffectiveFlowDirection . IsRightToLeft ( ) ) )
285
257
region = new Rectangle ( parent . Width - region . Right , region . Y , region . Width , region . Height ) ;
286
258
287
259
if ( region . Size != childSizeRequest . Request )
@@ -325,13 +297,11 @@ internal virtual void OnChildMeasureInvalidated(VisualElement child, Invalidatio
325
297
int count = children . Count ;
326
298
for ( var index = 0 ; index < count ; index ++ )
327
299
{
328
- var v = LogicalChildrenInternal [ index ] as VisualElement ;
329
- if ( v != null && v . IsVisible && ( ! v . IsPlatformEnabled || ! v . IsNativeStateConsistent ) )
300
+ if ( LogicalChildrenInternal [ index ] is VisualElement v && v . IsVisible && ( ! v . IsPlatformEnabled || ! v . IsNativeStateConsistent ) )
330
301
return ;
331
302
}
332
303
333
- var view = child as View ;
334
- if ( view != null )
304
+ if ( child is View view )
335
305
{
336
306
// we can ignore the request if we are either fully constrained or when the size request changes and we were already fully constrainted
337
307
if ( ( trigger == InvalidationTrigger . MeasureChanged && view . Constraint == LayoutConstraint . Fixed ) ||
@@ -433,7 +403,7 @@ void InternalChildrenOnCollectionChanged(object sender, NotifyCollectionChangedE
433
403
if ( v == null )
434
404
continue ;
435
405
436
- OnInternalRemoved ( v ) ;
406
+ OnInternalRemoved ( v , e . OldStartingIndex + i ) ;
437
407
}
438
408
}
439
409
@@ -466,11 +436,11 @@ void OnInternalAdded(View view)
466
436
view . MeasureInvalidated += OnChildMeasureInvalidated ;
467
437
}
468
438
469
- void OnInternalRemoved ( View view )
439
+ void OnInternalRemoved ( View view , int oldIndex )
470
440
{
471
441
view . MeasureInvalidated -= OnChildMeasureInvalidated ;
472
442
473
- OnChildRemoved ( view ) ;
443
+ OnChildRemoved ( view , oldIndex ) ;
474
444
if ( ShouldInvalidateOnChildRemoved ( view ) )
475
445
InvalidateLayout ( ) ;
476
446
}
0 commit comments