Files
DMS/DMS.WPF/Services/DialogService.cs

75 lines
3.2 KiB
C#
Raw Permalink Normal View History

2025-09-23 06:51:29 +08:00
using DMS.WPF.Interfaces;
2025-07-19 09:25:01 +08:00
using DMS.WPF.ViewModels.Dialogs;
using DMS.WPF.Views.Dialogs;
2025-07-04 14:15:44 +08:00
using iNKORE.UI.WPF.Modern.Controls;
namespace DMS.WPF.Services
{
public class DialogService : IDialogService
{
private readonly IServiceProvider _serviceProvider;
private static readonly Dictionary<Type, Type> _viewModelViewMap = new Dictionary<Type, Type>
{
{ typeof(DeviceDialogViewModel), typeof(DeviceDialog) },
{ typeof(ConfirmDialogViewModel), typeof(ConfirmDialog) },
2025-07-30 12:54:14 +08:00
{ typeof(VariableTableDialogViewModel), typeof(VariableTableDialog) },
2025-08-22 20:24:09 +08:00
{ typeof(ImportExcelDialogViewModel), typeof(ImportExcelDialog) },
2025-08-25 21:26:18 +08:00
{ typeof(ImportOpcUaDialogViewModel), typeof(ImportOpcUaDialog) },
{ typeof(VariableDialogViewModel), typeof(VariableDialog) },
2025-09-02 17:42:11 +08:00
{ typeof(PollLevelDialogViewModel), typeof(PollLevelDialog) },
{ typeof(IsActiveDialogViewModel), typeof(IsActiveDialog) },
2025-09-06 19:10:25 +08:00
{ typeof(MqttDialogViewModel), typeof(MqttDialog) },
{ typeof(MqttSelectionDialogViewModel), typeof(MqttSelectionDialog) },
2025-09-12 08:57:41 +08:00
{ typeof(MqttAliasBatchEditDialogViewModel), typeof(MqttAliasBatchEditDialog) },
2025-09-12 12:26:57 +08:00
{ typeof(HistorySettingsDialogViewModel), typeof(HistorySettingsDialog) },
2025-09-13 19:08:43 +08:00
{ typeof(AlarmSettingsDialogViewModel), typeof(AlarmSettingsDialog) },
2025-09-13 19:39:18 +08:00
{ typeof(EmailAccountDialogViewModel), typeof(EmailAccountDialog) },
2025-09-14 19:13:40 +08:00
{ typeof(EmailTemplateDialogViewModel), typeof(EmailTemplateDialog) },
{ typeof(TriggerDialogViewModel), typeof(TriggerDialog) },
2025-10-20 12:36:33 +08:00
{ typeof(InputDialogViewModel), typeof(InputDialog) },
{ typeof(TriggerSelectionDialogViewModel), typeof(TriggerSelectionDialog) }
2025-09-06 19:10:25 +08:00
// Add other mappings here
// ... other dialogs
};
public DialogService(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public async Task<TResult> ShowDialogAsync<TResult>(DialogViewModelBase<TResult> viewModel)
{
if (_viewModelViewMap.TryGetValue(viewModel.GetType(), out var viewType))
{
var tcs = new TaskCompletionSource<TResult>();
var dialog = (ContentDialog)_serviceProvider.GetService(viewType);
if (dialog == null)
{
// If not registered in DI, create an instance directly
dialog = (ContentDialog)Activator.CreateInstance(viewType);
}
dialog.DataContext = viewModel;
Func<TResult, Task> closeHandler = null;
closeHandler = async (result) =>
{
viewModel.CloseRequested -= closeHandler;
dialog.Hide();
tcs.SetResult(result);
};
viewModel.CloseRequested += closeHandler;
2025-07-28 13:06:36 +08:00
_ = await dialog.ShowAsync();
return await tcs.Task;
}
else
{
throw new ArgumentException($"No view registered for view model {viewModel.GetType().Name}");
}
}
}
}