Skip to content

Dialog Service

Damian edited this page Jun 3, 2023 · 5 revisions

The DialogService is used to create custom dialog windows (pop-up window) as either Modal or Non-Modal. Modal windows are ones that have a parent/owner window.

Check out our SampleDialogApp for implementation examples.

image

How to Use

Important namespaces:

  • using Prism.Mvvm;
  • using Prism.Services.Dialogs;

It's as easy as, 1, 2, 3, 4!

  1. Create new View and ViewModel which inherits from IDialogAware
  2. Register using, containerRegistry.RegisterDialog<TView, TViewModel>();
  3. Inject into your ViewModel's constructor
  4. Call your injected dialog _dialog.ShowDialog(...), DONE!

Quick Start Sample

This example is loosely based on our SampleDialogApp. You will need to create a new UserControl and ViewModel (i.e. MessageBoxView and MessageBoxViewModel).

Create View and ViewModel

Create a UserControl View which serves as your Dialog. You can optionally add a Button to be used for closing the dialog, for instance, an OK button.

<UserControl ...>
  <StackPanel>
    <Label Content="Hello, I'm a Dialog Box" />
    <Button Content="OK" Command="{Binding CmdResult}" />
  </StackPanel>
</UserControl>

Wire up your dialog's ViewModel

using System;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;

public class MessageBoxViewModel : BindableBase, IDialogAware
{
  public MessageBoxViewModel()
  {
    Title = "Alert!";
  }

  public event Action<IDialogResult>? RequestClose;

  public string CustomMessage { get => _customMessage; set => SetProperty(ref _customMessage, value); }

  public virtual void OnDialogClosed()
  {
    // Detach custom event handlers here, etc.
  }

  public void OnDialogOpened(IDialogParameters parameters)
  {
    var title = parameters.GetValue<string>("title");
    if (!string.IsNullOrEmpty(title))
        Title = title;

    CustomMessage = parameters.GetValue<string>("message");
  }

  public virtual void RaiseRequestClose(IDialogResult dialogResult)
  {
    RequestClose?.Invoke(dialogResult);
  }
}

Register the Dialog

Register your dialog View and ViewModel in your App.axaml.cs

using Prism.DryIoc;
using Prism.Ioc;

public partial class App : PrismApplication
{
  ...
  protected override void RegisterTypes(IContainerRegistry containerRegistry)
  {
    containerRegistry.Register<MainWindow>();
    containerRegistry.RegisterDialog<MessageBoxView, MessageBoxViewModel>();
  }
}

Call Dialog from ViewModel

using Prism.Commands;
using Prism.Services.Dialogs;
using SampleDialogApp.Views;

namespace SampleDialogApp.ViewModels;

public class MainWindowViewModel : ViewModelBase
{
  private readonly IDialogService _dialogService;
  private string _returnedResult = "";

  public MainWindowViewModel(IDialogService dialogService)
  {
    _dialogService = dialogService;
    Title = "My Dialog!";
  }

  public DelegateCommand CmdShowMsgBox => new(() =>
  {
    var title = "MessageBox Title Here";
    var message = "Hello, I am a simple MessageBox modal window with an OK button.\n\n" +
                  "When too much text is added, a scrollbar will appear.";

    _dialogService.ShowDialog(
      nameof(MessageBoxView),    // String: Name of the view
      new DialogParameters($"title={title}&message={message}"),  // Optional parameters to pass to the dialog
      r => { });  // Action<IDialogResult> invoked after window is closed
  });

  ...
}

Coming Soon

  • In PR #75, DialogService allows for an optional parent Window (owner of the dialog) to be passed it. By default, the main Window will be the owner.
  • Make Action<IDialogResult> optional