33// Copyright (c) MADE Apps.
44// </copyright>
55// <summary>
6- // Defines a collection of extensions for application views.
6+ // Defines a collection of extensions for Xamarin.Forms views.
77// </summary>
88// --------------------------------------------------------------------------------------------------------------------
99
1010namespace MADE . App . Views . Extensions
1111{
12+ using System ;
13+
14+ using Xamarin . Forms ;
15+
1216 /// <summary>
13- /// Defines a collection of extensions for application views.
17+ /// Defines a collection of extensions for Xamarin.Forms views.
1418 /// </summary>
1519 public static class ViewExtensions
1620 {
17- #if __ANDROID__
1821 /// <summary>
1922 /// Sets the visibility of the given control by the given visible boolean value.
2023 /// </summary>
@@ -24,72 +27,36 @@ public static class ViewExtensions
2427 /// <param name="isVisible">
2528 /// A value indicating whether the control is visible.
2629 /// </param>
27- public static void SetVisible ( this Android . Views . View view , bool isVisible )
30+ public static void SetVisible ( this View view , bool isVisible )
2831 {
29- SetVisible ( view , isVisible , false ) ;
30- }
31-
32- /// <summary>
33- /// Sets the visibility of the given control by the given visible boolean value.
34- /// If the control can contain child control, these will also be updated if the given parameter is set to true.
35- /// </summary>
36- /// <param name="view">
37- /// The control to update visibility.
38- /// </param>
39- /// <param name="isVisible">
40- /// A value indicating whether the control is visible.
41- /// </param>
42- /// <param name="updateChildViews">
43- /// A value indicating whether to update the control's child visibility states.
44- /// </param>
45- public static void SetVisible ( this Android . Views . View view , bool isVisible , bool updateChildViews )
46- {
47- if ( view == null )
48- {
49- return ;
50- }
51-
52- view . Visibility = isVisible ? Android . Views . ViewStates . Visible : Android . Views . ViewStates . Gone ;
53-
54- if ( ! updateChildViews || ! ( view is Android . Views . ViewGroup viewGroup ) )
55- {
56- return ;
57- }
58-
59- for ( int i = 0 ; i < viewGroup . ChildCount ; i ++ )
60- {
61- try
62- {
63- Android . Views . View child = viewGroup . GetChildAt ( i ) ;
64- child ? . SetVisible ( isVisible , true ) ;
65- }
66- catch ( System . Exception )
67- {
68- // Ignored
69- }
70- }
32+ SetVisible < View > ( view , isVisible , false ) ;
7133 }
72- #endif
7334
74- #if WINDOWS_UWP
7535 /// <summary>
7636 /// Sets the visibility of the given control by the given visible boolean value.
7737 /// </summary>
38+ /// <typeparam name="T">
39+ /// The type of views that are potentially children of the given view.
40+ /// </typeparam>
7841 /// <param name="view">
7942 /// The control to update visibility.
8043 /// </param>
8144 /// <param name="isVisible">
8245 /// A value indicating whether the control is visible.
8346 /// </param>
84- public static void SetVisible ( this Windows . UI . Xaml . UIElement view , bool isVisible )
47+ public static void SetVisible < T > ( this View view , bool isVisible )
48+ where T : View
8549 {
86- SetVisible ( view , isVisible , false ) ;
50+ SetVisible < T > ( view , isVisible , false ) ;
8751 }
8852
8953 /// <summary>
9054 /// Sets the visibility of the given control by the given visible boolean value.
9155 /// If the control can contain child control, these will also be updated if the given parameter is set to true.
9256 /// </summary>
57+ /// <typeparam name="T">
58+ /// The type of views that are potentially children of the given view.
59+ /// </typeparam>
9360 /// <param name="view">
9461 /// The control to update visibility.
9562 /// </param>
@@ -99,25 +66,32 @@ public static void SetVisible(this Windows.UI.Xaml.UIElement view, bool isVisibl
9966 /// <param name="updateChildViews">
10067 /// A value indicating whether to update the control's child visibility states.
10168 /// </param>
102- public static void SetVisible ( this Windows . UI . Xaml . UIElement view , bool isVisible , bool updateChildViews )
69+ public static void SetVisible < T > ( this View view , bool isVisible , bool updateChildViews )
70+ where T : View
10371 {
10472 if ( view == null )
10573 {
10674 return ;
10775 }
10876
109- view . Visibility = isVisible ? Windows . UI . Xaml . Visibility . Visible : Windows . UI . Xaml . Visibility . Collapsed ;
77+ view . IsVisible = isVisible ;
11078
111- if ( ! updateChildViews || ! ( view is Windows . UI . Xaml . Controls . Panel viewGroup ) )
79+ if ( ! updateChildViews || ! ( view is Layout < T > panel ) )
11280 {
11381 return ;
11482 }
11583
116- foreach ( Windows . UI . Xaml . UIElement child in viewGroup . Children )
84+ foreach ( T child in panel . Children )
11785 {
118- child ? . SetVisible ( isVisible , true ) ;
86+ try
87+ {
88+ child ? . SetVisible < T > ( isVisible , true ) ;
89+ }
90+ catch ( Exception )
91+ {
92+ // Ignored
93+ }
11994 }
12095 }
121- #endif
12296 }
12397}
0 commit comments