View model load event?

Topics: Prism v4 - Silverlight 4
Nov 14, 2011 at 10:59 PM

I’m new to Prism and I’m looking for solution for some issue with loading views and view models. I have view registered like this

_regionManager.RegisterViewWithRegion(RegionNames.ExamRegion, () => _container.Resolve<Views.ExamView>());

 And viewmodel


 I’m navigating to view using navigation system and uriMapper:UriMapper

For now I’m executing some initialization of ViewModel by attaching viewmodel to loaded event of view

        <i:EventTrigger EventName="Loaded">
            <ei:CallMethodAction MethodName="ViewLoaded" TargetObject="{Binding}" />
        <i:EventTrigger EventName="Unloaded">
            <ei:CallMethodAction MethodName="ViewUnloaded" TargetObject="{Binding}" />

 But this event (Loaded) is executing only once (for first entry) and when I navigate to this view again I have same data from old viewmodel and I would like to start from new view with base data for every entry to view. ExamViewModel Is registed not as singleton but still it is always the same model for every entry. How can I force to load new view model? How Can I execute some initialization of data when viewmodel Is attached to view?

Nov 15, 2011 at 8:03 PM


You might find the IRegionMemberLifetime interface useful to fulfill the requirements of your scenario. The IRegionMemberLifetime interface allows you to control the lifetime of views within regions by defining a single property named KeepAlive. Usually, when a view is navigated from (that is, when the view is the active one and a navigation request to another view is executed) the view is deactivated. Then, if the KeepAlive property of the view model returns false, the deactivated view is removed from the region. So, when a navigation request to that view is performed, a new instance of the view should be added to the region.

You can find more information about the IRegionMemberLifetime interface in the following chapter of the Prism documentation:

If this does not help you to resolve this undesired behavior, it would be useful if you could provide us with a repro-sample application portraying this problem so that we could help you understand the causes behind it.

I hope you find this useful,

Damian Cherubini