Radical Documentation
View on GitHub
Primary version
Primary version
  • Home
  • Presentation
    • AbstractViewModel
    • Conventions
      • Bootstrap Conventions
      • Runtime Conventions
      • Conventions override
    • Commands and DelegateCommand
    • IViewResolver
      • Default view behaviors
      • view life cycle events
        • Callback expectations
        • notify messages
    • Message broker MVVM built-in messages
    • Application boot process
      • Application configuration
      • Application shutdown
      • Singleton applications
    • AbstractMementoViewModel
      • Simple ViewModel graphs
      • Collections and complex ViewModel graphs
    • Validation and Validation Services
    • Resources
      • Services as resources
      • ViewModels as resources
  • UI Composition
    • UI Composition
      • Region content lifecycle
      • TabControl region
      • Create a custom region
  • Concepts
    • Inversion of Control
      • Third party DI containers
    • Entities
      • Property System
    • Messaging and Message Broker
      • POCO messages
      • Standalone message handlers
    • Observers
      • PropertyObserver
      • MementoObserver
      • BrokerObserver
  • Memento
    • Change Tracking Service
      • MementoEntity and MementoEntityCollection
      • Handling change tracking:
        • Simple model
        • Collections
        • Complex objects graph
      • Atomic operations
      • Change Tracking Service API
      • Property Metadata for the ChangeTrackingService
      • Handling collection sync
      • Property State
  • Behaviors
    • DataGrid Behaviors
    • Password
    • Generic routed event handler to command behavior
    • Overlay adorner
      • Busy status manager
    • TextBox behaviors:
      • Command
      • Auto select
      • DisableUndoManager (.Net 3.5 only)
  • Markup Extensions
    • Editor binding
    • Auto Command binding
  • How to
    • Get the view of a given view model
    • Bi-directional communication between different windows/views
    • Handle the busy status during async/long running operations
    • Implement a customer improvement program
    • Manage focus
    • Create a splash screen
    • Access view model after view is closed
    • Intercept ViewModels before they are used
  • Upgrade guides
    • Radical Presentation 1.x to Radical 2.x for .NET Core
    • Radical 2.0.0 to Radical 2.1.0
Powered by GitBook
On this page
  1. How to

Access view model after view is closed

Good or not one of the scenario users need to support is to be able to use a ViewModel after the hosting View is closed. A typical sample is the following:

var view = viewResolver.GetView<MySampleView>();
view.ShowDialog();
var viewModel = conventions.GetViewDataContext(view, ViewDataContextSearchBehavior.LocalOnly);
//access some properties of the view model instance

The above snippet will fail with a null reference exception due to the fact that the view model instance will be null. The underlying reason is that as soon as the hosting view is closed:

  • the ViewModel is detached from its View;

  • Both components are released through the IReleaseComponents service, causing disposition of disposable instances if supported;

The above is by design. GetViewDataContext will return a null reference in the above scenario. A first approach, as a workaround, can be the following:

var view = viewResolver.GetView<MySampleView>();
var viewModel = conventions.GetViewDataContext( view, ViewDataContextSearchBehavior.LocalOnly );
view.ShowDialog();
//access here some properties of the view model instance

We simply retrieve a reference to the ViewModel before the view is closed and we use it later. What can happen is that accessing one of the ViewModel properties an ObjectDisposedException is thrown because the AbstractViewModel base class knows that the ViewModel instance was released and disposed.

The definitive solution is to override the automatic release behavior decorating the View class with the ViewManualReleaseAttribute and changing our code as follows:

var view = viewResolver.GetView<MySampleView>();
view.ShowDialog();
var viewModel = conventions.GetViewDataContext(view, ViewDataContextSearchBehavior.LocalOnly);
//access here some properties of the view model instance
conventions.ViewReleaseHandler(view, ViewReleaseBehavior.Force);

Once MySampleView is decorated with the ViewManualReleaseAttribute it won't be released anymore automatically and the ViewModel won't be detached, once we have finished using the ViewModel instance we ask to the conventions to force the release of the View and ViewModel using the ViewReleaseHandler convention and the Force enumeration value to override the default behavior.

PreviousCreate a splash screenNextIntercept ViewModels before they are used

Last updated 3 years ago