This repository has been archived by the owner on Nov 21, 2017. It is now read-only.
/
MessageBoxService.cs
92 lines (84 loc) · 4.08 KB
/
MessageBoxService.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
// ****************************************************************************
// <copyright file="MessageBoxService.cs" company="Pedro Lamas">
// Copyright © Pedro Lamas 2011
// </copyright>
// ****************************************************************************
// <author>Pedro Lamas</author>
// <email>pedrolamas@gmail.com</email>
// <date>17-11-2011</date>
// <project>Cimbalino.Phone.Toolkit</project>
// <web>http://www.pedrolamas.com</web>
// <license>
// See license.txt in this solution or http://www.pedrolamas.com/license_MIT.txt
// </license>
// ****************************************************************************
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows;
using Microsoft.Xna.Framework.GamerServices;
namespace Cimbalino.Phone.Toolkit.Services
{
/// <summary>
/// Represents an implementation of the <see cref="IMessageBoxService"/>.
/// </summary>
public class MessageBoxService : IMessageBoxService
{
/// <summary>
/// Displays a message box that contains the specified text and an OK button.
/// </summary>
/// <param name="text">The message to display.</param>
public void Show(string text)
{
MessageBox.Show(text);
}
/// <summary>
/// Displays a message box that contains the specified text, title bar caption, and an OK button.
/// </summary>
/// <param name="text">The message to display.</param>
/// <param name="caption">The title of the message box.</param>
public void Show(string text, string caption)
{
Show(text, caption, MessageBoxButton.OK);
}
/// <summary>
/// Displays a message box that contains the specified text, title bar caption, and response buttons.
/// </summary>
/// <param name="text">The message to display.</param>
/// <param name="caption">The title of the message box.</param>
/// <param name="button">A value that indicates the button or buttons to display.</param>
/// <returns>A value that indicates the user's response to the message.</returns>
public MessageBoxResult Show(string text, string caption, MessageBoxButton button)
{
return MessageBox.Show(text, caption, button);
}
/// <summary>
/// Displays a message box that contains the specified text, title bar caption, and response buttons.
/// </summary>
/// <param name="text">The message to display.</param>
/// <param name="caption">The title of the message box.</param>
/// <param name="buttons">The captions for message box buttons. The maximum number of buttons is two.</param>
/// <param name="selectedButton">The <see cref="Action{Int32}" /> to be called once the operation is finished.</param>
public void Show(string text, string caption, IEnumerable<string> buttons, Action<int> selectedButton)
{
Guide.BeginShowMessageBox(caption, text, buttons, 0, MessageBoxIcon.None, ar =>
{
var buttonIndex = Guide.EndShowMessageBox(ar);
Deployment.Current.Dispatcher.BeginInvoke(() => selectedButton(buttonIndex.HasValue ? buttonIndex.Value : -1));
}, null);
}
/// <summary>
/// Displays a message box that contains the specified text, title bar caption, and response buttons.
/// </summary>
/// <param name="text">The message to display.</param>
/// <param name="caption">The title of the message box.</param>
/// <param name="buttons">The captions for message box buttons. The maximum number of buttons is two.</param>
/// <returns>The <see cref="Task"/> object representing the asynchronous operation.</returns>
public Task<int> ShowAsync(string text, string caption, IEnumerable<string> buttons)
{
var taskCompletionSource = new TaskCompletionSource<int>();
Show(text, caption, buttons, taskCompletionSource.SetResult);
return taskCompletionSource.Task;
}
}
}