After weeks of using Prism I have been through every kind of navigation methods. But there is still one thing that I haven't been able to achieve with Prism "out-of-the-box": navigate to a view-model instance. It is really easy to navigate to a view-model or view type but there is actually no way to navigate to a real view-model instance.
For example when I have a list of different view-model instances (different type too), if one is selected and I want to display it in my content region, I cannot achieve it unless I publish the change through event aggregation and I do not want that because this gets me stuck to one kind of view in my content region. What I want to do is to register a view-model instance into a region, so the view can load dynamically from data templates.
Actually I achieved to do this by creating a RegisterViewModelWithRegion as an extension method to RegionManager, it works well but it is not really neat as I have to manually lookup through my application resources, load the view, attach my view-model instance and then add the view to my region.
I rea开发者_运维技巧lly feel that I am missing something into the architecture because this kind of practice seem obvious to me but apparently I cannot find anybody having the same concern.
Your help and experience would much appreciated. Thanks.
After some discussions with the Prism team, I found out that actually, using the Region.Add(object view)
method to inject a view-model in a region is totally ok. It will give exactly the same result (except for some minor case).
I inspected the ItemsControlRegionAdapter and all it does in the end is
regionTarget.ItemsSource = region.Views;
regionTarget
being the target control (ListBox for example) and region.Views
property being the objects you inject into your regions.
Of course, thanks for your help Rachel :-)
Take a look at this article I wrote on using Navigation with MVVM
You need a ViewModel
for your application shell that defines which ViewModel
is the CurrentPage
. To change ViewModels, you just fire an Event which the shell will pick up to set the CurrentPage
to whatever ViewModel should be current.
You can switch pages with something like this with PRISM:
eventAggregator.GetEvent<ChangePageEvent>()
.Publish(new ChangePageEventArgs(this));
I'm sure there's other ways to accomplish this, but so far I find this way the simplest
精彩评论