2025-07-19 09:25:01 +08:00
|
|
|
using DMS.WPF.ViewModels.Dialogs;
|
|
|
|
|
using DMS.WPF.Views.Dialogs;
|
2025-07-27 21:08:58 +08:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Windows;
|
2025-07-04 14:15:44 +08:00
|
|
|
using iNKORE.UI.WPF.Modern.Controls;
|
2025-06-12 13:15:55 +08:00
|
|
|
|
2025-07-27 21:08:58 +08:00
|
|
|
namespace DMS.WPF.Services
|
2025-06-12 13:15:55 +08:00
|
|
|
{
|
2025-07-27 21:08:58 +08:00
|
|
|
public class DialogService : IDialogService
|
|
|
|
|
{
|
|
|
|
|
private readonly IServiceProvider _serviceProvider;
|
|
|
|
|
private static readonly Dictionary<Type, Type> _viewModelViewMap = new Dictionary<Type, Type>
|
|
|
|
|
{
|
|
|
|
|
{ typeof(DeviceDialogViewModel), typeof(DeviceDialog) },
|
|
|
|
|
// { typeof(MqttDialogViewModel), typeof(MqttDialog) }, // 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;
|
|
|
|
|
|
|
|
|
|
_ = dialog.ShowAsync();
|
|
|
|
|
|
|
|
|
|
return await tcs.Task;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentException($"No view registered for view model {viewModel.GetType().Name}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-06-12 13:15:55 +08:00
|
|
|
}
|