Currently, in order to use scoped regions you must manually add views to a region by using the Region.Add method and specify that a scoped region must be created. This means scoped regions don't work with the region navigation framework.
This doesn't seem to be needed right now. I have a solution, and no one else has seemed to have asked for this.
For me this would be very interesting! In my application I've a container view and I fill it with other views. This allow to take the same region subdivision in the shell window across modules and get an additional but different region sub-subdivision for every module. I'm looking for a workaround to navigate to a view added with nestedRegionManager.Add() method in a module.
Brian does the technology and the solution you presented via Pluralsight address this issue?
Sorry Tavi I didn't understand what you mean, what does Pluralsight has to do with my issue? thanks
Yikes 馃槗 my apologies for the lack of context; the topic of navigation to nested regions, if I'm not totally off base here, is presented by Brian in a Pluralsight educational series. I'll double check for relevance.
Don't worry, now it's more clear! Thank you. I don't know if Brian has got a solution for this because I have to see quite precisely between the courses. Now I store the nestedRegionManager after Add() method in my module class and I use eventaggregator in some viewmodels for asking to the module class which view is requested to navigate.
this.eventAggregator.GetEvent<RequestToNavigateEvent>().Publish("starterView_1");
this.eventAggregator.GetEvent<RequestToNavigateEvent>().Publish("starterView_2");
Obviously works but I don't like it. 馃槗
I have a Pluralsight course that shows you how to add scoped region support for the navigation framework:
https://www.pluralsight.com/courses/prism-mastering-tabcontrol
Yep, that's what I thought; we use it in our product and works beautifully.
Hi Brian, I've just seen your Pluralsight course and I find it extremely interesting!
I implemented "ScopedRegionNavigationContentLoader" and "RegionManagerAwareBehavior" as you described. Now when I debug the RegionManager stored in the viewmodel that implement "IRegionManagerAware" I can see all scoped regions. So this is correct now.
But now when I call:
this.RegionManager.RequestNavigate(SubRegionNames.LeftRegion, "starterView_1");
in the main region (it hosts scoped regions with "starterView_1") I got the label "System.Object" instead my view.
The same issue in my module class, if I write:
public void Initialize()
{
var nestedRegionManager = this._regionManager.Regions[RegionNames.MainRegion].Add(this.starterContainerView, "StarterContainerView", true);
nestedRegionManager.RequestNavigate(SubRegionNames.LeftRegion, "starterView_1");
}
I obtain "System.Object" instead my view. I've seen in your multiple shell course that you obtain the same issue if you don't register the view as object.
unityContainer.RegisterType<object, View>("starterView_1");
I register my view with an interface not an object type. So I'm confused because seems like Unity can't find my view by the name so return an object type. Thanks
When using the navigation framework, you must register your views as type Object or it won't work. This is a requirement for using navigation and is discussed in my Introduction to Prism course.
Ok I'll see that point. I think this requirement is a limit for PRISM applications, doesn't it? I register my views with interfaces because take advantage of properties to connect view to a proper viewmodel. In particular every view inherits from IView with a viewmodel property and in the constructor of the view its datacontext is connected to the viewmodel.
No, it's not a limitation. If you don't want to use the navigation framework, then you can continue to register your Views as you are now. If you want to use navigation, you must register as an object because of how resolving objects with DI containers work in general.
@brianlagunas I would very much like this to be part of PRISM. From a user perspektive it makes sense to have a scoped region flag on the RequestNavigation compared to other solutions.
@brianlagunas It seems there _is_ interest in scoped regions. People do not ask because they do not know it exists and it is rather difficult to find it. It will be nice if Prism supports it out of the box.
Maybe re-open the issue?
About the implementation in the course: I think that RegionManagerAwareBehavior is not needed at all. It will be sufficient to assign scoped region manager to IRegionManagerAware on the view(model) in the ScopedRegionNavigationContentLoader when the view is created.
I am a big fan of viewmodel-first navigation. But scoped region manager does not work with viewmodel-first, simply because in
private void InnerAdd(object view, string viewName, IRegionManager scopedRegionManager)
{
...
DependencyObject dependencyObject = view as DependencyObject;
if (dependencyObject != null)
{
Regions.RegionManager.SetRegionManager(dependencyObject, scopedRegionManager);
}
if the view is in fact a view model, it is not a DependencyObject and nothing happens.
Does anybody know any approach to scoped region managers with viewmodel-first?
@dvorn this is really going to be tough sell. I have seen so many different requirements for scoped regions, I would be hesitant to try to add it directly to the framework as I know that it will only work for a certain scenario, and a lot of devs will still have to have their own custom implementations.
In my view, I have tab control with a named region. The content of some of the tabs require a scoped region manager since it may contain multiple instances of the same sub-view.
This thread confirmed that view injection had to be used over the region navigation framework.
What is the best practice for resolving the instance of the sub-view which has its own view model. I need to pass a parameter to the view model so it initializes with the correct data.
I was able to get this to work by overloading the constructor of the view, but another thread suggest this isn't correct. https://github.com/PrismLibrary/Prism/issues/367
Is there is a "cleaner" technique for doing this within PRISM?
Thanks in advance.
case ModulePerson.VIEW_NAME_PERSON_PAYMENTS_TAB:
int personId = Convert.ToInt32(navigationParameters["PersonId"]);
var viewPaymentsTab = tabRegion.Views.OfType<Views.PaymentsView>().SingleOrDefault();
if (viewPaymentsTab == null)
{
viewPaymentsTab = UnityContainer.Resolve<Views.PaymentsView>(new ResolverOverride[] { new ParameterOverride("personId", personId) });
var scopedRegion = tabRegion.Add(viewPaymentsTab, null, true);
Prism.Regions.RegionManager.SetRegionManager(viewPaymentsTab, scopedRegion);
RegionManagerAware.SetRegionManagerAware(viewPaymentsTab, scopedRegion);
}
if (viewPaymentsTab != null) tabRegion.Activate(viewPaymentsTab);
break;
the the code behind in my view I did the following:
public MembershipView()
{
InitializeComponent();
}
public MembershipView(int membershipId) : this()
{
if (DataContext != null)
{
_vm = DataContext as ViewModels.MembershipViewModel;
if (_vm != null)
{
_vm.GetMembershipCommand.Execute(new ViewModels.MembershipEventParams() { MemberId = membershipId });
}
}
}
ViewModels.MembershipViewModel _vm = null;
public int MembershipId
{
get
{
if (_vm != null && _vm.MembershipModel!=null)
{
return _vm.MembershipModel.MemberId;
}
else
{
return 0;
}
}
}
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
@brianlagunas It seems there _is_ interest in scoped regions. People do not ask because they do not know it exists and it is rather difficult to find it. It will be nice if Prism supports it out of the box.
Maybe re-open the issue?
About the implementation in the course: I think that RegionManagerAwareBehavior is not needed at all. It will be sufficient to assign scoped region manager to IRegionManagerAware on the view(model) in the ScopedRegionNavigationContentLoader when the view is created.
I am a big fan of viewmodel-first navigation. But scoped region manager does not work with viewmodel-first, simply because in
if the view is in fact a view model, it is not a DependencyObject and nothing happens.
Does anybody know any approach to scoped region managers with viewmodel-first?