Skip to content

ViewModelActivator class

Attributes: [DebuggerDisplay("{Activated}, {Deactivated}")]

Defined in

Namespace: ReactiveUI.Reactive Assembly: ReactiveUI.Reactive.dll Full name: ReactiveUI.Reactive.ViewModelActivator Modifiers: public sealed

Summary

View source

ViewModelActivator is a helper class that you instantiate in your ViewModel classes in order to help with Activation. Views will internally call this class when the corresponding View comes on screen. This means you can set up resources such as subscriptions to global objects that should be cleaned up on exit. Once you instantiate this class, use the WhenActivated method to register what to do when activated.

View Activation is not the same as being loaded / unloaded; Views are Activated when they enter the Visual Tree, and are Deactivated when they leave the Visual Tree. This is a critical difference when it comes to views that are recycled, such as UITableViews or Virtualizing ScrollViews.

Create this class solely in the Base Class of any classes that inherit from this class (i.e. if you create a FooViewModel that supports activation, the instance should be protected and a child BarViewModel should use the existing ViewModelActivator).

NOTE: You must set up Activation in the corresponding View when using ViewModel Activation.

Applies to

net10.0, net10.0-maccatalyst26.0, net10.0-desktop1.0, net10.0-browserwasm1.0, net10.0-tvos26.0, net10.0-windows10.0.19041, net10.0-macos26.0, net10.0-ios26.0, net10.0-android36.0, net9.0, net9.0-tvos18.0, net9.0-macos15.0, net9.0-maccatalyst18.0, net9.0-windows10.0.19041, net9.0-ios18.0, net9.0-desktop1.0, net8.0, net8.0-windows10.0.19041, net8.0-ios18.0, net8.0-maccatalyst18.0, net8.0-macos15.0, net8.0-tvos18.0, net8.0-ios17.5, net8.0-maccatalyst17.5, net8.0-macos14.5, netstandard2.1, net481, net462, net471

Class hierarchy
classDiagram
class ViewModelActivator
class IDisposable {
    <>
}
IDisposable <|.. ViewModelActivator

Implements: IDisposable

Constructors

NameSummary
.ctorInitializes a new instance of the [ViewModelActivator](# class.

Properties

NameSummary
ActivatedGets a observable which will tick every time the Activator is activated.
DeactivatedGets a observable which will tick every time the Activator is deactivated.

Methods

NameSummary
ActivateThis method is called by the framework when the corresponding View is activated. Call this method in unit tests to simulate a ViewModel being activated.
DeactivateThis method is called by the framework when the corresponding View is deactivated. Respects the activation reference count.
Dispose
Inherited members