This repository has been archived by the owner on Sep 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 309
/
Copy pathDialogService.cs
208 lines (189 loc) · 8.78 KB
/
DialogService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// ****************************************************************************
// <copyright file="DialogService.cs" company="GalaSoft Laurent Bugnion">
// Copyright © GalaSoft Laurent Bugnion 2009-2016
// </copyright>
// ****************************************************************************
// <author>Laurent Bugnion</author>
// <email>laurent@galasoft.ch</email>
// <date>02.10.2014</date>
// <project>GalaSoft.MvvmLight</project>
// <web>http://www.mvvmlight.net</web>
// <license>
// See license.txt in this solution or http://www.galasoft.ch/license_MIT.txt
// </license>
// ****************************************************************************
using System;
using System.Threading.Tasks;
using Windows.UI.Popups;
namespace GalaSoft.MvvmLight.Views
{
/// <summary>
/// An implementation of <see cref="IDialogService"/> allowing
/// to display simple dialogs to the user. Note that this class
/// uses the built in Android dialogs which may or may not
/// be sufficient for your needs. Using this class is easy
/// but feel free to develop your own IDialogService implementation
/// if needed.
/// </summary>
////[ClassInfo(typeof(IDialogService))]
public class DialogService : IDialogService
{
/// <summary>
/// Displays information about an error.
/// </summary>
/// <param name="message">The message to be shown to the user.</param>
/// <param name="title">The title of the dialog box. This may be null.</param>
/// <param name="buttonText">The text shown in the only button
/// in the dialog box. If left null, the text "OK" will be used.</param>
/// <param name="afterHideCallback">A callback that should be executed after
/// the dialog box is closed by the user.</param>
/// <returns>A Task allowing this async method to be awaited.</returns>
public async Task ShowError(string message, string title, string buttonText, Action afterHideCallback)
{
var dialog = CreateDialog(message, title, buttonText, null, afterHideCallback);
await dialog.ShowAsync();
}
/// <summary>
/// Displays information about an error.
/// </summary>
/// <param name="error">The exception of which the message must be shown to the user.</param>
/// <param name="title">The title of the dialog box. This may be null.</param>
/// <param name="buttonText">The text shown in the only button
/// in the dialog box. If left null, the text "OK" will be used.</param>
/// <param name="afterHideCallback">A callback that should be executed after
/// the dialog box is closed by the user.</param>
/// <returns>A Task allowing this async method to be awaited.</returns>
public async Task ShowError(Exception error, string title, string buttonText, Action afterHideCallback)
{
var dialog = CreateDialog(error.Message, title, buttonText, null, afterHideCallback);
await dialog.ShowAsync();
}
/// <summary>
/// Displays information to the user. The dialog box will have only
/// one button with the text "OK".
/// </summary>
/// <param name="message">The message to be shown to the user.</param>
/// <param name="title">The title of the dialog box. This may be null.</param>
/// <returns>A Task allowing this async method to be awaited.</returns>
public async Task ShowMessage(string message, string title)
{
var dialog = CreateDialog(message, title);
await dialog.ShowAsync();
}
/// <summary>
/// Displays information to the user. The dialog box will have only
/// one button.
/// </summary>
/// <param name="message">The message to be shown to the user.</param>
/// <param name="title">The title of the dialog box. This may be null.</param>
/// <param name="buttonText">The text shown in the only button
/// in the dialog box. If left null, the text "OK" will be used.</param>
/// <param name="afterHideCallback">A callback that should be executed after
/// the dialog box is closed by the user.</param>
/// <returns>A Task allowing this async method to be awaited.</returns>
public async Task ShowMessage(string message, string title, string buttonText, Action afterHideCallback)
{
var dialog = CreateDialog(message, title, buttonText, null, afterHideCallback);
await dialog.ShowAsync();
}
/// <summary>
/// Displays information to the user. The dialog box will have only
/// one button.
/// </summary>
/// <param name="message">The message to be shown to the user.</param>
/// <param name="title">The title of the dialog box. This may be null.</param>
/// <param name="buttonConfirmText">The text shown in the "confirm" button
/// in the dialog box. If left null, the text "OK" will be used.</param>
/// <param name="buttonCancelText">The text shown in the "cancel" button
/// in the dialog box. If left null, the text "Cancel" will be used.</param>
/// <param name="afterHideCallback">A callback that should be executed after
/// the dialog box is closed by the user. The callback method will get a boolean
/// parameter indicating if the "confirm" button (true) or the "cancel" button
/// (false) was pressed by the user.</param>
/// <returns>A Task allowing this async method to be awaited.</returns>
public async Task<bool> ShowMessage(
string message,
string title,
string buttonConfirmText,
string buttonCancelText,
Action<bool> afterHideCallback)
{
var result = false;
var dialog = CreateDialog(
message,
title,
buttonConfirmText,
buttonCancelText,
null,
afterHideCallback,
r => result = r);
await dialog.ShowAsync();
return result;
}
/// <summary>
/// Displays information to the user in a simple dialog box. The dialog box will have only
/// one button with the text "OK". This method should be used for debugging purposes.
/// </summary>
/// <param name="message">The message to be shown to the user.</param>
/// <param name="title">The title of the dialog box. This may be null.</param>
/// <returns>A Task allowing this async method to be awaited.</returns>
public async Task ShowMessageBox(string message, string title)
{
var dialog = CreateDialog(message, title);
await dialog.ShowAsync();
}
private MessageDialog CreateDialog(
string message,
string title,
string buttonConfirmText = "OK",
string buttonCancelText = null,
Action afterHideCallback = null,
Action<bool> afterHideCallbackWithResponse = null,
Action<bool> afterHideInternal = null)
{
var dialog = new MessageDialog(message, title);
dialog.Commands.Add(
new UICommand(
buttonConfirmText,
o =>
{
if (afterHideCallback != null)
{
afterHideCallback();
}
if (afterHideCallbackWithResponse != null)
{
afterHideCallbackWithResponse(true);
}
if (afterHideInternal != null)
{
afterHideInternal(true);
}
}));
dialog.DefaultCommandIndex = 0;
if (!string.IsNullOrEmpty(buttonCancelText))
{
dialog.Commands.Add(
new UICommand(
buttonCancelText,
o =>
{
if (afterHideCallback != null)
{
afterHideCallback();
}
if (afterHideCallbackWithResponse != null)
{
afterHideCallbackWithResponse(false);
}
if (afterHideInternal != null)
{
afterHideInternal(false);
}
}));
dialog.CancelCommandIndex = 1;
}
return dialog;
}
}
}