1+ using System ;
2+ using System . Threading . Tasks ;
3+ using Microsoft . Maui . Controls ;
4+
5+ namespace Maui . Controls . Sample . Pages ;
6+
7+ public partial class ToolbarPage
8+ {
9+ ToolbarItem ? _cachedSecondary3 ;
10+
11+ public Command ClickedCommand { get ; }
12+
13+ public ToolbarPage ( )
14+ {
15+ InitializeComponent ( ) ;
16+
17+ ClickedCommand = new Command ( ( ) =>
18+ {
19+ menuLabel . Text = "You clicked on ToolbarItem: Test Secondary (3)" ;
20+ } ) ;
21+
22+ BindingContext = this ;
23+ }
24+
25+ void ItemClicked ( object sender , EventArgs e )
26+ {
27+ if ( sender is ToolbarItem tbi )
28+ {
29+ menuLabel . Text = $ "You clicked on ToolbarItem: { tbi . Text } ";
30+ }
31+ }
32+
33+ void Button_Clicked ( object sender , EventArgs e )
34+ {
35+ secondary4 . IsEnabled = ! secondary4 . IsEnabled ;
36+ }
37+
38+ void Button_Clicked1 ( object sender , EventArgs e )
39+ {
40+ primary1 . IsEnabled = ! primary1 . IsEnabled ;
41+ }
42+
43+ void Button_Clicked2 ( object sender , EventArgs e )
44+ {
45+ // 5 second delay so you can have the menu open and see the change.
46+ // However, the menu will close if change happens. There is no way around this.
47+ Task . Delay ( 5000 ) . ContinueWith ( t =>
48+ {
49+ Dispatcher . Dispatch ( ( ) =>
50+ {
51+ secondary2 . IsEnabled = ! secondary2 . IsEnabled ;
52+ } ) ;
53+ } ) ;
54+ }
55+
56+ void Button_Clicked3 ( object sender , EventArgs e )
57+ {
58+ secondary1 . Text = secondary1 . Text == "Test Secondary (1)" ? "Changed Text" : "Test Secondary (1)" ;
59+ }
60+
61+ void Button_Clicked4 ( object sender , EventArgs e )
62+ {
63+ _cachedSecondary3 ??= secondary3 ;
64+
65+ if ( ToolbarItems . Contains ( _cachedSecondary3 ) )
66+ {
67+ ToolbarItems . Remove ( _cachedSecondary3 ) ;
68+ }
69+ else
70+ {
71+ ToolbarItems . Add ( _cachedSecondary3 ) ;
72+ }
73+ }
74+
75+ void Button_Clicked5 ( object sender , EventArgs e )
76+ {
77+ secondary3 . Command = new Command ( ( ) =>
78+ {
79+ menuLabel . Text = "You clicked on ToolbarItem: Test Secondary (3) with changed Command" ;
80+ } ) ;
81+ }
82+ }
0 commit comments