Skip to content

Instantly share code, notes, and snippets.

@Andy1666
Last active April 21, 2023 04:50
Show Gist options
  • Save Andy1666/03a5ccb98603aa7241e04f976382050b to your computer and use it in GitHub Desktop.
Save Andy1666/03a5ccb98603aa7241e04f976382050b to your computer and use it in GitHub Desktop.
PopupDialogService for the "materialDesign PopupDialog" using Prism IDialogAware interface
using Prism.Services.Dialogs;
using System;
using System.Windows;
namespace Sample.Services.Interfaces
{
public interface IPopupDialogService
{
/// <summary>
/// Shows the "materialDesign Popup Dialog" using the IDialogAware interface from Prism.
/// Without ViewModelLocator, Without registering
///
/// => Uses the RootDialogHost
/// </summary>
/// <typeparam name="TView">View (normaly a UserControl)</typeparam>
/// <typeparam name="TViewModel">ViewModel (DataContext)</typeparam>
void ShowPopupDialog<TView, TViewModel>() where TView : FrameworkElement where TViewModel : IDialogAware;
/// <summary>
/// Shows the "materialDesign Popup Dialog" using the IDialogAware interface from Prism.
/// Without ViewModelLocator, Without registering
/// </summary>
/// <typeparam name="TView">View (normaly a UserControl)</typeparam>
/// <typeparam name="TViewModel">ViewModel (DataContext)</typeparam>
/// <param name="dialogHost">The dialogidentifier from materialDesign</param>
void ShowPopupDialog<TView, TViewModel>(string dialogHost) where TView : FrameworkElement where TViewModel : IDialogAware;
/// <summary>
/// Shows the "materialDesign Popup Dialog" using the IDialogAware interface from Prism.
/// Without ViewModelLocator, Without registering
///
/// => Uses the RootDialogHost
/// </summary>
/// <typeparam name="TView">View (normaly a UserControl)</typeparam>
/// <typeparam name="TViewModel">ViewModel (DataContext)</typeparam>
/// <param name="callBack">callBackAction like normal dialog in Prism</param>
void ShowPopupDialog<TView, TViewModel>(Action<IDialogResult> callBack) where TView : FrameworkElement where TViewModel : IDialogAware;
/// <summary>
/// Shows the "materialDesign Popup Dialog" using the IDialogAware interface from Prism.
/// Without ViewModelLocator, Without registering
/// </summary>
/// <typeparam name="TView">View (normaly a UserControl)</typeparam>
/// <typeparam name="TViewModel">ViewModel (DataContext)</typeparam>
/// <param name="dialogHost">The dialogidentifier from materialDesign</param>
/// <param name="callBack">callBackAction like normal dialog in Prism</param>
void ShowPopupDialog<TView, TViewModel>(string dialogHost, Action<IDialogResult> callBack) where TView : FrameworkElement where TViewModel : IDialogAware;
/// <summary>
/// Shows the "materialDesign Popup Dialog" using the IDialogAware interface from Prism.
/// Without ViewModelLocator, Without registering
///
/// => Uses the RootDialogHost
/// </summary>
/// <typeparam name="TView">View (normaly a UserControl)</typeparam>
/// <typeparam name="TViewModel">ViewModel (DataContext)</typeparam>
/// <param name="parameters">dialogParameters like normal dialog in Prism</param>
/// <param name="callBack">callBackAction like normal dialog in Prism</param>
void ShowPopupDialog<TView, TViewModel>(IDialogParameters parameters, Action<IDialogResult> callBack) where TView : FrameworkElement where TViewModel : IDialogAware;
/// <summary>
/// Shows the "materialDesign Popup Dialog" using the IDialogAware interface from Prism.
/// Without ViewModelLocator, Without registering
/// </summary>
/// <typeparam name="TView">View (normaly a UserControl)</typeparam>
/// <typeparam name="TViewModel">ViewModel (DataContext)</typeparam>
/// <param name="dialogHost">The dialogidentifier from materialDesign</param>
/// <param name="parameters">dialogParameters like normal dialog in Prism</param>
/// <param name="callBack">callBackAction like normal dialog in Prism</param>
void ShowPopupDialog<TView, TViewModel>(string dialogHost, IDialogParameters parameters, Action<IDialogResult> callBack) where TView : FrameworkElement where TViewModel : IDialogAware;
}
}
using DryIoc;
using MaterialDesignThemes.Wpf;
using Prism.Ioc;
using Prism.Services.Dialogs;
using System;
using System.Windows;
using Sample.Services.Interfaces;
namespace Sample.Services
{
public class PopupDialogService : IPopupDialogService
{
private readonly IContainerExtension _container;
public PopupDialogService(IContainerExtension container)
{
_container = container;
}
public void ShowPopupDialog<TView, TViewModel>() where TView : FrameworkElement where TViewModel : IDialogAware
{
var dialogView = _container.Resolve<TView>();
dialogView.DataContext = _container.Resolve<TViewModel>();
var viewModel = dialogView.DataContext as IDialogAware;
var closeHandler = new Action<IDialogResult>((e) =>
{
if (viewModel.CanCloseDialog())
{
DialogHost.CloseDialogCommand.Execute(e, null);
viewModel.OnDialogClosed();
}
});
viewModel.RequestClose += closeHandler;
DialogHost.Show(dialogView);
}
public void ShowPopupDialog<TView, TViewModel>(string dialogHost) where TView : FrameworkElement where TViewModel : IDialogAware
{
var dialogView = _container.Resolve<TView>();
dialogView.DataContext = _container.Resolve<TViewModel>();
var viewModel = dialogView.DataContext as IDialogAware;
var closeHandler = new Action<IDialogResult>((e) =>
{
if (viewModel.CanCloseDialog())
{
DialogHost.CloseDialogCommand.Execute(e, null);
viewModel.OnDialogClosed();
}
});
viewModel.RequestClose += closeHandler;
DialogHost.Show(dialogView, dialogHost);
}
public void ShowPopupDialog<TView, TViewModel>(Action<IDialogResult> callBack) where TView : FrameworkElement where TViewModel : IDialogAware
{
var dialogView = _container.Resolve<TView>();
dialogView.DataContext = _container.Resolve<TViewModel>();
var viewModel = dialogView.DataContext as IDialogAware;
var closeHandler = new Action<IDialogResult>((e) =>
{
if (viewModel.CanCloseDialog())
{
DialogHost.CloseDialogCommand.Execute(e, null);
viewModel.OnDialogClosed();
callBack?.Invoke(e);
}
});
viewModel.RequestClose += closeHandler;
DialogHost.Show(dialogView);
}
public void ShowPopupDialog<TView, TViewModel>(string dialogHost, Action<IDialogResult> callBack) where TView : FrameworkElement where TViewModel : IDialogAware
{
var dialogView = _container.Resolve<TView>();
dialogView.DataContext = _container.Resolve<TViewModel>();
var viewModel = dialogView.DataContext as IDialogAware;
var closeHandler = new Action<IDialogResult>((e) =>
{
if (viewModel.CanCloseDialog())
{
DialogHost.CloseDialogCommand.Execute(e, null);
viewModel.OnDialogClosed();
callBack?.Invoke(e);
}
});
viewModel.RequestClose += closeHandler;
DialogHost.Show(dialogView, dialogHost);
}
public void ShowPopupDialog<TView, TViewModel>(IDialogParameters parameters, Action<IDialogResult> callBack) where TView : FrameworkElement where TViewModel : IDialogAware
{
var dialogView = _container.Resolve<TView>();
dialogView.DataContext = _container.Resolve<TViewModel>();
var viewModel = dialogView.DataContext as IDialogAware;
var closeHandler = new Action<IDialogResult>((e) =>
{
if (viewModel.CanCloseDialog())
{
DialogHost.CloseDialogCommand.Execute(e, null);
viewModel.OnDialogClosed();
callBack?.Invoke(e);
}
});
viewModel.RequestClose += closeHandler;
DialogHost.Show(dialogView);
viewModel.OnDialogOpened(parameters);
}
public void ShowPopupDialog<TView, TViewModel>(string dialogHost, IDialogParameters parameters, Action<IDialogResult> callBack) where TView : FrameworkElement where TViewModel : IDialogAware
{
var dialogView = _container.Resolve<TView>();
dialogView.DataContext = _container.Resolve<TViewModel>();
var viewModel = dialogView.DataContext as IDialogAware;
var closeHandler = new Action<IDialogResult>((e) =>
{
if (viewModel.CanCloseDialog())
{
DialogHost.CloseDialogCommand.Execute(e, null);
viewModel.OnDialogClosed();
callBack?.Invoke(e);
}
});
viewModel.RequestClose += closeHandler;
DialogHost.Show(dialogView, dialogHost);
viewModel.OnDialogOpened(parameters);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment