From b1d1f2a396de432cf2610d0752422e1e995fe100 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20B=C3=B6rchers?= Date: Mon, 20 Jul 2020 21:59:30 +0200 Subject: [PATCH] Try to let it work --- App/App.xaml.cs | 2 + App/MainWindowViewModel.cs | 11 +-- Dialogs/DialogService.cs | 127 +++++++++++++++++++++++++-------- Dialogs/MessageBoxView.xaml | 4 +- Dialogs/MessageBoxView.xaml.cs | 15 +--- Dialogs/MessageBoxViewModel.cs | 2 +- 6 files changed, 114 insertions(+), 47 deletions(-) diff --git a/App/App.xaml.cs b/App/App.xaml.cs index ef9cc9a..e5a621a 100644 --- a/App/App.xaml.cs +++ b/App/App.xaml.cs @@ -3,6 +3,7 @@ using Dialogs; using ModernWpf; using ModernWpfPlayground.Types; using Prism.Ioc; +using Prism.Services.Dialogs; namespace ModernWpfPlayground { @@ -21,6 +22,7 @@ namespace ModernWpfPlayground protected override void RegisterTypes(IContainerRegistry containerRegistry) { + containerRegistry.RegisterSingleton(); containerRegistry.Register(); containerRegistry.Register(); containerRegistry.RegisterDialog(); diff --git a/App/MainWindowViewModel.cs b/App/MainWindowViewModel.cs index 0f08602..bf037a0 100644 --- a/App/MainWindowViewModel.cs +++ b/App/MainWindowViewModel.cs @@ -8,6 +8,7 @@ using Microsoft.Win32; using ModernWpfPlayground.MvvmStuff; using ModernWpfPlayground.Types; using Prism.Commands; +using Prism.Logging; using Prism.Services.Dialogs; using YamlDotNet.Serialization; using static ModernWpf.ThemeManager; @@ -18,15 +19,17 @@ namespace ModernWpfPlayground public class MainWindowViewModel : BaseViewModel { private readonly IDialogService _service; + private readonly ILoggerFacade _logger; private const string AppName = "TaBEA 3.0.0"; private string? _path; private string _title = AppName; private readonly ISerializer _serializer; private readonly IDeserializer _deserializer; - public MainWindowViewModel(IDialogService service) + public MainWindowViewModel(IDialogService service, ILoggerFacade logger) { _service = service; + _logger = logger; ShowDialogCommand = new DelegateCommand(ShowDialog); CloseCommand = new DelegateCommand(() => Application.Current.Shutdown()); OpenViewModelCommand = new DelegateCommand(LoadViewModel); @@ -135,12 +138,12 @@ namespace ModernWpfPlayground private void ShowDialogService() { - void executeCallBack(IDialogResult result) + static void ExecuteCallBack(IDialogResult result) { - + MessageBox.Show(Application.Current.MainWindow, "DialogResult = " + result.Result); } - _service.Show(nameof(MessageBoxView), default, executeCallBack); + _service.ShowDialog(nameof(MessageBoxView), default, ExecuteCallBack); } diff --git a/Dialogs/DialogService.cs b/Dialogs/DialogService.cs index b004b4a..650d5f4 100644 --- a/Dialogs/DialogService.cs +++ b/Dialogs/DialogService.cs @@ -1,32 +1,103 @@ -//using System; -//using Prism.Services.Dialogs; +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 -//{ -// public class DialogService : IDialogService -// { -// /// -// public void Show(string name, IDialogParameters parameters, Action callback) -// { -// TODO_IMPLEMENT_ME(); -// } +namespace Dialogs +{ + /// + /// Implements to show modal and non-modal dialogs. + /// + /// + /// The dialog's ViewModel must implement IDialogAware. + /// + public class DialogService : IDialogService + { + private readonly IContainerExtension _containerExtension; -// /// -// public void Show(string name, IDialogParameters parameters, Action callback, string windowName) -// { -// TODO_IMPLEMENT_ME(); -// } + /// + /// Initializes a new instance of the class. + /// + /// + public DialogService(IContainerExtension containerExtension) + { + _containerExtension = containerExtension; + } -// /// -// public void ShowDialog(string name, IDialogParameters parameters, Action callback) -// { -// TODO_IMPLEMENT_ME(); -// } + /// + /// Shows a non-modal dialog. + /// + /// The name of the dialog to show. + /// The parameters to pass to the dialog. + /// The action to perform when the dialog is closed. + public void Show(string name, IDialogParameters parameters, Action callback) + { + throw new NotImplementedException(); + } -// /// -// public void ShowDialog(string name, IDialogParameters parameters, Action callback, string windowName) -// { -// TODO_IMPLEMENT_ME(); -// } -// } -//} \ No newline at end of file + /// + /// Shows a non-modal dialog. + /// + /// The name of the dialog to show. + /// The parameters to pass to the dialog. + /// The action to perform when the dialog is closed. + /// The name of the hosting window registered with the IContainerRegistry. + public void Show(string name, IDialogParameters parameters, Action callback, string windowName) + { + throw new NotImplementedException(); + } + + /// + /// Shows a modal dialog. + /// + /// The name of the dialog to show. + /// The parameters to pass to the dialog. + /// The action to perform when the dialog is closed. + public void ShowDialog(string name, IDialogParameters parameters, Action callback) + { + ShowDialogInternal(name, parameters, callback, null); + } + + /// + /// Shows a modal dialog. + /// + /// The name of the dialog to show. + /// The parameters to pass to the dialog. + /// The action to perform when the dialog is closed. + /// The name of the hosting window registered with the IContainerRegistry. + public void ShowDialog(string name, IDialogParameters parameters, Action callback, + string windowName) + { + ShowDialogInternal(name, parameters, callback, windowName); + } + + void ShowDialogInternal(string name, IDialogParameters parameters, Action callback, + string windowName) + { + var content = _containerExtension.Resolve(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(viewModel, d => d.OnDialogOpened(parameters)); + Action closeHandler = default; + + closeHandler += e => + { + viewModel.RequestClose -= closeHandler; + callback?.Invoke(e); + }; + viewModel.RequestClose += closeHandler; + + if (contentDialog.Owner == null) + contentDialog.Owner = Application.Current?.Windows.OfType().FirstOrDefault(x => x.IsActive); + contentDialog.ShowAsync().Await(); + } + } +} \ No newline at end of file diff --git a/Dialogs/MessageBoxView.xaml b/Dialogs/MessageBoxView.xaml index d8bcec1..7792172 100644 --- a/Dialogs/MessageBoxView.xaml +++ b/Dialogs/MessageBoxView.xaml @@ -7,9 +7,9 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mvvm="http://prismlibrary.com/" xmlns:ui="http://schemas.modernwpf.com/2019" - Title="Save your work?" + Title="{Binding Title}" d:DataContext="{d:DesignInstance local:MessageBoxViewModel}" - d:DesignHeight="450" + d:DesignHeight="450" d:DesignWidth="800" mvvm:ViewModelLocator.AutoWireViewModel="True" CloseButtonText="Cancel" diff --git a/Dialogs/MessageBoxView.xaml.cs b/Dialogs/MessageBoxView.xaml.cs index dbad076..ce111f8 100644 --- a/Dialogs/MessageBoxView.xaml.cs +++ b/Dialogs/MessageBoxView.xaml.cs @@ -1,15 +1,5 @@ -using System; -using System.Collections.Generic; -using System.Text; +using System.Linq; using System.Windows; -using System.Windows.Controls; -using System.Windows.Data; -using System.Windows.Documents; -using System.Windows.Input; -using System.Windows.Media; -using System.Windows.Media.Imaging; -using System.Windows.Navigation; -using System.Windows.Shapes; namespace Dialogs { @@ -21,6 +11,7 @@ namespace Dialogs public MessageBoxView() { InitializeComponent(); - } + Owner = Application.Current.Windows.OfType().FirstOrDefault(x => x.IsActive); + } } } diff --git a/Dialogs/MessageBoxViewModel.cs b/Dialogs/MessageBoxViewModel.cs index 901f7ce..cee0b6e 100644 --- a/Dialogs/MessageBoxViewModel.cs +++ b/Dialogs/MessageBoxViewModel.cs @@ -24,7 +24,7 @@ namespace Dialogs } /// - public string Title { get; } + public string Title { get; } = "Hallo Holger"; private DelegateCommand _closeDialogCommand;