Mvvm popup with viewmodel, parameter and result

I spent two days trying to figure this out.

I have implemented two ways to work with mvvm popups

An example of the first use of aproach:

_childWindowController
                .ShowDialogWithResult<AddNationalityPopup,AddNationalityPopupModel, AddNationalityResult>(
                    (result, a) =>
                    {
                        if (a.DialogResult.HasValue && a.DialogResult.Value)
                        {
                            if (result.NationalityCountryId.HasValue)
                            {
                                Background.NationalityCountryId = result.NationalityCountryId.Value;
                                Background.NationalityDescription = result.NationalityDescription;
                            }
                        }
                    });

      

Second approach:

var window = _childWindowController.CreateDialog<AddNationalityPopup>();

    window.Closed += (sender, args) =>
    {
        if (args.DialogResult.HasValue && args.DialogResult.Value)
        {
            var result = (AddNationalityResult)window.Result;
            if (result.NationalityCountryId.HasValue)
            {
                Background.NationalityCountryId = result.NationalityCountryId.Value;
                Background.NationalityDescription = result.NationalityDescription;
            }
        }
    };

    window.ShowDialog();

      

In the first approach, the user of the service needs to know the view types, view model and result in order to be able to display the dialog

The second interface simplifies a bit, but I still need to know what type produced the result before using it.

Have you ever faced the problem of displaying a dialog with a view model?

How to improve the design of the window service?

Can you provide an example of a good implementation of a dialog service?

+2


source to share


1 answer


I recommend that you take a look at User Experience Patterns as it discusses various approaches you can take to handle user interactions in MVVM. An alternative to using the interop service is to implement the request object...

MVVM . , . , . , , .

This approach provides a simple yet flexible mechanism that maintains a clean separation between model and view views - this allows the view model to encapsulate the application's presentation logic, including any required user interaction, while allowing the visual aspects of the interaction to be completely encapsulated. The implementation of the view model, including expected user interactions through the view, can be easily tested, and the UI designer has great flexibility in choosing how to interact within the view using different behaviors that encapsulate different user interactions.



For an example of how to implement this, I recommend that you familiarize yourself with the Prism 4 source code and samples. The Prism library supports this pattern through the IInteractionRequest and InteractionRequest interfaces . The IInteractionRequest interface defines an event to trigger an interaction, and behavior in a view binds to that interface and subscribes to the event it provides.

You can use the classes and interfaces defined in the assembly Microsoft.Practices.Prism.Interactivity, or use these types as the basis for your dialog service implementation.

+7


source







All Articles