Using IDialogService on Prism in Xamarin Forms

To show a dialog in Prism we have the pre-registered service IPageDialogService, but what happens when you want to show a custom dialog? In version 7.2 they introduced the IDialogService which is a pre-registered service that allows you to show custom dialogs.

Let’s use it step by step

1.Create a view with your custom design

2.Create a ViewModel and implement the IDialogAware interface

In addition, I will create a close command which will raise the RequestClose method to close the view.

3. Register your custom dialog view in the App.xaml.cs

4. Show your dialog

Inject the IDialogService and call the ShowDialog method to show the dialog.

NOTE: If you don’t pass a callback in the latest version of prism it will crash when closing the dialog. The issue was solved two days ago, but a newer version hasn’t been released yet. So, for now, make sure to pass it until the new version is released.

Passing/receiving parameters

To pass parameters you can do it when calling ShowDialog by passing a DialogParameters object and sending the key/value for each parameter.

You will receive the parameters in the OnDialogOpened method of your dialog ViewModel.

Another option is to use the IAutoInitialize in the Dialog ViewModel which will map automatically the name of the parameter sent to a property with the same name.

Show it from the XAML

You can also show it directly from the XAML (No ViewModel needed), just add the Prism reference and in the command property specify the dialog name you want to navigate.

That’s all for now, you can see the full source code used here.

Happy coding!

You may also like