Skip to content

Upgrading to Prism v9.0

Damian edited this page Apr 13, 2024 · 9 revisions

Upgrading to Prism v9.0

v8.1.97 to v9.0.271-pre

WARNING: Work in progress

General Changes

  • Namespace: Prism.Regions -> Prism.Navigation.Regions
  • IDialogService and IDialogAware - New closing mechanism.

Dialogs

  1. Closing a dialog via RequestClose changed from event to DialogCloseListener
  2. Showing a dialog no longer requires the parent window as a parameter.
  3. Setting a custom ParentWindow with, KnownDialogParameters.ParentWindow
    • By default the parent window is the root main window.
    • You can set an alternate Window via the DialogParameter, KnownDialogParameters.ParentWindow
    • Sample (from *.axaml.cs): new DialogParameters() { { KnownDialogParameters.ParentWindow, this.Parent} });
  4. Setting a custom dialog window with KnownDialogParameters.WindowName
// NEW:
_dialogService.ShowDialog(
    nameof(MessageBoxView),
    new DialogParameters($"title={title}&message={message}"));

// OLD:
_dialogService.ShowDialog(
    nameof(MessageBoxView),
    new DialogParameters($"title={title}&message={message}"));

MessageBoxViewModel

public class MessageBoxViewModel : BindableBase, IDialogAware
{
    // v9.0.271-pre
    public DialogCloseListener RequestClose { get; }

    // v8.1.97
    // public event Action<IDialogResult>? RequestClose;

    public DelegateCommand<string> CmdResult => new DelegateCommand<string>((param) =>
    {
        // None = 0, OK = 1, Cancel = 2, Abort = 3, Retry = 4, Ignore = 5, Yes = 6, No = 7
        ButtonResult result = ButtonResult.None;

        if (int.TryParse(param, out int intResult))
            result = (ButtonResult)intResult;

        // v9.0.271-pre
        RequestClose.Invoke(result);

        // v8.1.97
        // RaiseRequestClose(new DialogResult(result));
    });

    /// <summary>Allow the dialog to close</summary>
    public virtual bool CanCloseDialog() => true;

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

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