Skip to content

Upgrading to Prism v9.0

Damian edited this page Apr 12, 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
// OLD (v8.1.97):
public class MessageBoxViewModel : BindableBase, IDialogAware
{
    // v9.0.271-pre
    DialogCloseListener IDialogAware.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;

        RaiseRequestClose(new DialogResult(result));
    });

    public virtual bool CanCloseDialog()
    {
        return true;  // Allow the dialog to close
    }

    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);
    }
}
  1. Showing a dialog no longer requires the parent window as a parameter.
// NEW:
        _dialogService.ShowDialog(
            nameof(MessageBoxView),
            new DialogParameters($"title={title}&message={message}"));

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

MessageBoxViewModel

Prism v9.0:

public class MessageBoxViewModel : BindableBase, IDialogAware
{
    private string _customMessage = string.Empty;
    private string _title = "Notification";
    private int _maxHeight;
    private int _maxWidth;

    public MessageBoxViewModel()
    {
        Title = "Alert!";

        MaxHeight = 800;
        MaxWidth = 600;
    }

    public event Action<IDialogResult>? RequestClose;

    public string Title { get => _title; set => SetProperty(ref _title, value); }

    public int MaxHeight { get => _maxHeight; set => SetProperty(ref _maxHeight, value); }

    public int MaxWidth { get => _maxWidth; set => SetProperty(ref _maxWidth, value); }

    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;

        RaiseRequestClose(new DialogResult(result));
    });

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

    public virtual bool CanCloseDialog()
    {
        // Allow the dialog to close
        return 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");
    }

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

Previously in Prism v8.1.97:

public class MessageBoxViewModel : BindableBase, IDialogAware
{
    private string _customMessage = string.Empty;
    private string _title = "Notification";
    private int _maxHeight;
    private int _maxWidth;

    public MessageBoxViewModel()
    {
        Title = "Alert!";

        MaxHeight = 800;
        MaxWidth = 600;
    }

    public event Action<IDialogResult>? RequestClose;

    public string Title { get => _title; set => SetProperty(ref _title, value); }

    public int MaxHeight { get => _maxHeight; set => SetProperty(ref _maxHeight, value); }

    public int MaxWidth { get => _maxWidth; set => SetProperty(ref _maxWidth, value); }

    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;

        RaiseRequestClose(new DialogResult(result));
    });

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

    public virtual bool CanCloseDialog()
    {
        // Allow the dialog to close
        return 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");
    }

    public virtual void RaiseRequestClose(IDialogResult dialogResult)
    {
        RequestClose?.Invoke(dialogResult);
    }
}
Clone this wiki locally