Try to let it work

This commit is contained in:
Holger Börchers 2020-07-20 21:59:30 +02:00
parent 7ad587228a
commit b1d1f2a396
6 changed files with 114 additions and 47 deletions

View File

@ -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<IDialogService, Dialogs.DialogService>();
containerRegistry.Register<MainWindow>();
containerRegistry.Register<MainWindowViewModel>();
containerRegistry.RegisterDialog<MessageBoxView, MessageBoxViewModel>();

View File

@ -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);
}

View File

@ -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
// {
// /// <inheritdoc />
// public void Show(string name, IDialogParameters parameters, Action<IDialogResult> callback)
// {
// TODO_IMPLEMENT_ME();
// }
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;
// /// <inheritdoc />
// public void Show(string name, IDialogParameters parameters, Action<IDialogResult> callback, string windowName)
// {
// TODO_IMPLEMENT_ME();
// }
/// <summary>
/// Initializes a new instance of the <see cref="DialogService"/> class.
/// </summary>
/// <param name="containerExtension"></param>
public DialogService(IContainerExtension containerExtension)
{
_containerExtension = containerExtension;
}
// /// <inheritdoc />
// public void ShowDialog(string name, IDialogParameters parameters, Action<IDialogResult> callback)
// {
// TODO_IMPLEMENT_ME();
// }
/// <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();
}
// /// <inheritdoc />
// public void ShowDialog(string name, IDialogParameters parameters, Action<IDialogResult> callback, string windowName)
// {
// TODO_IMPLEMENT_ME();
// }
// }
//}
/// <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();
}
}
}

View File

@ -7,7 +7,7 @@
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:DesignWidth="800"

View File

@ -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<Window>().FirstOrDefault(x => x.IsActive);
}
}
}

View File

@ -24,7 +24,7 @@ namespace Dialogs
}
/// <inheritdoc />
public string Title { get; }
public string Title { get; } = "Hallo Holger";
private DelegateCommand<string> _closeDialogCommand;