Click here to Skip to main content
15,178,370 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Prism DialogService to show a user control in a dialog box(with OK and Cancel buttons), and I use it this way

C#
private void AddProduct()
{
   Product selectedProduct = null;

  _dialogService.ShowDialog("AddProduct", new DialogParameters(" "), r =>
    {
        selectedProduct = r.Parameters.GetValue<Product>("SelectedProduct");
    });

   if(selectedProduct  == null) return
   Products.Add(selectedProduct)
 }


The dialogservice assigned in the constructor call like this:
public ProductViewModel(IDialogService dialogService)
{
  _dialogService = dialogService;
}

The dialog registered in the container:
protected override void RegisterTypes(IContainerRegistry containerRegistry)
       {
           containerRegistry.RegisterDialog<Product, ProductViewModel>();
       }



It works well but I am not able to unit test my code after the return statement
if(selectedProduct  == null) 
, because selectedProduct is always null.

I'm using Nunit and Moq framework to mock the _DialogService.ShowDialog() method.

Maybe my approach for AddProduct dialog is not good, or maybe I am missing something, so any suggestion would be a great help for me

What I have tried:

I mocked the ShowDialog method but it will never set selectedProduct value
Posted
Updated 29-Dec-20 17:05pm
v3

1 solution

This is not a direct reply to your question, but there is an easy way to do dialogs in Prism. Just create your dialog to your taste. Just do not new-up it like this: MyDialog dlg = new MyDialog(); This will violate MVVM prime directives. No. Rather abstract it away behind an interface and use the interface. See this response where Brian Lagunas, the grand master of Prism, explains it:

c# - OpenFileDialog using Prism MVVM - Stack Overflow[^]
   
Comments
rameshk553 29-Dec-20 22:03pm
   
I'm not newing-up like MyDialog dlg = new MyDialog();
instead I'm using container registry to register the dialog like this:
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterDialog<product, productviewmodel="">();
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900