Quantcast
Channel: patterns & practices: Prism
Viewing all articles
Browse latest Browse all 1878

New Post: What would be the best practice to remove a view from a region?

$
0
0
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():
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
Here are codes when clicking Button A and Button B
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
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

Viewing all articles
Browse latest Browse all 1878

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>