I'm really new to Prism and trying to understand the proper way to remove a view from a region. I have written a simple WPF MVVM Prism application that has one region (MainRegion) with two buttons (Button A and Button B) in Shell view.
Basically when clicking the Button A it will navigate to ViewA. The same way for the Button B, it will navigate to ViewB when it's clicked. These two views are populated in MainRegion. Here is what I register them in IUnityContainer in Initialize():
Here are codes when clicking Button A and Button B
Is this the right way to remove the view? If I don't call RemoveView(), the new view that is being navigated will be appended to the existing view loaded before.
Please advise.
Thanks,
Brew
Basically when clicking the Button A it will navigate to ViewA. The same way for the Button B, it will navigate to ViewB when it's clicked. These two views are populated in MainRegion. Here is what I register them in IUnityContainer in Initialize():
PublicClass MainModule Implements IModule PrivateReadOnly _container As IUnityContainer PublicSubNew(container As IUnityContainer) Me._container = container EndSubPublicSub Initialize() Implements Microsoft.Practices.Prism.Modularity.IModule.Initialize _container.RegisterType(Of Object, View)("ViewA") _container.RegisterType(Of Object, View)("ViewB") EndSubEndClass
Sub ButtonACommandClicked() RemoveViews() 'To review all views before navigatingDim regionManager = ServiceLocator.Current.GetInstance(Of IRegionManager)() Dim listUri = New Uri("ViewA", UriKind.Relative) regionManager.RequestNavigate("MainRegion", listUri, AddressOf NavigationToViewA) EndSubSub ButtonBCommandClicked() RemoveViews() 'To review all views before navigatingDim regionManager = ServiceLocator.Current.GetInstance(Of IRegionManager)() Dim listUri = New Uri("ViewB", UriKind.Relative) regionManager.RequestNavigate("MainRegion", listUri, AddressOf NavigationToViewB) EndSubPrivateSub RemoveViews() Dim regionManager = ServiceLocator.Current.GetInstance(Of IRegionManager)() Dim views = regionManager.Regions("MainRegion").Views ForEach view In views regionManager.Regions("MainRegion").Remove(view) NextEndSub
Please advise.
Thanks,
Brew