I have a custom calendar control for which there is a custom view mode. In this binding, we bind some events that do not turn off correctly, and therefore garbage collection is not completed. Below is our custom view binding. As you can see, the event is connected in the constructor, and the OnSelectedDate event is disabled (the user selects a date). Therefore, if you select a date, the event will be unleashed correctly and the garbage collected, but if you just return, the event is still connected and garbage collection is not performed. I was thinking of triggering an event with zero values and thus unlocking the event. But I think that for this you need to be in a smarter way.
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;
}
}
}
}
:)