@@ -12,20 +12,21 @@ namespace CommunityToolkit.Maui.Sample.Pages.Alerts;
1212
1313public partial class SnackbarPage : BasePage < SnackbarViewModel >
1414{
15- const string displayCustomSnackbarText = "Display a Custom Snackbar, Anchored to this Button " ;
15+ public const string DisplayCustomSnackbarText = "Display Custom Snackbar" ;
1616 const string dismissCustomSnackbarText = "Dismiss Custom Snackbar" ;
17- readonly IReadOnlyList < Color > colors = typeof ( Colors )
17+
18+ readonly IReadOnlyList < Color > colors = [ .. typeof ( Colors )
1819 . GetFields ( BindingFlags . Static | BindingFlags . Public )
1920 . ToDictionary ( c => c . Name , c => ( Color ) ( c . GetValue ( null ) ?? throw new InvalidOperationException ( ) ) )
20- . Values . ToList ( ) ;
21+ . Values ] ;
2122
2223 ISnackbar ? customSnackbar ;
2324
2425 public SnackbarPage ( SnackbarViewModel snackbarViewModel ) : base ( snackbarViewModel )
2526 {
2627 InitializeComponent ( ) ;
2728
28- DisplayCustomSnackbarButton . Text = displayCustomSnackbarText ;
29+ DisplayCustomSnackbarButtonAnchoredToButton . Text = DisplayCustomSnackbarText ;
2930
3031 Snackbar . Shown += Snackbar_Shown ;
3132 Snackbar . Dismissed += Snackbar_Dismissed ;
@@ -34,9 +35,9 @@ public SnackbarPage(SnackbarViewModel snackbarViewModel) : base(snackbarViewMode
3435 async void DisplayDefaultSnackbarButtonClicked ( object ? sender , EventArgs args ) =>
3536 await this . DisplaySnackbar ( "This is a Snackbar.\n It will disappear in 3 seconds.\n Or click OK to dismiss immediately" ) ;
3637
37- async void DisplayCustomSnackbarButtonClicked ( object ? sender , EventArgs args )
38+ async void DisplayCustomSnackbarAnchoredToButtonClicked ( object ? sender , EventArgs args )
3839 {
39- if ( DisplayCustomSnackbarButton . Text is displayCustomSnackbarText )
40+ if ( DisplayCustomSnackbarButtonAnchoredToButton . Text is DisplayCustomSnackbarText )
4041 {
4142 var options = new SnackbarOptions
4243 {
@@ -52,20 +53,20 @@ async void DisplayCustomSnackbarButtonClicked(object? sender, EventArgs args)
5253 "This is a customized Snackbar" ,
5354 async ( ) =>
5455 {
55- await DisplayCustomSnackbarButton . BackgroundColorTo ( colors [ Random . Shared . Next ( colors . Count ) ] , length : 500 ) ;
56- DisplayCustomSnackbarButton . Text = displayCustomSnackbarText ;
56+ await DisplayCustomSnackbarButtonAnchoredToButton . BackgroundColorTo ( colors [ Random . Shared . Next ( colors . Count ) ] , length : 500 ) ;
57+ DisplayCustomSnackbarButtonAnchoredToButton . Text = DisplayCustomSnackbarText ;
5758 } ,
5859 FontAwesomeIcons . Microsoft ,
5960 TimeSpan . FromSeconds ( 30 ) ,
6061 options ,
61- DisplayCustomSnackbarButton ) ;
62+ DisplayCustomSnackbarButtonAnchoredToButton ) ;
6263
6364 var cts = new CancellationTokenSource ( TimeSpan . FromSeconds ( 5 ) ) ;
6465 await customSnackbar . Show ( cts . Token ) ;
6566
66- DisplayCustomSnackbarButton . Text = dismissCustomSnackbarText ;
67+ DisplayCustomSnackbarButtonAnchoredToButton . Text = dismissCustomSnackbarText ;
6768 }
68- else if ( DisplayCustomSnackbarButton . Text is dismissCustomSnackbarText )
69+ else if ( DisplayCustomSnackbarButtonAnchoredToButton . Text is dismissCustomSnackbarText )
6970 {
7071 if ( customSnackbar is not null )
7172 {
@@ -75,11 +76,11 @@ async void DisplayCustomSnackbarButtonClicked(object? sender, EventArgs args)
7576 customSnackbar . Dispose ( ) ;
7677 }
7778
78- DisplayCustomSnackbarButton . Text = displayCustomSnackbarText ;
79+ DisplayCustomSnackbarButtonAnchoredToButton . Text = DisplayCustomSnackbarText ;
7980 }
8081 else
8182 {
82- throw new NotSupportedException ( $ "{ nameof ( DisplayCustomSnackbarButton ) } .{ nameof ( ITextButton . Text ) } Not Recognized") ;
83+ throw new NotSupportedException ( $ "{ nameof ( DisplayCustomSnackbarButtonAnchoredToButton ) } .{ nameof ( ITextButton . Text ) } Not Recognized") ;
8384 }
8485 }
8586
@@ -97,6 +98,20 @@ async void DisplaySnackbarInModalButtonClicked(object? sender, EventArgs e)
9798 {
9899 if ( Application . Current ? . Windows [ 0 ] . Page is Page mainPage )
99100 {
101+ var button = new Button ( )
102+ . CenterHorizontal ( )
103+ . Text ( "Display Snackbar" ) ;
104+ button . Command = new AsyncRelayCommand ( token => button . DisplaySnackbar (
105+ "This Snackbar is anchored to the button on the bottom to avoid clipping the Snackbar on the top of the Page." ,
106+ ( ) => { } ,
107+ "Close" ,
108+ TimeSpan . FromSeconds ( 5 ) , token : token ) ) ;
109+
110+ var backButton = new Button ( )
111+ . CenterHorizontal ( )
112+ . Text ( "Back to Snackbar MainPage" ) ;
113+ backButton . Command = new AsyncRelayCommand ( mainPage . Navigation . PopModalAsync ) ;
114+
100115 await mainPage . Navigation . PushModalAsync ( new ContentPage
101116 {
102117 Content = new VerticalStackLayout
@@ -105,19 +120,11 @@ await mainPage.Navigation.PushModalAsync(new ContentPage
105120
106121 Children =
107122 {
108- new Button { Command = new AsyncRelayCommand ( static token => Snackbar . Make ( "Snackbar in a Modal MainPage" ) . Show ( token ) ) }
109- . Top ( ) . CenterHorizontal ( )
110- . Text ( "Display Snackbar" ) ,
123+ button ,
111124
112- new Label ( )
113- . Center ( ) . TextCenter ( )
114- . Text ( "This is a Modal MainPage" ) ,
115-
116- new Button { Command = new AsyncRelayCommand ( mainPage . Navigation . PopModalAsync ) }
117- . Bottom ( ) . CenterHorizontal ( )
118- . Text ( "Back to Snackbar MainPage" )
125+ backButton
119126 }
120- } . Center ( )
127+ }
121128 } . Padding ( 12 ) ) ;
122129 }
123130 }
0 commit comments