0

Within my DataTemplate (written in C#), I have this code:

var plusMinusGrid = new Grid
{
    Children =
    {
         _minusFrame.Column(0).Bind(IsVisibleProperty, nameof(DeckRow.FRMIsVisible), source: this),
         _plusFrame.Column(0).Bind(IsVisibleProperty,  nameof(DeckRow.FRPIsVisible), source: this)
    },
};

var plusMinusTapGesture = new TapGestureRecognizer();
plusMinusTapGesture.Tapped += PlusMinusTap;
plusMinusGrid.GestureRecognizers.Add(plusMinusTapGesture);

So I am adding the tap event to a part of each row.

My question is, will this be an issue as a memory leak and if that's the case is there a way that I can deal with this.

2
  • I have used a similar pattern, each row of a CollectionView has a slider. Each row gets an event subscription. Like you I have read that you should remove this upon ending with the minus equal (-=). I added that code. But I have t say that the life span of an app is not that long, and perhaps you'd never need it.... however if you can, you should remove it, just seems to make sense.
    – drHodge
    Commented Mar 9, 2021 at 5:13
  • I'm not really sure how to remove this upon ending however. Hopefully we can get some advice here. If you are also interested, can you upvote and the question may get more attention. Thank you
    – Richard A
    Commented Mar 9, 2021 at 6:06

1 Answer 1

0

Here is what I have. Override the Disappearing method add a call to a CleanUp() that you write yourself. As I am doing the += on in the view model I add the -= there also. This is annoying as I also found that I needed to set itemsource to null, but itemsource is in the view. Since Disappearing is part of the view, I call the VM's CleanUp() method!

As my page is used modally this works fine. If your page is not modal, and you refresh the grid, make sure you do the -= on existing items before you repopulate.

View page :

    public void CleanUp()
    {
        this.athleteDetailVM.CleanUp();
        this.collectionV.ItemsSource = null;
    }

VM Implementation:

    public void CleanUp()
    {
        foreach (CheckedItem<EventResult> item in eventResults)
        {
            item.PropertyChanged -= null;
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.