How to prevent WPF enumerations in ModelView

Currently, in my application, I am using the func / lambda method to display message boxes, as described below in the URL:

http://www.deanchalk.me.uk/post/WPF-MVVM-e28093-Simple-e28098MessageBoxShowe28099-With-Action-Func.aspx

To convey the text of the message and the title of the message is not a problem, however I also want to transfer the image and the image in the image field (yes / no, etc.). These are WPF listings. Currently, I have written several methods for converting these enums to non-WPF (native) enums, but it is a little tedious to copy each value.

Is it possible to use WPF enumerations in the ViewModel? (I think no). And if not, how can I prevent their use and still select them in the ViewModel?

0
source share
1 answer

I am a little confused by your ModelView and ViewModel conditions. With MVVM, there is only a model, view, and presentation model.

This article talks about abstracting the message box so that you can run unit tests without blocking the build server while it waits for user interaction.

The implementation uses the Func delegate, but you can do it just as easily using the interface. An approach then would be to create your own enumerations, and then convert them to implement the MessageBox interface.

eg.

 public enum ConfirmationResult { Yes, No, Cancel ..etc } public enum ConfirmationType { YesNo, OkCancel ..etc } public interface IConfirmation { ConfirmationResult ShowConfirmation(string message, ConfirmationType confirmationType) } public class MessageBoxConfirmation : IConfirmation { ConfirmationResult ShowConfirmation(string message, ConfirmationType confirmationType) { // convert ConfirmationType into MessageBox type here // MessageBox.Show(...) // convert result to ConfirmationResult type } } 

Then your view models perceive IConfirmation as a dependency (for example, in their constructor), and in unit tests you can drown out the IConfirmation interface to always return a specific result from the ShowConfirmation method.

You can also overload the ShowConfirmation method to provide options for images, window titles, etc.

+3
source

Source: https://habr.com/ru/post/909925/


All Articles