first edit
This commit is contained in:
74
XLIMS.SERVICES/DialogService.cs
Normal file
74
XLIMS.SERVICES/DialogService.cs
Normal file
@@ -0,0 +1,74 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Windows;
|
||||
using XLIMS.CONTRACT;
|
||||
using XLIMS.PSV.ViewModels;
|
||||
using XLIMS.PSV.Windows;
|
||||
|
||||
namespace XLIMS.SERVICES;
|
||||
|
||||
public class DialogService : IDialogService
|
||||
{
|
||||
// Словарь: ViewModel → соответствующий Window
|
||||
private readonly Dictionary<Type, Type> _mappings = new();
|
||||
|
||||
public DialogService()
|
||||
{
|
||||
// Регистрируем соответствия (можно вынести в отдельный конфигуратор)
|
||||
Register<BrowseTprzViewModel, EditWindow>();
|
||||
Register<BrowseSerialViewModel, EditWindow>();
|
||||
Register<PsvViewModel, PsvWindow>();
|
||||
Register<GoodViewModel,EditWindow>();
|
||||
Register<BadViewModel,EditWindow>();
|
||||
// и т.д.
|
||||
}
|
||||
|
||||
public void Register<TViewModel, TWindow>()
|
||||
where TViewModel : class
|
||||
where TWindow : Window, new()
|
||||
{
|
||||
_mappings[typeof(TViewModel)] = typeof(TWindow);
|
||||
}
|
||||
|
||||
public void ShowDialog<TViewModel>(TViewModel viewModel)
|
||||
where TViewModel : class
|
||||
{
|
||||
ShowDialogInternal(viewModel, true);
|
||||
}
|
||||
|
||||
public bool? ShowDialog<TViewModel>(TViewModel viewModel, out object? result)
|
||||
where TViewModel : class
|
||||
{
|
||||
var dialogResult = ShowDialogInternal(viewModel, true);
|
||||
result = (dialogResult.Owner as Window)?.DialogResult;
|
||||
return dialogResult.DialogResult;
|
||||
}
|
||||
|
||||
// Немодальное окно (опционально)
|
||||
public void Show<TViewModel>(TViewModel viewModel)
|
||||
where TViewModel : class
|
||||
{
|
||||
ShowDialogInternal(viewModel, false);
|
||||
}
|
||||
|
||||
private Window ShowDialogInternal(object viewModel, bool modal)
|
||||
{
|
||||
var vmType = viewModel.GetType();
|
||||
if (!_mappings.TryGetValue(vmType, out var windowType))
|
||||
throw new InvalidOperationException($"Нет зарегистрированного окна для ViewModel {vmType}");
|
||||
|
||||
var window = (Window)Activator.CreateInstance(windowType)!;
|
||||
window.DataContext = viewModel;
|
||||
|
||||
// Если главное окно уже открыто — устанавливаем Owner (чтобы окно не уходило за него)
|
||||
if (Application.Current?.MainWindow != null && Application.Current.MainWindow != window)
|
||||
window.Owner = Application.Current.MainWindow;
|
||||
|
||||
if (modal)
|
||||
window.ShowDialog();
|
||||
else
|
||||
window.Show();
|
||||
|
||||
return window;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user