This project is read-only.

Bxf ViewModel lifetime

Sep 19, 2011 at 7:19 PM

Hi folks,

We're developing a business  WPF application  using  CSLA4 and Bxf . We have just encountered an issue to manage the ViewModel lifetime. Cause, we didn't anticipate the ViewModel keep persisting through the pages.

Per exemple, one viewmodel has a timer, to get data data periodically. And this viewmodel continues to get data even when the relevant page is not being viewed because the viewmodel does not get destroyed when the user goes to a different page. (Maybe the timer should be in the page rather than the viewmodel?)

Can ViewModels be destroyed when the user navigates away from the associated page that is using them?

Is there an option with bxf to do this ?

Thanks in advance for your help,

Cédric

Coordinator
Sep 20, 2011 at 5:56 AM

The lifetime of the viewmodel is controlled by the .NET runtime and garbage collector.

If some object references your object, then your object can't be removed by the GC.

If your object uses a timer, then the timer has a reference to your object. The timer is a system object, so it never goes away (probably), so your object will never go away.

Nov 28, 2011 at 8:39 PM

Stopping the timer before calling another view seems to solve the problem:

            _refreshTimer.Stop();            
            Shell.Instance.ShowView(
                typeof (MaintenanceRootView).AssemblyQualifiedName,
                "rootViewModelViewSource", new ViewModels.MaintenanceRootViewModel(),
                "MainContent");

Todd