mirror of
https://github.com/holgerb83/ModernWpfPlayground.git
synced 2025-04-19 23:03:49 +02:00
103 lines
4.3 KiB
C#
103 lines
4.3 KiB
C#
using System;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using ModernWpf.Controls;
|
|
using Prism.Common;
|
|
using Prism.Ioc;
|
|
using Prism.Services.Dialogs;
|
|
|
|
namespace 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)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
/// <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)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
/// <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, null);
|
|
}
|
|
|
|
/// <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, windowName);
|
|
}
|
|
|
|
void ShowDialogInternal(string name, IDialogParameters parameters, Action<IDialogResult> callback,
|
|
string windowName)
|
|
{
|
|
var content = _containerExtension.Resolve<object>(name);
|
|
if (!(content is ContentDialog contentDialog))
|
|
throw new NullReferenceException("A dialog's content must be a content dialog");
|
|
|
|
if (!(contentDialog.DataContext is IDialogAware viewModel))
|
|
throw new NullReferenceException("A dialog's ViewModel must implement the IDialogAware interface");
|
|
|
|
MvvmHelpers.ViewAndViewModelAction<IDialogAware>(viewModel, d => d.OnDialogOpened(parameters));
|
|
Action<IDialogResult> closeHandler = default;
|
|
|
|
closeHandler += e =>
|
|
{
|
|
viewModel.RequestClose -= closeHandler;
|
|
callback?.Invoke(e);
|
|
};
|
|
viewModel.RequestClose += closeHandler;
|
|
|
|
if (contentDialog.Owner == null)
|
|
contentDialog.Owner = Application.Current?.Windows.OfType<Window>().FirstOrDefault(x => x.IsActive);
|
|
contentDialog.ShowAsync().Await();
|
|
}
|
|
}
|
|
} |