and ideas for its refactoring…
Modal dialogs with MVVM and Silverlight 4
In the previous post we covered how to show Views and how to wire them up with their ViewModels while preserving ability to correctly see their preview in Visual Studio and Blend design mode.
In this post i would like to tackle another problem that is very common in MVVM and yet very rarely done correctly.
I will try to provide simple and platform agnostic way of showing Modal Dialogs in MVVM manner without any code in View (later in the post we will reuse same concept and provide way to show simple message dialogs in the same way).
For those of you that have no patience here is the demo application that shows dialogs in action. On the main page there are two identical views each of them showing a list of users. For each user if you click the Edit button you will see the modal dialog to edit users properties.
Del button shows the message dialog to confirm deleting of the user n or to cancel it.
So lets begin: Actual implementation of the concept will be done in Silverlight 4 but it can be easily and without any pain converted to any other UI platform like WPF etc.
Our main design goals are:
- Platform agnostic solution to show modal dialogs that could be easily reused in WPF (or any other platform that supports concept of dialogs)
- We want our modal dialogs to be triggered by some UI event in our Views (like click on the button etc) but not from codebehind of the view
- Actual logic of showing dialogs must reside in ViewModel and NEVER (oh never) in code behind of the dialog (View)
- Dialogs must support MVVM pattern themselves so they should be able to bind to their ViewModel if needed
- Dialogs should be able to return result to the caller (maybe we wont need this always but there must be a way if needed)
Nice thing that could help us a lot is that Silverlight 3/4 comes with very handy ChildWindow control that is perfect fit for what we are trying to do, so we will try to reuse it in smart way.
The question remains how to use Silverlight ChildWindow class with MVVM pattern and to remain decoupled from anything that is platform specific about it.
We will first take the most important things that each Modal Dialog should have and extract this to separate interface IModalWindow:
public interface IModalWindow
{
bool? DialogResult { get; set; }
event EventHandler Closed;
void Show();
object DataContext { get; set; }
void Close();
}
As you can see IModalWindow is almost like an 1:1 abstraction of ChildWindow class ![]()
But its important to note here that IModalWindow contains only pure .NET constructs so it does not have anything Silverlight specific so that means we can implement this interface and create modal dialog controls in any .NET platform (WPF, WinForms etc).
IModalWindow has Show and Close methods, then it has DataContext so we can bind it to its ViewModel, then there is DialogResult boolean property to tell us if user confirmed the action or not, and it has event handler so we can hook to it when dialog is closed to pickup some results or do other actions (if we need it).
In order to be able to actually show dialogs that implement IModalWindow to our user we will introduce another layer of abstraction – it will be service that implements IModalDialogService interface that will have generic methods for showing the dialogs and completely hide the details of the implementation – as we said this should be platform agnostic so interface will contain nothing specific to Silverlight.
here is how the IModalDialogService interface looks:
public interface IModalDialogService
{
void ShowDialog<TViewModel>(IModalWindow view, TViewModel viewModel, Action<TViewModel> onDialogClose);
void ShowDialog<TDialogViewModel>(IModalWindow view, TDialogViewModel viewModel);
}
Our service interface defines two methods, first is one when when we want to specify OnClose handler so that we can do some action when dialog is closed and second overload is for showing modal dialogs in ‘Fire and forget’ mode without knowing when or how the dialog is closed.
Both ShowDialog methods are generic, accepting TViewModel type where we specify type of the ViewModel for our dialog (usually this is ViewModel interface). If we don’t need ViewModel for the dialog we can always set this to null.
So when showing dialog we pass the actual dialog – the View (i will explain later how we will get it) and its ViewModel and optionally we define an Action that is called when the dialog is closed (with the ViewModel instance of the dialog as the parameter – this is how we can get the results of the dialog or data user entered on the dialog etc).
And finally lets see how the Silverlight version of the ModalDialogService is implemented:
public class ModalDialogService : IModalDialogService
{
public void ShowDialog<TDialogViewModel>(IModalWindow view, TDialogViewModel viewModel, Action<TDialogViewModel> onDialogClose)
{
view.DataContext = viewModel;
if (onDialogClose != null)
{
view.Closed += (sender, e) => onDialogClose(viewModel);
}
view.Show();
}
public void ShowDialog<TDialogViewModel>(IModalWindow view, TDialogViewModel viewModel)
{
this.ShowDialog(view, viewModel, null);
}
}
Implementation as you can see is very simple.
Service just sets the passed ViewModel to the DataContext of the passed dialog view, attaches the OnClose handler if we provided it and finally shows the view.
Now lets see the XAML and codebehind of one simple dialog we defined in sample application – dialog to edit user detials:
<controls:ChildWindow x:Class="MvvmModalDialogs.Views.EditUserModalDialogView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:Ignorable="d"
xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls" xmlns:DummyViewModels="clr-namespace:MvvmModalDialogs.ViewModels.DummyViewModels" Width="400" Height="300"
Title="Edit User" d:DataContext="{Binding Source={StaticResource viewModel}}" >
<controls:ChildWindow.Resources>
<DummyViewModels:DummyEditUserModalDialogViewModel x:Key="viewModel" ></DummyViewModels:DummyEditUserModalDialogViewModel>
</controls:ChildWindow.Resources>
<Grid x:Name="LayoutRoot" Margin="2">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TextBlock Text="{Binding Path=User.FullName}" HorizontalAlignment="Center" VerticalAlignment="Center" />
<StackPanel Orientation="Horizontal" Grid.Row="1" HorizontalAlignment="Right" VerticalAlignment="Center">
<Button x:Name="CancelButton" Content="Cancel" Click="CancelButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,5,20,5" />
<Button x:Name="OKButton" Content="OK" Click="OKButton_Click" Width="75" Height="23" HorizontalAlignment="Right" Margin="0,5,20,5" />
</StackPanel>
<Grid Height="142" HorizontalAlignment="Center" Margin="30,17,0,0" Name="grid1" VerticalAlignment="Center" Width="310">
<Grid.RowDefinitions>
<RowDefinition Height="32*" />
<RowDefinition Height="33*" />
<RowDefinition Height="32*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="84*" />
<ColumnDefinition Width="31*" />
<ColumnDefinition Width="195*" />
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Height="23" HorizontalAlignment="Right" Text="User Id:" VerticalAlignment="Center" />
<TextBlock Height="23" HorizontalAlignment="Stretch" Text="{Binding User.Id}" VerticalAlignment="Center" Grid.Row="0" Grid.Column="2" />
<TextBlock Grid.Row="1" Grid.Column="0" Height="23" HorizontalAlignment="Right" Text="Username:" VerticalAlignment="Center" />
<TextBox Height="23" HorizontalAlignment="Stretch" Text="{Binding User.Username, Mode=TwoWay}" VerticalAlignment="Center" Grid.Row="1" Grid.Column="2" />
<TextBlock Grid.Row="2" Grid.Column="0" Height="23" HorizontalAlignment="Right" Text="Is Admin:" VerticalAlignment="Center" />
<CheckBox Height="23" HorizontalAlignment="Stretch" IsChecked="{Binding User.IsAdmin, Mode=TwoWay}" VerticalAlignment="Center" Grid.Row="2" Grid.Column="2" />
</Grid>
</Grid>
</controls:ChildWindow>
So its just simple ChildWindow control, that has few TextBoxes to show and edit details of one User instance.
And in the codebehind of the ChildWindow control we just mark it as implementor of our IChildWindow interface (and we set the DialogResult to true if user clicked OK button, but this can be done in ‘pure’ way via DelegateCommand i just used codebehind here for simplicity and because its not part of the business logic, its concern of the UI to tell us if the user closed dialog by clicking on OK button or not).
public partial class EditUserModalDialogView : IModalWindow
{
public EditUserModalDialogView()
{
InitializeComponent();
}
private void OKButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
}
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
}
}
So all this gives us a completely platform agnostic solution to modal dialogs, and yet we used the powerful ChildWindow control that comes with Silverlight.
If we later decide to implement our modal dialogs in a different way we can do this easily just by creating a different control that implements IChildWindow and we are cool, nothing needs to be changed in our code that shows the dialogs.
So now how would we use this dialog in code?
There is one more peace of the puzzle to solve. We need to register our dialog controls somehow so we can easily pass them to our IModalService to show them so i will use ServiceLocator pattern for this.
We will create a Bootstrapper class that will be called in App.xaml.cs on application startup and this class will have task to register all common dialogs that we will be using in our application:
public class Bootstrapper
{
public static void InitializeIoc()
{
SimpleServiceLocator.SetServiceLocatorProvider(new UnityServiceLocator());
SimpleServiceLocator.Instance.Register<IModalDialogService, ModalDialogService>();
SimpleServiceLocator.Instance.Register<IMessageBoxService, MessageBoxService>();
SimpleServiceLocator.Instance.Register<IMainPageViewModel, MainPageViewModel>();
SimpleServiceLocator.Instance.Register<IModalWindow, EditUserModalDialogView>(Constants.EditUserModalDialog);
}
}
As you can see we are registering (among other things) EditUserModalDialogView that is in fact ChildWindow control as implementor of IModalWindow interface under a name from constants (Constants is the class holding distinct string names for each of the dialog types we want to use in application).
So now that everything is set, and our ServiceLocator knows of our dialogs, we can retrieve new instance of any of our dialogs to show them from the ViewModel of some view (when some button is clicked inside a DelegateCommand etc).
In this example i have only one dialog type that is implementing IModalWindow interface – EditUserModalDialogView, but i could have had many different types of dialogs that i will be showing to users so i would register them all in the Boostrapper as implementors of IModalWindow but under different named constants.
Then i would just retrieve the one i need by asking my service locator for a type that implements IModalWindow and supplying a string key that would determine which dialog exactly i need.
For example to retrieve instance of EditUserModalDialogView i would type this:
var dialog = SimpleServiceLocator.Instance.Get<IModalWindow>(Constants.EditUserModalDialog);
In order to just show dialog without any ViewModel or OnClose callback i would do this:
var dialog = SimpleServiceLocator.Instance.Get<IModalWindow>(Constants.EditUserModalDialog); this.modalDialogService.ShowDialog(dialog, null);
or to set ViewModel for the dialog i could do this:
var dialog = SimpleServiceLocator.Instance.Get<IModalWindow>(Constants.EditUserModalDialog);
this.modalDialogService.ShowDialog(dialog, new EditUserModalDialogViewModel
{
User = userInstanceToEdit
});
These were all simple cases. Now lets see how we can show dialog from DelegateCommand in ViewModal when user clicks on some Button in View, and also let’s set a callback function to be called when dialog is closed so we can do something meaningful like updating user details:
First we crate button on View to trigger the action and bind its Command property to a DelegateCommand in ViewModel and use binding for CommandParameter (in this case binding is the actual User instance):
<Button Content="Edit" Command="{Binding Source={StaticResource viewModelLocator}, Path=ViewModel.ShowUserCommand}" CommandParameter="{Binding}" />
And now in the ViewModel in the ShowUserCommand DelegateCommand we place the code to show the dialog and update Users collection afterwards if needed:
this.ShowUserCommand = new DelegateCommand<User>(userInstanceToEdit =>
{
var dialog = SimpleServiceLocator.Instance.Get<IModalWindow>(Constants.EditUserModalDialog);
this.modalDialogService.ShowDialog(dialog, new EditUserModalDialogViewModel
{
User = userInstanceToEdit
},
returnedViewModelInstance =>
{
if (dialog.DialogResult.HasValue && dialog.DialogResult.Value)
{
var oldPos = this.Users.IndexOf(userInstanceToEdit);
this.Users.RemoveAt(oldPos);
this.Users.Insert(oldPos, returnedViewModelInstance.User);
}
});
});
So basically im setting the DelegateCommand (see details of the DelegateCommand implementation in the project attached to the post) with code to retrieve the Edit User dialog from the ServiceLocator.
Then im calling services ShowDialog method, passing the new instance of EditUserModalDialogViewModel as the ViewModel of the dialog (containing the user instance i want to edit) and im passing lamda for onDialogClose callback to remove old instance of the User from Users collection on ViewModel and replace it with the new one we received from the dialog.
Off course im first checking if user clicked on Ok button (using dialog.DialogResult property to check that).
And there you have it: simple MVVM solution for modal dialogs that you can port to any platform and implement with any kind of control or popup you want.
To show this concept in more detail i created another similar service to show message boxes to the user.
Again we have the service interface – IMessageBoxService that offers two ways of showing message boxes, first one more complex where we specify message, caption and buttons displayed on the message box and second one, simpler where we just specify message and caption, so only OK button is shown to the user:
public interface IMessageBoxService
{
GenericMessageBoxResult Show(string message, string caption, GenericMessageBoxButton buttons);
void Show(string message, string caption);
}
I also created abstractions for the common button types and return results so we don’t depend on Silverlight MessageBox class (that we will use underneath) is using:
public enum GenericMessageBoxButton
{
Ok,
OkCancel
}
public enum GenericMessageBoxResult
{
Ok,
Cancel
}
So lets see how the actual Silverlight implementation of IMessageBoxService interface would look like:
public class MessageBoxService : IMessageBoxService
{
public GenericMessageBoxResult Show(string message, string caption, GenericMessageBoxButton buttons)
{
var slButtons = buttons == GenericMessageBoxButton.Ok
? MessageBoxButton.OK
: MessageBoxButton.OKCancel;
var result = MessageBox.Show(message, caption, slButtons);
return result == MessageBoxResult.OK ? GenericMessageBoxResult.Ok : GenericMessageBoxResult.Cancel;
}
public void Show(string message, string caption)
{
MessageBox.Show(message, caption, MessageBoxButton.OK);
}
}
Again its very simple, we are using the built in Silverlight’s MessageBox class to show the actual message box but later we can decide its not good enough and implement it in another way (maybe with 3rd party message box component etc) yet our code in ViewModel wont change since it will be talking only to our interface.
Here is the example of how i use this MessageBoxService in the demo project for this post:
this.DeleteUserCommand = new DelegateCommand<User>(p =>
{
var result = this.messageBoxService.Show(string.Format("Are you sure you want to delete user {0} ???", p.Username),
"Please Confirm", GenericMessageBoxButton.OkCancel);
if (result == GenericMessageBoxResult.Ok)
{
this.Users.Remove(p);
}
});
What i do here is just set the DelegateCommand in ViewModel to first show the message box with Ok and Cancel buttons and if user pressed Ok do some action – delete from the list.
So i think this two solutions covered all design goals we set on the start: we have decoupled way of showing modal dialogs and message boxes currently done in Silverlight but they could be easily converted to another platform.
Also approach is MVVM friendly without any hacks so any pattern purist can be satisfied with it.
If you have some concerns or suggestions on this approach i would be happy to hear it!
Here is the attached VS 2010 solution with full source code from the post.
Live demo of the application showing modal dialogs and message boxes flying around the screen.
| This entry was posted by roboblob on January 19, 2010 at 15:15, and is filed under MVVM, Programming, Silverlight. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
- DotNetKicks.com
- DotNetShoutout
- Tweets that mention Modal dialogs with MVVM and Silverlight 4 : My Public Interface — Topsy.com
- Binding UI Events from View to commands in ViewModel in Silverlight 4 : My Public Interface
- uberVU – social comments
- Unit Testing Modal Dialogs in MVVM and Silverlight 4 « My Public Interface
- Links (4/27/2010) « Steve Pietrek-Everything SharePoint/Silverlight
- Open Child Window in MVVM Silverlight « My Master Thesis














about 5 months ago
How much of this could be done in VS2008 / SL3, and which bits are VS2010 / SL4 specific?
about 5 months ago
I think there is nothing VS 2010 specific in this code. I used VS 2010 because it has much better design time support for Silverlight.
So feel free to try to convert this to VS 2008 i guess you should not have any problems (if you bump into some issues let me know so i can post warnings here).
Thanks.
about 4 months ago
Hello! You assert, that it is a platform agnostic decision, but as far as I know, WPF window has method ShowDialog intended for showing itself as modal dialog. While you’re using usual Show method. This would not lead to modal behavior, would it?
Thanks
about 4 months ago
Hi wazzzuup,
in my opinion it would be weird to name the method on IModalDialogService called ShowModal. Instead i find the naming IModalDialogService.Show() more natural.
From the name of the interface you can get the context (Modal) and Show is the actual command. This code has nothing to do with the WPF ShowDialog window’s method. Its completely different, new platform agnostic implementation so i have chosen my own naming convention. If you don’t like it feel free to rename it to be as you prefer.
Thanks for your feedback!
about 4 months ago
Dude,
This doesn’t run from your link within your article. Nor does it compile in Vs2k10rc using Silt4rc.
What gives?
about 4 months ago
I have rebuilt the solution in Visual Studio 2010 RC and uploaded it instead of the old version (for Beta2).
Download again and it will work in RC. (if VS complains about missing references just use Rebuild Solution and it will recompile all the projects).
Same goes for the online demo app, its updated to use latest Silverlight 4 version from the RC.
Thanks for notifying me that it was not working!
Cheers!
about 3 months ago
Any chance for a VB.NET version of this?
Would have been nice..
about 3 months ago
Hardly because i don’t do VB so much…
You can always use of the C# => VB translators
about 3 months ago
I’m on it
Thanx btw, your example is really clean and well documented with the text.
about 3 months ago
Roboblob, I runned in to a problem with the differences in C# -> VB.NET. In C# you have a view that implements an interface (IModalWindow) like this:
public partial class EditUserModalDialogView : IModalWindow
The interface IModalWindow has a few methods in it, but the EditUserModalDialogView is not forced by VS to implement these (?). So when I try doing the similar thing in VB.NET like this:
Partial Public Class DummyModalDialogView
Implements IModalWindow
I’m beeing forced to implement all methods from IModalWindow in my codebehind for the view, which breaks the MVVM pattern
So, what am I doing wrong her?
about 3 months ago
Wow that is strange.
Can you send me zipped VB solution for VS 2010 so i can look that up.
send to webmaster [at] roboblob [dot] com
thanks!
about 3 months ago
Solution sent!
Thanx for your effort on this, I’m beginning to regret starting of in VB insted of C#
about 3 months ago
I’ve encountered the exact same problem as @Mcad010. Did you find a solution or workaround?
Thanks
about 3 months ago
I´ve spend the last few days looking into the MVVM and dialogs like this example. What puzzles me is that the ViewModel starts opening up GUI elements.
1) How will one unit test the e.g. DeleteUserCmd when this now requires a response from an MessageBox?
2) Another implementation of a View on top of the ViewModel will be forced to inherit different popups when binding to the commands, which seems unintended.
To me it seems that e.g. a delete user confirmation dialog should reside in the View, since it really has no ViewModel relation. Something like the ability to cancel/suppres the Command if the user regrets this.
I just have no idea at the moment on how to do this.
Any comments?
about 3 months ago
Hi S10,
in my opinion ViewModel should definitely start opening the GUI elements. BUT they need to be abstracted by some interface. So in fact, ViewModel knows not about what kind of GUI element it is or how it works but triggers its showing and waits for response.
To me this is a natural approach. Its application logic to show some question to the user so ViewModel should trigger it and get the response.
In my example real GUI modal window is abstracted by IModalWindow and IModalDialogService and therefore it is decoupled and can be easily tested using RhinoMocks or some stub implementations that would just invoke the Action onDialogClose that ViewModel supplies with some mock value for the purpouse of testing.
If you need realword example on how to test this let me know i can create some post on this subject…
let me know how it went…
Thanks for your feedback its appreciated!
about 3 months ago
Hi
Just wanted to inform you, that the sample (live demo) does not work any longer – generates some kind of ‘beta expired’ message,
cheers
Christian
about 3 months ago
Hi Christian,
i have rebuilt the live demo application and Visual Studio solution attached to the post in the final version of Visual Studio 2010 and Silverlight 4 so its should be ok now.
Thanks for letting me know!
about 3 months ago
Hi
Thanks for you reply.
Firstly it´s great to finally read a blogg that actually shows the implementation of the abstacted dialogs that you hear people speek about in different places
It would be great if you had the time to demonstrate a real world example of how to to unit test these dialogs using RhinoMocs
about 3 months ago
Its done, you can see real-world example of Unit Testing my Modal Dialogs on this my latest blog post:
http://blog.roboblob.com/2010/04/21/unit-testing-modal-dialogs-in-mvvm-and-silverlight-4/
enjoy!
about 3 months ago
Hello,
I found your post helpful which explains MVVM pattern usage in dialog box.
I recently posted a similar article, but based on our framework, which we believe provide a more elegant solution to the existing solution.
Our framework includes full-featured routed event, routed command, delegate command, command reference that complies with WPF specification, and a set of UI toolset that sits on the top of that architecture (which is the missing puzzle in most of MVVM libraries out there).
You can check out my in-depth MVVM discussion at http://intersoftpt.wordpress.com/2010/04/24/clientui-part-3-comprehensive-mvvm-framework-for-silverlight-development/
Our objective is to spread this good news as much as possible to Silverlight developers and community. I hope you can help us by mentioning/ping back to my article from your blog.
Let me know if you have any thoughts or feedback.
Many thanks in advanced,
Jimmy.
about 3 months ago
Yes i see you have some nice things in the ClientUI.
Any idea when will this available to the public?
thanks for sharing!
about 3 months ago
Hey
Thanks for posting the Unit test example. Nice streamlined solution
about 2 months ago
Dude,
I’m going to up the ante by asking you to re-do this thing using MEF.
about 2 months ago
Hi. I just wanted to know if you could provide some guidance on how to implement this solution with the MVVM Light Toolkit. This framework is being very used in a lot of Silverlight applications and it would be great if you could provide some guidance on how to integrate it on your solution.
Thanks a lot for this great article!
about 2 months ago
Hi,
there is plenty of information on MVVM Light Toolkit on the web so i don’t think i will be doing that.
I’m trying to build my own framework so i have no time or wish to integrate my solution with other frameworks.
But it should be straight forward to adjust it so give it a try!
Good luck,
Slobo
about 1 month ago
Hi Slobo,
I modified dialog service whenever moved from CAB to Prism and MVP and MVVM. Your idea looks clean and free from wpf and silverlight.
Thank you so much.
about 1 month ago
Thank you for your feedback,
im glad it works good for you.
Its always good to abstract the framework (Sl, WPF) and work purely with interfaces, whatever there is behind them…
Cheers!
about 1 day ago
Hello,
First thanks for the sample. Next, I took what you have and modified it utilize MEF instead of needing the Unity container.
I can send the source. I may post it on my website when I have time.
Thanks
about 1 day ago
Hi frosty,
i would love to see this example with MEF when you post it somewhere definitely post here a link to the article/code.
Thanks for your feedback!
about 13 hours ago
Hi thanks for the post… I’m using PRISM with MVVM. My question is, can we refer another viewmodel from the parent viewmodel like in the following code.
this.modalDialogService.ShowDialog(dialog, new EditUserModalDialogViewModel
i know this can be done using viewmodel interface and unity. i have modal dialogs showing upo 5 levels (one dialog showing another). so is it ok to have this approach/whats the best solution to tackle the problem. i’m using a controller class to navigate/inject the main views.
about 11 hours ago
Hi Saheer,
in my opinion there is nothing wrong with showing multiple levels of modal dialogs from architecture perspective but it is little weird from usability standpoint (but thats another question).
Your code to show dialog from other dialog looks ok except of the part where you are manually creating EditUserModalDialogViewModel (using the new keyword).
I think this should be avoided and you should not manually create instances of classes (except for builtin .net classes) and you should instead always use Service Locator or Dependency Injection patterns to get new instances of your ViewModels.
If you need to manually create a ViewModel then at least ‘ask’ the Service Locator for the instance of the ViewModel and then set it up and use it how you want. Something Like this:
var viewModel = ServiceLocator.Instance.Get();
this.modalDialogService.ShowDialog(dialog, viewModel);
this way you delegate the responsibility of creating Viewmodel to the centralized service (in this case its ServiceLocator) and then if later you have some changes in the constructor of that ViewModel its not your problem, ServiceLocator will handle this for you and you dont need to change your code.
If you want to see how Service Locator works check my code examples im always using it (underneath the covers of Service Locator im using Microsoft Unity IOC to create those instances).
Hope this helps…
Thanks for your feedback.
Cheers!