Mahendra
Mahendra

Reputation: 165

Xamarin.Forms : When should I unhook the control's events in ContentPage

In our xamarin.forms application we are creating 10 text boxes inside a ContentPage. Then we are hooking TextChanged events of these textboxes.

Sample Code

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class PinView : ContentPage
{
    protected override void OnBindingContextChanged()
    {
        base.OnBindingContextChanged();
        for (int i = 0; i < 10; i++)
        {
        Entry entry = new Entry();
        entry.TextChanged += OnEntryTextChanged;
        }
    }

    private void OnEntryTextChanged(object sender, TextChangedEventArgs e)
    {
    //Some Code
    }
}

Now My question is should I care about unhooking these events? if 'Yes' then please let me know how and when.

Upvotes: 0

Views: 60

Answers (1)

Michał Żołnieruk
Michał Żołnieruk

Reputation: 2105

If you would subscribe using XAML, then for sure you don't need to worry about it - events are unsubscribed automatically. If you subscribe using code, then it looks like it should be covered by the framework (the page gets disposed), but it's advised to do it anyway. It may be a good idea to ask the xamarin forms team on github.

Upvotes: 1

Related Questions