Skip to content
This repository was archived by the owner on May 1, 2024. It is now read-only.
This repository was archived by the owner on May 1, 2024. It is now read-only.

[Bug] Custom renderer's OnElementChanged is not called when renderer is disposed #8239

@GarryPSmith

Description

@GarryPSmith

When a custom renderer is automatically disposed it should trigger a call to OnElementChanged() with a reference to the associated element in the ElementChangedEventArgs under OldElement to allow for any clean-up (i.e. remove event handlers). This is not happening, OnElementChanged() is not called before the renderer is disposed.

Steps to Reproduce

  1. Create a custom control with an event in the shared project.
  2. Create a renderer in the Android project using a WebView as the native control.
  3. In OnElementChanged() when a NewElement is passed in the ElementChangedEventArgs add a handler to the control's event.
  4. When a OldElement is passed remove the event handler.
  5. Use the custom control in a ContentPage.
  6. Navigate away from the page and back to it.
  7. Invoke the event on the custom control.

Expected Behavior

The event handler is called correctly.

Actual Behavior

Everything works fine until the page is navigated away from and back to. Invoking the event then results in an ObjectDisposedException against the control.
Placing a breakpoint on the OnElementChanged method in the renderer and adding an overridden Dispose method with a breakpoint shows that when the page is navigated away from the Dispose method is called but OnElementChanged is not called. When the page is navigated back to a new renderer instance is created and the OnElementChanged method is called with a NewElement referenced in the ElementChangedEventArgs so an additional event handler is attached.
When the event is invoked the handler for the old renderer instance is still trying to be called which is now disposed.

Basic Information

  • Version with issue: Xamarin Forms 4.3.0.908675
  • Last known good version:
  • IDE: Visual Studio 2019
  • Platform Target Frameworks:
    • Android: API 27

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions