Xamarin.Forms 中的对象处理 [英] Object disposing in Xamarin.Forms

查看:27
本文介绍了Xamarin.Forms 中的对象处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找在 Xamarin Forms 应用程序中处理对象的正确方法.目前我正在使用 XAML 和 MVVM 编码风格.然后从我的视图模型中,我通过内置服务定位器(DependencyService).理想情况下,我应该能够从我的视图模型中对对象调用 Dispose(),但其他解决方案如附加到 ContentPage.OnDisappearingNavigationPage.Popped 是可行的.

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.

推荐答案

几周前我有几乎相同的要求.我想确保当页面关闭时,我的视图模型中的事件订阅将被取消订阅.经过大量研究,我得出的结论是,最简单的解决方案是使用 ContentPage.OnDisappearing 方法.

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.

正如您所指出的,您要处理的对象在您的 ViewModel 中,因此您需要一些基础设施来确保您的 ViewModel 在它消失时得到通知.为此,我定义了视图模型的基本实现,该实现具有两个关键方法 OnAppearing 和 OnDisappearing(请注意,这是一个类而不是接口,因为我还有其他基本功能,例如 IPropertyNotify 实现 - 此处未显示).

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. 
    }
}

然后我将 ContentPage 子类化并覆盖 OnAppearing 和 OnDisappearing 方法,然后使用它们来通知我的视图模型.

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();
    }
}

然后,当您实现一个页面时,只需确保它的类型为 PageBase:

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">

然后在您的 ViewModel 中,您可以覆盖 OnDisappearing 方法并处理您的对象:

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
    }
}

需要注意的一件事 - 如果您使用堆栈导航,当您将另一个页面堆叠在当前页面的顶部时会调用 OnDisappearing 方法(毕竟您的页面暂时消失了).因此,您将需要满足这一点,并且在这种情况下可能不会处理您的对象.但是,如果您没有在页面顶部堆叠任何内容,则无需担心.就我而言,它只是事件订阅,所以我在 OnAppearing 中附加了事件处理程序,并在 OnDisappearing 上分离了它们.

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.

希望能帮到你!

这篇关于Xamarin.Forms 中的对象处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆