Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object disposing in Xamarin.Forms

I'm looking for the right way to dispose objects in a Xamarin Forms application. Currently i'm using XAML and MVVM coding style. Then from my view model i get a reference to a disposable object through the builtin service locator (DependencyService). Ideally i should be able to call Dispose() on the objects from my view model, but other solutions like attaching to ContentPage.OnDisappearing and NavigationPage.Popped could be feasible.

like image 217
kalitsov Avatar asked Apr 19 '17 11:04

kalitsov


Video Answer


1 Answers

I had pretty much the same requirement a couple of weeks ago. I wanted to make sure that event subscriptions in my view models would be unsubscribed when the page is closed. After a lot of research my conclusion was that the simplest solution was to use the ContentPage.OnDisappearing method.

As you pointed out the object you want to dispose is in your ViewModel, so you need a little bit of infrastructure to make sure your ViewModel is informed when the it's disappearing. To do that I defined a base implementation of my view model that had two key methods OnAppearing and OnDisappearing (note this was a class rather than an interface because I have other base functionality such as IPropertyNotify implementation - not shown here).

public class ViewModelBase
{
    /// <summary>
    /// Called when page is appearing.
    /// </summary>
    public virtual void OnAppearing()
    {
        // No default implementation. 
    }

    /// <summary>
    /// Called when the view model is disappearing. View Model clean-up should be performed here.
    /// </summary>
    public virtual void OnDisappearing()
    {
        // No default implementation. 
    }
}

Then I subsclassed ContentPage and override the OnAppearing and OnDisappearing methods and then use them to notify my view model.

public class PageBase : ContentPage
{
    /// <summary>
    /// Performs page clean-up.
    /// </summary>
    protected override void OnDisappearing()
    {
        base.OnDisappearing();

        var viewModel = BindingContext as ViewModelBase;

        // Inform the view model that it is disappearing so that it can remove event handlers
        // and perform any other clean-up required..
        viewModel?.OnDisappearing();
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();

        // Inform the view model that it is appearing
        var viewModel = BindingContext as ViewModelBase;

        // Inform the view model that it is appearing.
        viewModel?.OnAppearing();
    }
}

Then when you implement a page just make sure that it is of type PageBase:

<?xml version="1.0" encoding="utf-8" ?>
<pages:PageBase xmlns="http://xamarin.com/schemas/2014/forms"
          xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
          xmlns:controls="clr-namespace:Forms.App.Controls;assembly=Forms.App"
          xmlns:converters="clr-namespace:Forms.App.Converters;assembly=Forms.App"
          xmlns:pages="clr-namespace:Forms.App.Pages;assembly=Forms.App"
          x:Class="Forms.App.Pages.LogonPage"
          NavigationPage.HasNavigationBar="False"
          Title="Logon">

And in your ViewModel you can then override your OnDisappearing method and dispose your objects:

public class FormViewModel : ViewModelBase
{
    public override void OnDisappearing()
    {
        base.OnDisappearing();

        // Dispose whatever objects are neede here
    }
}

Just one thing to watch out for - if you're using stack navigation the OnDisappearing method gets called when you stack another page on-top of your current page (your page is disappearing temporarily after all). So you will need to cater for this and probably not dispose your object in that case. However if you're not stacking anything on-top of your page there is nothing to worry about. In my case it was just event subscriptions so I attached the event handlers in the OnAppearing and detached them on the OnDisappearing.

I hope that helps you out!

like image 120
Caleb Seadon Avatar answered Sep 23 '22 20:09

Caleb Seadon