1
1
using VijayAnand . MauiToolkit . Core ;
2
+ using VijayAnand . MauiToolkit . Services ;
2
3
3
4
namespace PopupDialogs
4
5
{
5
6
public partial class MainPage : ContentPage
6
7
{
7
- readonly IDialogService _dialogService ;
8
+ readonly IDialogService _genericDialog ;
9
+ readonly IMauiDialogService _mauiDialog ;
8
10
9
- public MainPage ( IDialogService dialogService )
11
+ public MainPage ( IDialogService genericDialog , IMauiDialogService mauiDialog )
10
12
{
11
13
InitializeComponent ( ) ;
12
- _dialogService = dialogService ;
14
+ _genericDialog = genericDialog ;
15
+ _mauiDialog = mauiDialog ;
13
16
}
14
17
15
- private async void OnClicked ( object sender , EventArgs e )
18
+ private async void OnBtnClicked ( object sender , EventArgs e )
16
19
{
17
20
if ( sender is Button btn )
18
21
{
@@ -24,24 +27,24 @@ private async void OnClicked(object sender, EventArgs e)
24
27
switch ( btn . AutomationId )
25
28
{
26
29
case "Alert1" :
27
- await _dialogService . DisplayAlertAsync ( "Greeting" , "Hello from .NET MAUI!!!" , "OK" ) ;
30
+ await _genericDialog . DisplayAlertAsync ( "Greeting" , "Hello from .NET MAUI!!!" , "OK" ) ;
28
31
break ;
29
32
case "Alert2" :
30
- var response = await _dialogService . DisplayAlertAsync ( "Confirm" , "Would you like to proceed?" , "Yes" , "No" ) ;
33
+ var response = await _genericDialog . DisplayAlertAsync ( "Confirm" , "Would you like to proceed?" , "Yes" , "No" ) ;
31
34
var message = response is true ? "Glad that you opted in." : "Sorry to miss you, hope to see you soon." ;
32
- await _dialogService . DisplayAlertAsync ( "Response" , message , "OK" ) ;
35
+ await _genericDialog . DisplayAlertAsync ( "Response" , message , "OK" ) ;
33
36
break ;
34
37
case "Prompt" :
35
- var result = await _dialogService . DisplayPromptAsync ( "Subscribe" , "Enter your email to send notifications." , maxLength : 100 , inputType : InputType . Email ) ;
36
-
38
+ var result = await _genericDialog . DisplayPromptAsync ( "Subscribe" , "Enter your email to send notifications." , maxLength : 100 , inputType : InputType . Email ) ;
39
+
37
40
if ( ! string . IsNullOrWhiteSpace ( result ) )
38
41
{
39
- await _dialogService . DisplayAlertAsync ( "Response" , result , "OK" ) ;
42
+ await _genericDialog . DisplayAlertAsync ( "Response" , result , "OK" ) ;
40
43
}
41
44
break ;
42
45
case "ActionSheet" :
43
- var action = await _dialogService . DisplayActionSheetAsync ( "Unsaved Changes ..." , "What would you like to do?" , "Cancel" , "Discard" , "Save" , "Save" , "Upload" , "Share" , "Discard ") ;
44
- await _dialogService . DisplayAlertAsync ( "Response" , action , "OK" ) ;
46
+ var action = await _genericDialog . DisplayActionSheetAsync ( "Unsaved Changes ..." , "What would you like to do?" , "Cancel" , "Discard" , "Save" , "Save" , "Upload" , "Share" ) ;
47
+ await _genericDialog . DisplayAlertAsync ( "Response" , action , "OK" ) ;
45
48
break ;
46
49
default :
47
50
// Static approach for custom implementation
@@ -51,5 +54,47 @@ private async void OnClicked(object sender, EventArgs e)
51
54
}
52
55
}
53
56
}
57
+
58
+ private async void OnMauiBtnClicked ( object sender , EventArgs e )
59
+ {
60
+ if ( sender is Button btn )
61
+ {
62
+ // DI approach
63
+ // Switch betwen native/custom implementations in the MauiProgram.cs
64
+ // The order in which dependencies are registered determines the instance injected
65
+ // The latest one wins
66
+
67
+ var rightToLeft = FlowDirection . RightToLeft ;
68
+
69
+ switch ( btn . AutomationId )
70
+ {
71
+ case "Alert1" :
72
+ await _mauiDialog . DisplayAlertAsync ( "Greeting" , "Hello from .NET MAUI!!!" , "OK" , rightToLeft ) ;
73
+ break ;
74
+ case "Alert2" :
75
+ var response = await _mauiDialog . DisplayAlertAsync ( "Confirm" , "Would you like to proceed?" , "Yes" , "No" , rightToLeft ) ;
76
+ var message = response is true ? "Glad that you opted in." : "Sorry to miss you, hope to see you soon." ;
77
+ await _mauiDialog . DisplayAlertAsync ( "Response" , message , "OK" , rightToLeft ) ;
78
+ break ;
79
+ case "Prompt" :
80
+ var result = await _mauiDialog . DisplayPromptAsync ( "Subscribe" , "Enter your email to send notifications." , rightToLeft , maxLength : 100 , keyboard : Keyboard . Email ) ;
81
+
82
+ if ( ! string . IsNullOrWhiteSpace ( result ) )
83
+ {
84
+ await _mauiDialog . DisplayAlertAsync ( "Response" , result , "OK" , rightToLeft ) ;
85
+ }
86
+ break ;
87
+ case "ActionSheet" :
88
+ var action = await _mauiDialog . DisplayActionSheetAsync ( "Unsaved Changes ..." /*, "What would you like to do?"*/ , "Cancel" , "Discard" , rightToLeft , "Save" , "Upload" , "Share" ) ;
89
+ await _mauiDialog . DisplayAlertAsync ( "Response" , action , "OK" , rightToLeft ) ;
90
+ break ;
91
+ default :
92
+ // Static approach for custom implementation
93
+ // Static approach for native dialogs soon in an upcoming preview
94
+ await MauiPopupDialog . Instance . DisplayAlertAsync ( "Static Invocation" , "Welcome to .NET MAUI!!!" , "OK" , rightToLeft ) ;
95
+ break ;
96
+ }
97
+ }
98
+ }
54
99
}
55
100
}
0 commit comments