C# WPF PRISM – Pass multiple parameters to UserControl

I am using the PRISM framework for my C# WPF application. The UserControl is inheriting from the IDialogAware Interface. What I’m trying to do, is passing multiple parameters from the MainWindowViewModel to the UserControlViewModel. But when i the OnDialogOpened Method is called, and I’m trying to fill the parameter1, there is an System.FormatException because the value I’m getting from parameters.GetValue("parameter1") is:

"false, parameter2=False"

Sure, I could solve it with the substring() method, but isn’t there a nicer or better way?

MainWindowViewModel:

_dialogService.ShowDialog("SecondUserControl", 
        new DialogParameters($"parameter1={parameter1}, parameter2={parameter2}"), r =>
        {});

UserControlViewModel:

public void OnDialogOpened(IDialogParameters parameters)
{
  var parameter1 = parameters.GetValue<bool>("parameter1");
  var parameter2 = parameters.GetValue<bool>("parameter2");
}

>Solution :

DialogParameters inherits ParametersBase which implements IEnumerable<KeyValuePair<string, object>> and has methods typical for collections like Add(string key, object value) so you can create new instance and insert your parameters or just initialize a new instance with collection initializer. I found an example in "Using dialog service" par of official documentation:

var parameters = new DialogParameters();
parameters.Add("parameter1", value1);
parameters.Add("parameter2", value2);

// or

var parameters = new DialogParameters
    {
        { "parameter1", value1 },
        { "parameter2", value2 }
    };

_dialogService.ShowDialog("SecondUserControl", parameters, r => {});

Leave a Reply