|Ok, so here's what I have now. Then a question..
public static class DialogService
private static Dictionary<Type, Type> registry = new Dictionary<Type, Type>();
public static void RegisterDialog(Type dialogType, Type viewModelType)
throw new ArgumentException("The dialogType must be a subclass of type Window.");
bool exists = registry.Where(x => x.Key == dialogType).Any();
public static bool? ShowDialog<TViewModelType>(TViewModelType viewModel) where TViewModelType : _DialogBaseViewModel
Type dialogType = registry.Where(x => x.Value == viewModel.GetType()).Select(x => x.Key).FirstOrDefault();
var dialog = (Window)Activator.CreateInstance(dialogType);
dialog.DataContext = viewModel;
bool? result = dialog.DialogResult;
bool? dialogResult = DialogService.ShowDialog(new PersonViewModel());
The reason I'm forcing the VM to inherit from _DialogBaseViewModel is that _DialogBaseViewModel has a property called DialogResult. I have a DP that is set in a window's header XAML that sets the DialogResult from the VM and closes the window. Works real nice:
How would you return an object from this service?
Assume a dialog prompts the user to enter some data. That data could be stored to a model. I would want the service to return the model if DialogResult was true.
My first thought was to have a static wrapper class around this service that has methods for each type I would want to return. If null is returned then the dialog's DialogResult was false.
What do you think?
If it's not broken, fix it until it is