/
DialogService.cs
191 lines (167 loc) · 8.15 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
using Prism.Common;
using Prism.Ioc;
using System;
using System.ComponentModel;
using System.Linq;
using System.Windows;
namespace Prism.Services.Dialogs
{
/// <summary>
/// Implements <see cref="IDialogService"/> to show modal and non-modal dialogs.
/// </summary>
/// <remarks>
/// The dialog's ViewModel must implement IDialogAware.
/// </remarks>
public class DialogService : IDialogService
{
private readonly IContainerExtension _containerExtension;
/// <summary>
/// Initializes a new instance of the <see cref="DialogService"/> class.
/// </summary>
/// <param name="containerExtension"></param>
public DialogService(IContainerExtension containerExtension)
{
_containerExtension = containerExtension;
}
/// <summary>
/// Shows a non-modal dialog.
/// </summary>
/// <param name="name">The name of the dialog to show.</param>
/// <param name="parameters">The parameters to pass to the dialog.</param>
/// <param name="callback">The action to perform when the dialog is closed.</param>
public void Show(string name, IDialogParameters parameters, Action<IDialogResult> callback)
{
ShowDialogInternal(name, parameters, callback, false);
}
/// <summary>
/// Shows a non-modal dialog.
/// </summary>
/// <param name="name">The name of the dialog to show.</param>
/// <param name="parameters">The parameters to pass to the dialog.</param>
/// <param name="callback">The action to perform when the dialog is closed.</param>
/// <param name="windowName">The name of the hosting window registered with the IContainerRegistry.</param>
public void Show(string name, IDialogParameters parameters, Action<IDialogResult> callback, string windowName)
{
ShowDialogInternal(name, parameters, callback, false, windowName);
}
/// <summary>
/// Shows a modal dialog.
/// </summary>
/// <param name="name">The name of the dialog to show.</param>
/// <param name="parameters">The parameters to pass to the dialog.</param>
/// <param name="callback">The action to perform when the dialog is closed.</param>
public void ShowDialog(string name, IDialogParameters parameters, Action<IDialogResult> callback)
{
ShowDialogInternal(name, parameters, callback, true);
}
/// <summary>
/// Shows a modal dialog.
/// </summary>
/// <param name="name">The name of the dialog to show.</param>
/// <param name="parameters">The parameters to pass to the dialog.</param>
/// <param name="callback">The action to perform when the dialog is closed.</param>
/// <param name="windowName">The name of the hosting window registered with the IContainerRegistry.</param>
public void ShowDialog(string name, IDialogParameters parameters, Action<IDialogResult> callback, string windowName)
{
ShowDialogInternal(name, parameters, callback, true, windowName);
}
void ShowDialogInternal(string name, IDialogParameters parameters, Action<IDialogResult> callback, bool isModal, string windowName = null)
{
IDialogWindow dialogWindow = CreateDialogWindow(windowName);
ConfigureDialogWindowEvents(dialogWindow, callback);
ConfigureDialogWindowContent(name, dialogWindow, parameters);
if (isModal)
dialogWindow.ShowDialog();
else
dialogWindow.Show();
}
/// <summary>
/// Create a new <see cref="IDialogWindow"/>.
/// </summary>
/// <param name="name">The name of the hosting window registered with the IContainerRegistry.</param>
/// <returns>The created <see cref="IDialogWindow"/>.</returns>
protected virtual IDialogWindow CreateDialogWindow(string name)
{
if (string.IsNullOrWhiteSpace(name))
return _containerExtension.Resolve<IDialogWindow>();
else
return _containerExtension.Resolve<IDialogWindow>(name);
}
/// <summary>
/// Configure <see cref="IDialogWindow"/> content.
/// </summary>
/// <param name="dialogName">The name of the dialog to show.</param>
/// <param name="window">The hosting window.</param>
/// <param name="parameters">The parameters to pass to the dialog.</param>
protected virtual void ConfigureDialogWindowContent(string dialogName, IDialogWindow window, IDialogParameters parameters)
{
var content = _containerExtension.Resolve<object>(dialogName);
var dialogContent = content as FrameworkElement;
if (dialogContent == null)
throw new NullReferenceException("A dialog's content must be a FrameworkElement");
var viewModel = dialogContent.DataContext as IDialogAware;
if (viewModel == null)
throw new NullReferenceException("A dialog's ViewModel must implement the IDialogAware interface");
ConfigureDialogWindowProperties(window, dialogContent, viewModel);
MvvmHelpers.ViewAndViewModelAction<IDialogAware>(viewModel, d => d.OnDialogOpened(parameters));
}
/// <summary>
/// Configure <see cref="IDialogWindow"/> and <see cref="IDialogAware"/> events.
/// </summary>
/// <param name="dialogWindow">The hosting window.</param>
/// <param name="callback">The action to perform when the dialog is closed.</param>
protected virtual void ConfigureDialogWindowEvents(IDialogWindow dialogWindow, Action<IDialogResult> callback)
{
Action<IDialogResult> requestCloseHandler = null;
requestCloseHandler = (o) =>
{
dialogWindow.Result = o;
dialogWindow.Close();
};
RoutedEventHandler loadedHandler = null;
loadedHandler = (o, e) =>
{
dialogWindow.Loaded -= loadedHandler;
dialogWindow.GetDialogViewModel().RequestClose += requestCloseHandler;
};
dialogWindow.Loaded += loadedHandler;
CancelEventHandler closingHandler = null;
closingHandler = (o, e) =>
{
if (!dialogWindow.GetDialogViewModel().CanCloseDialog())
e.Cancel = true;
};
dialogWindow.Closing += closingHandler;
EventHandler closedHandler = null;
closedHandler = (o, e) =>
{
dialogWindow.Closed -= closedHandler;
dialogWindow.Closing -= closingHandler;
dialogWindow.GetDialogViewModel().RequestClose -= requestCloseHandler;
dialogWindow.GetDialogViewModel().OnDialogClosed();
if (dialogWindow.Result == null)
dialogWindow.Result = new DialogResult();
callback?.Invoke(dialogWindow.Result);
dialogWindow.DataContext = null;
dialogWindow.Content = null;
};
dialogWindow.Closed += closedHandler;
}
/// <summary>
/// Configure <see cref="IDialogWindow"/> properties.
/// </summary>
/// <param name="window">The hosting window.</param>
/// <param name="dialogContent">The dialog to show.</param>
/// <param name="viewModel">The dialog's ViewModel.</param>
protected virtual void ConfigureDialogWindowProperties(IDialogWindow window, FrameworkElement dialogContent, IDialogAware viewModel)
{
var windowStyle = Dialog.GetWindowStyle(dialogContent);
if (windowStyle != null)
window.Style = windowStyle;
window.Content = dialogContent;
window.DataContext = viewModel; //we want the host window and the dialog to share the same data context
if (window.Owner == null)
window.Owner = Application.Current?.Windows.OfType<Window>().FirstOrDefault(x => x.IsActive);
}
}
}