Navigation between Parent / Child Views

Topics: Getting Started
Mar 20, 2012 at 8:55 PM

I am having a hard time figuring out how to correctly implement navigation in my application.

Think about a simple application with 2 Views:

ShellView: Shows a list of items along with an "Add" button

AddItemView: Shows text boxes to allow the user to enter the data of a new item.

How would I show the AddItemView after a click on the Add button?

I tried using IWindowManager but that doesn't seem to be the correct choice because of the following:

It wants an instance of the ViewModel whose View should be shown. Problem is, the ShellViewModel doesn't have an instance of AddItemViewModel and can't easily create one. I am using a DI container to wire up my dependencies, so I want to avoid newing up that instance directly in ShellViewModel, because then it would also need to know about all references of AddItemViewModel. I don't want to inject an instance of AddItemViewModel into ShellViewModel because that would mean that every click onto the "Add" button would show the same instance with all the consequences like stale data in combo boxes that are populated in the constructor.

What am I missing?

Mar 21, 2012 at 1:08 AM

Checkout the Caliburn.Micro.HelloWindowManagerWP71 and grab the ShowDialog result.  Then you can do:

 public IEnumerable<IResult> ShowInputDialog() {
            var showDialog = new ShowDialog<DialogViewModel>();
            yield return showDialog;

            var result = showDialog.Dialog.Result;
            if(result != null) {
                yield return new ShowDialog<MessageViewModel>()
                    .ConfigureWith(x => x.Message = "The user entered: " + result);

Mar 21, 2012 at 9:46 PM

Thanks, that looks great!