在单点触摸和mvvmcross中使用自定义视图绑定的垃圾收集问题

在单点触摸和mvvmcross中使用自定义视图绑定的垃圾收集问题

本文介绍了在单点触摸和mvvmcross中使用自定义视图绑定的垃圾收集问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义日历控件,其中有一个自定义的视图绑定。在这个视图绑定中,我们挂钩了一些未解耦的事件,因此垃圾收集未完成。以下是我们的自定义视图绑定。正如你所看到的,事件被连接到了构造函数中,并且在OnSelectedDate事件中被解耦(用户选择了一个日期)。因此,如果您选择一个日期,则事件的分离正确并收集垃圾,但如果您刚刚返回,事件仍会挂钩并且不会执行垃圾收集。我想过用空值触发事件,从而使事件变得无法解决。但我认为必须有一些更聪明的方法来实现这一点。

I have a custom calendar control for which there is an custom viewbinding. In this viewbinding we hook up some events which are not decoupled correct and therefor is the garbage collecting not completed. In the following is our custom view binding. As you can see the event is hooked up in the constructor and decoupled in the OnSelectedDate event is triggered(the user selects an date). Therefore if you choose a date the event is decouple correct and garbage collected but if you just go back, the event is still hooked up and no garbage collecting is performed. I thought about trigger the event with null values and and thereby decoulpe the event. But I think there must be some more clever way to achieve this.

namespace CmsApp.Core.Binders
{
    public class CalendarViewBinding:MvxBaseTargetBinding
    {
        private CalendarView _calendarView;
        private DateTime _currentValue;
         public CalendarViewBinding(CalendarView calendarView)
        {
            _calendarView = calendarView;
            _calendarView.OnDateSelected+=OnDateSelected;
        }

         protected override void Dispose(bool isDisposing)
         {
             if(_calendarView!=null)
             {
                 _calendarView.OnDateSelected -= OnDateSelected;
                 _calendarView = null;
             }
             base.Dispose(isDisposing);

         }

        private void OnDateSelected(object sender, SelectedDateEventArgs args)
        {
            _currentValue = args.SelectedDate;
            this.FireValueChanged(_currentValue);
            _calendarView.OnDateSelected -= OnDateSelected;
        }

        public override void SetValue(object value)
        {
            var date = (DateTime)value;
            _currentValue = date;
            _calendarView.SelectedDate = _currentValue;

        }

        public override Type TargetType
        {
            get
            {
                return typeof(DateTime);
            }
        }

        public override MvxBindingMode DefaultMode
        {
            get
            {
                return MvxBindingMode.TwoWay;
            }
        }
    }
}

帮助表示赞赏:)

Any help is appreciated :)

推荐答案

在我看来,你的绑定几乎是正确的。

It looks to me like your binding is almost correct.

我能看到的唯一问题是,它经常取消订阅活动 - 您无法调用 _calendarView.OnDateSelected - = OnDateSelected; 两次 - 但我不认为这是你看到的问题。

The only issue I can see is that it unsubscribes from the event too often - you can't call _calendarView.OnDateSelected -= OnDateSelected; twice - but I don't think this is the problem you are seeing.

我目前猜测问题不在你使用的代码中:

I currently would guess that the problem is not in the code you are using:


  • 或者您正在使用的底层框架中的绑定代码存在错误

  • 或其他内容中的错误/问题您使用此绑定的方式

  • 或您的内存泄漏与此绑定无关

从这里发布的有限代码来测试它并不容易,但如果你能制作一个简单的应用程序来重现你所看到的泄漏G。分享这一点,你可能会得到更多的反馈。

It's not easy to test this from the limited code posted here, but it would be simpler if you could produce a simple app that reproduces the leak you are seeing. Share that and you might be able to get more feedback.

如果你相信我的猜测是错误的,那么唯一的我可以建议你在绑定中切换到WeakReferences - 但是这种感觉就像是一种贴纸,而不是一种治疗方式。

If you believe my guesses are wrong, then the only thing I can suggest is that you switch to WeakReferences inside your binding - but this feels like a sticking plaster rather than a cure.

只需添加一个链接到

这篇关于在单点触摸和mvvmcross中使用自定义视图绑定的垃圾收集问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 22:39