java.interop icon indicating copy to clipboard operation
java.interop copied to clipboard

Bound Add/Set listener events get duplicated

Open jpobst opened this issue 5 years ago • 3 comments

Workaround: https://github.com/xamarin/java.interop/wiki/Duplicate-Events

Given a class which implements both an addXXXListener and a setXXXListener for the same listener, we will generate each event twice when doing the Event implementation for part.

Example, for AndroidX.ConstraintLayout 2.0.1+:

#region "Event implementation for AndroidX.ConstraintLayout.Motion.Widget.MotionLayout.ITransitionListener"
public event EventHandler<global::AndroidX.ConstraintLayout.Motion.Widget.MotionLayout.TransitionChangeEventArgs> TransitionChange {
	add {
		global::Java.Interop.EventHelper.AddEventHandler<global::AndroidX.ConstraintLayout.Motion.Widget.MotionLayout.ITransitionListener, global::AndroidX.ConstraintLayout.Motion.Widget.MotionLayout.ITransitionListenerImplementor>(
		ref weak_implementor_AddTransitionListener,
		__CreateITransitionListenerImplementor,
		AddTransitionListener,
		__h => __h.OnTransitionChangeHandler += value);
	}
	remove {
		global::Java.Interop.EventHelper.RemoveEventHandler<global::AndroidX.ConstraintLayout.Motion.Widget.MotionLayout.ITransitionListener, global::AndroidX.ConstraintLayout.Motion.Widget.MotionLayout.ITransitionListenerImplementor>(
		ref weak_implementor_AddTransitionListener,
		global::AndroidX.ConstraintLayout.Motion.Widget.MotionLayout.ITransitionListenerImplementor.__IsEmpty,
		__v => RemoveTransitionListener (__v),
		__h => __h.OnTransitionChangeHandler -= value);
	}
}

public event EventHandler<global::AndroidX.ConstraintLayout.Motion.Widget.MotionLayout.TransitionChangeEventArgs> TransitionChange {
	add {
		global::Java.Interop.EventHelper.AddEventHandler<global::AndroidX.ConstraintLayout.Motion.Widget.MotionLayout.ITransitionListener, global::AndroidX.ConstraintLayout.Motion.Widget.MotionLayout.ITransitionListenerImplementor>(
				ref weak_implementor_SetTransitionListener,
				__CreateITransitionListenerImplementor,
				SetTransitionListener,
				__h => __h.OnTransitionChangeHandler += value);
	}
	remove {
		global::Java.Interop.EventHelper.RemoveEventHandler<global::AndroidX.ConstraintLayout.Motion.Widget.MotionLayout.ITransitionListener, global::AndroidX.ConstraintLayout.Motion.Widget.MotionLayout.ITransitionListenerImplementor>(
				ref weak_implementor_SetTransitionListener,
				global::AndroidX.ConstraintLayout.Motion.Widget.MotionLayout.ITransitionListenerImplementor.__IsEmpty,
				__v => SetTransitionListener (null),
				__h => __h.OnTransitionChangeHandler -= value);
	}
}

Note how one is for AddTransitionListener and one is for SetTransitionListener.

This causes:

Error CS0102 The type 'MotionLayout' already contains a definition for 'TransitionChange'

This is caused in: SourceWriterExtensions.AddInterfaceListenerEventsAndProperties.

jpobst avatar Sep 08 '20 15:09 jpobst

Is there a way to change this, or a work-around of any kind? This occurs when I try to bind the UseButton Android-SDK library

ARLeandroC avatar Oct 05 '21 18:10 ARLeandroC

Yes! This issue should reference the documentation I wrote for fixing it via metadata: https://github.com/xamarin/java.interop/wiki/Duplicate-Events

jpobst avatar Oct 05 '21 18:10 jpobst

Thanks! I see now that it's not exactly my case, but it's similar: A Java class has two attributes which implement a nested interface, and the main C# class has created event handlers for each of the methods in the interface, a copy for each variable. I can't use this solution because the duplicated methods have the same signature, they just use a different WeakReference fields.

ARLeandroC avatar Oct 06 '21 00:10 ARLeandroC