Reputation: 245
Im new to the Caliburn Micro framework, and am working on an app that has a list view, which you can double click on an item to get take to a detailed view.
THis all works fine, and I current am doing the forward navigation by sending ChangePage messages, which the SHellView picks up and issues an ActivateItem command for the new page.
What I am unable to quite figure out is how to navigate back to a page and keep its state that it was in when you left it? I've read about the Conductor collection but not quite sure how it works in practice?
Does someone have an example where they send ChangePage messages using the eventAggregator and it is processed by the ShellView by checking if that page already exists first and if not create a new one?
Thanks!
UPDATE:
My change page message looks like this:
public class ChangePageMessage
{
public readonly Type _viewModelType;
public ChangePageMessage(Type viewModelType)
{
_viewModelType = viewModelType;
}
}
And my handling of the message in ShellView is:
public void Handle(ChangePageMessage message)
{
if (message._viewModelType == typeof(SearchResultsViewModel))
{
ActivateItem(new SearchResultsViewModel(_eventAggregator));
}
else if(message._viewModelType == typeof(DetailedDocumentViewModel))
{
ActivateItem(new DetailedDocumentViewModel(_eventAggregator));
}
else
{
//here
}
}
Upvotes: 0
Views: 261
Reputation: 169200
You could for example store the visited view models in a list or dictionary in the ShellViewModel
and simply check whether an instance of the type message._viewModelType
already exists in this collection when you receive a ChangePageMessage
event.
If it exists, you return that instance. If not, then you create a new instance, add it to the list or dicionary, and return this one. Something like this:
private readonly Dictionary<Type, Screen> _viewModels = new Dictionary<Type, Screen>();
public void Handle(ChangePageMessage message)
{
if (_viewModels.TryGetValue(message._viewModelType), out Screen viewModel))
{
ActivateItem(viewModel);
}
else if (message._viewModelType == typeof(SearchResultsViewModel))
{
var vm = new SearchResultsViewModel(_eventAggregator);
_viewModels.Add(message._viewModelType, vm);
ActivateItem(vm);
}
else if (message._viewModelType == typeof(DetailedDocumentViewModel))
...
}
Upvotes: 2