1
votes

I've created a dialog service using interfaces to show custom dialog/confirmation boxes (I know that Prism comes with this ability but the dialog boxes don't appear to be customizable and don't match my layout/color scheme). The dialog service has a register function that registers the dialog view type with dialog viewmodel type in a dictionary. This is done so that a viewmodel can be passed in and in a loosely coupled fashion, an instance of the needed view can be created. The code looks like this:

    private readonly IDialogService dialogService = new DialogService(Application.Current.MainWindow);

    // Registers the confirmation window viewmodel with the confirmation window view
    dialogService.Register<ConfirmationWindowViewModel, ConfirmationWindow>();

so my initial thought was to try to create this in the unity bootstrapper (because of the registration passing in views and viewmodels) but I can't seem to find a way to do that and pass in the dialog service.

I must note that the constructor for the main window viewmodel also injects the region manager and the event aggregator for Prism; I had tried creating an instance in the bootstrapper and registering the instance but the creation of the region manager vs the injection causes errors. If I declare and initialize the dialog service in the main window viewmodel it of course works but from my understanding of MVVM we don't want the viewmodels to have any knowledge of the views so I'm trying to find another way to do it, without breaking IoC for region manager and event aggregator.

I am new to MVVM and Prism/Unity so my grasp of these concepts isn't fully solidified yet.

2

2 Answers

1
votes

I know that Prism comes with this ability but the dialog boxes don't appear to be customizable and don't match my layout/color scheme

You can create whatever you like as dialog, just derive from PopupWindowAction and override CreateWindow (and other methods as needed) to create the dialog you always wanted.

0
votes

In case anyone sees this later and is curious, my end decision was to get rid of the 'Register' function altogether in favor of a solid convention instead.

Previously, I would use this function and kept a dictionary of all the registered views/viewmodels:

    dialogService.Register<ConfirmationWindowViewModel, ConfirmationWindow>();

this would register take the and store them in the dictionary so I could later pass in a viewmodel and create an instance of the appropriate confirmation message view. Instead I removed all code regarding this part of the solution and replaced it with some reflection mixed in with naming conventions.

Step 1: Ensure all views are named with the suffix View at the end. Step 2: Ensure all viewmodels are named with the suffix ViewModel at the end. Step 3: Ensure these are all in appropriately named namespaces (views in views namespace and viewmodels in viewmodels namespace).

(most of this ^^ is done anyway)

Final Step: Replaced dictionary with this code:

        var viewTypeName = viewModel.GetType().AssemblyQualifiedName.Replace("Model", ""); 
        var viewType = Type.GetType(viewTypeName);

in the dialog interface. Now, no matter what viewmodel is passed in, it will pull the appropriate view with less code and no necessary linking as was done before.