-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDialogService.cs
More file actions
113 lines (102 loc) · 3.58 KB
/
DialogService.cs
File metadata and controls
113 lines (102 loc) · 3.58 KB
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
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Lemon.ModuleNavigation.Abstractions;
using Lemon.ModuleNavigation.Core;
using Microsoft.Extensions.DependencyInjection;
namespace Lemon.ModuleNavigation.Avaloniaui;
public class DialogService : IDialogService
{
private readonly IServiceProvider _serviceProvider;
public DialogService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public void Show(string name,
string? windowName = null,
IDialogParameters? parameters = null,
Action<IDialogResult>? callback = null)
{
ShowCore(name, windowName, false, parameters, callback).Wait();
}
public async Task ShowDialog(string name,
string? windowName = null,
IDialogParameters? parameters = null,
Action<IDialogResult>? callback = null)
{
await ShowCore(name, windowName, true, parameters, callback);
}
public IDialogResult WaitShowDialog(string name,
string? windowName = null,
IDialogParameters? parameters = null,
Action<IDialogResult>? callback = null)
{
return ShowDialogCoreSync(name, windowName, parameters, callback);
}
private IDialogResult ShowDialogCoreSync(string name,
string? windowName,
IDialogParameters? parameters = null,
Action<IDialogResult>? callback = null)
{
var tcs = new TaskCompletionSource<IDialogResult>();
var task = ShowCore(name,
windowName,
true,
parameters,
result =>
{
callback?.Invoke(result);
tcs.TrySetResult(result);
});
return tcs.Task.WaitOnDispatcherFrame();
}
private async Task ShowCore(string name,
string? windowName,
bool showDialog,
IDialogParameters? parameters = null,
Action<IDialogResult>? callback = null)
{
IDialogWindow dialogWindow;
if (string.IsNullOrEmpty(windowName))
{
dialogWindow = _serviceProvider.GetRequiredKeyedService<IDialogWindow>(DefaultDialogWindow.Key);
}
else
{
dialogWindow = _serviceProvider.GetRequiredKeyedService<IDialogWindow>(windowName);
}
var dialogViewModel = _serviceProvider.GetRequiredKeyedService<IDialogAware>(name);
dialogWindow.Title = dialogViewModel.Title;
dialogWindow.Content = _serviceProvider.GetRequiredKeyedService<IView>(name);
dialogWindow.DataContext = dialogViewModel;
dialogViewModel.OnDialogOpened(parameters);
bool handled = false;
void RequestCloseHandler(IDialogResult dialogResult)
{
callback?.Invoke(dialogResult);
handled = true;
dialogWindow.Close();
}
dialogViewModel.RequestClose += RequestCloseHandler;
dialogWindow.Closed += (s, e) =>
{
dialogViewModel.RequestClose -= RequestCloseHandler;
if (!handled)
{
callback?.Invoke(new DialogResult(ButtonResult.None));
}
dialogViewModel.OnDialogClosed();
};
if (showDialog)
{
if (Application.Current!.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime classicDesktop)
{
var owner = classicDesktop.MainWindow!;
await dialogWindow.ShowDialog(owner);
}
}
else
{
dialogWindow.Show();
}
}
}