如何取消订阅MvvmCross中的WeakSubscribe

use*_*631 1 c# wpf events xaml mvvmcross

_parameter.WeakSubscribe(() => _parameter.Value, HandleValueChanged);
Run Code Online (Sandbox Code Playgroud)

我像上面那样使用WeakSubscribe.

我的方案是当值发生变化时,系统将添加一个新参数,当前将取消订阅该事件.

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

Stu*_*art 6

如果您使用以下内容订阅:

 _token = thing.WeakSubscribe(() => parameter.Value, HandleValueChanged);
Run Code Online (Sandbox Code Playgroud)

那么您可以使用以下方式取消订阅:

 _token.Dispose();
 _token = null;
Run Code Online (Sandbox Code Playgroud)

这将调用https://github.com/MvvmCross/MvvmCross/blob/v3.1/CrossCore/Cirrious.CrossCore/WeakSubscription/MvxWeakEventSubscription.cs#L91中Dispose代码:

    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;
        }
    }
Run Code Online (Sandbox Code Playgroud)