1

I have been following the Prism 7.2.0.1367 release notes. I am able to navigate to a dialog ViewModel using dialogService.ShowDialog(NavigationStrings.MyViewModel). But when I close it, I get a null reference exception on RequestClose.

I have a ViewModel like so in my Xamarin Forms project:

public class MyViewModel : BaseViewModel, IDialogAware, IAutoInitialize
{
    private DelegateCommand _closeCommand;

    public DelegateCommand CloseCommand => _closeCommand ?? (_closeCommand = new DelegateCommand(Close));

    public event Action<IDialogParameters> RequestClose;

    public MyViewModel(INavigationService navigationService) : base(navigationService) { }

    public override void OnAppearing()
    {
        base.OnAppearing();
    }

    private void Close()
    {
        RequestClose(null);
    }

    public bool CanCloseDialog() => true;

    public void OnDialogClosed()
    {
        Console.WriteLine("The Demo Dialog has been closed...");
    }

    public void OnDialogOpened(IDialogParameters parameters)
    {
        // No need to do anything as IAutoInitialize will take care of what we need here...
    }
}

What am I supposed to set RequestClose to to stop it from being null? None of the documentation says anything about having to set this event.

Chucky
  • 1,701
  • 7
  • 28
  • 62

2 Answers2

0

RequestClose is null because you never subscribe to this event. You should test if RequestClose is null before calling RequestClose(null).

You can subscribe to this event if you want to handle the closing with a custom behavior.

Edited with the check if the action is null :

public class MyViewModel : BaseViewModel, IDialogAware, IAutoInitialize
{
    private DelegateCommand _closeCommand;

    public DelegateCommand CloseCommand => _closeCommand ?? (_closeCommand = new DelegateCommand(Close));

    public event Action<IDialogParameters> RequestClose;

    public MyViewModel(INavigationService navigationService) : base(navigationService) { }

    public override void OnAppearing()
    {
        base.OnAppearing();
    }

    private void Close()
    {
        RequestClose?.Invoke(null);
    }

    public bool CanCloseDialog() => true;

    public void OnDialogClosed()
    {
        Console.WriteLine("The Demo Dialog has been closed...");
    }

    public void OnDialogOpened(IDialogParameters parameters)
    {
        // No need to do anything as IAutoInitialize will take care of what we need here...
    }
}
  • Okay. And if I don't need any custom behavior? – Chucky Aug 20 '19 at 17:03
  • Just comment the RequestClose(null) – Frédéric THEVENON Aug 20 '19 at 19:23
  • I'm afraid that does not work. RequestClose is used with IDialogAware in Prism to close dialogs. You call it in a command from your UI. – Chucky Aug 21 '19 at 07:47
  • I've edited my answer with the null check of the action (in the Close method) – Frédéric THEVENON Aug 21 '19 at 10:10
  • 2
    I guess the framework should have subscribed to the event, and, in theory, it's guaranteed not to be null while the view model lives. – Haukinger Aug 21 '19 at 10:46
  • I've found a workaround for it that involves providing the ShowDialog method with a callback for when it's closed, but I don't feel that this should be necessary. You can read my issue here: https://github.com/PrismLibrary/Prism/issues/1883 – Chucky Aug 21 '19 at 12:36
0

Pass the "CallBack" when navigating. Ex:

dialogService.ShowDialog("DialogView", CloseDialogCallback);

void CloseDialogCallback(IDialogResult dialogResult)
    {

    }