Currently in my app I use the func/lambda method of showing message boxes as explained in the url below:
http://www.deanchalk.me.uk/post/WPF-MVVM-e28093-Simple-e28098MessageBoxShowe28099-With-Action-Func.aspx
To pass the message box text and caption is not a problem, however I also want to pass the image box image and image box type (yes/no etc). These are WPF enumerations. Currently I wrote a few methods to convert those enums into non WPF (own made) enums but it feels a bit tedious to copy every value.
Is it acceptable to use WPF enumerations in ViewModel? (I guess not). And if not, how can I prevent them to be used and still select them in the ViewModel?
I'm slightly confused with your terms ModelView, and ViewModel. With MVVM, there is just the model, the view, and the view model.
That article is talking 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 could do this just as easily using an interface. An approach then would be to create your own enumerations, and then convert them for the MessageBox implementation of the interface.
E.g.
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
}
}
Your view models would then take an IConfirmation as a dependency (in their constructor for example), and in unit tests, you can stub the IConfirmation interface to always return a particular result from the ShowConfirmation method.
You could also overload the ShowConfirmation method to provide options for images, window titles etc.