如何在MvvmCross中取消订阅WeakSubscribe [英] how to unsubscribe from WeakSubscribe in the MvvmCross

查看:80
本文介绍了如何在MvvmCross中取消订阅WeakSubscribe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

_parameter.WeakSubscribe(() => _parameter.Value, HandleValueChanged);

我像上面一样使用WeakSubscribe.

I use the WeakSubscribe like the above.

我的情况是,值更改后,系统将添加一个新参数,而当前参数将退订该事件.

My scenario is when the value has changed the system will add a new parameter and the current will unsubscribe from the event.

我发现了这个问题,但是它不起作用.

I found this question but it doesn't work.

推荐答案

如果您使用以下方式订阅:

If you subscribe using something like:

 _token = thing.WeakSubscribe(() => parameter.Value, HandleValueChanged);

然后您可以使用以下方式退订:

then you can unsubscribe using:

 _token.Dispose();
 _token = null;

这将在Dispose 代码.#L91"rel =" noreferrer> https://github.com/MvvmCross/MvvmCross/blob/v3.1/CrossCore/Cirrious.CrossCore/WeakSubscription/MvxWeakEventSubscription.cs#L91 :

This calls the Dispose code in https://github.com/MvvmCross/MvvmCross/blob/v3.1/CrossCore/Cirrious.CrossCore/WeakSubscription/MvxWeakEventSubscription.cs#L91 :

    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            RemoveEventHandler();
        }
    }

    private void RemoveEventHandler()
    {
        if (!_subscribed)
            return;

        var source = (TSource) _sourceReference.Target;
        if (source != null)
        {
            _sourceEventInfo.GetRemoveMethod().Invoke(source, new object[] {CreateEventHandler()});
            _subscribed = false;
        }
    }

这篇关于如何在MvvmCross中取消订阅WeakSubscribe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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