Xamarin.Android绑定YandexMapKit遇CS0542错误的解决咨询
That CS0542 error pops up because C# doesn't allow a member to share the exact same name as its containing class. In your generated binding code, the event OverlayItem is defined inside a class also named OverlayItem—that's the root of the naming conflict.
Why This Happens
Xamarin's binding generator automatically converts Java listener patterns into C# events. For the Java method setOverlayItemListener, it generates an event named OverlayItem (by stripping the set and Listener suffixes). Unfortunately, this event ends up being placed inside the OverlayItem class, triggering the C# naming rule violation.
Step-by-Step Fix
Here's how to resolve this by modifying the binding metadata to rename the conflicting event:
Open your Xamarin.Android Binding Library project
If you don't already have one, create a
Transformsfolder in the project root, then add aMetadata.xmlfile inside it.Add the following XML to
Metadata.xmlto rename the event:<metadata> <!-- Rename the conflicting OverlayItem event in the OverlayItem class --> <attr path="/api/package[@name='ru.yandex.yandexmapkit.overlay']/class[@name='OverlayItem']/event[@name='OverlayItem']" name="name">OverlayItemClicked</attr> </metadata>This tells the binding generator to use
OverlayItemClickedinstead ofOverlayItemfor the event name, completely avoiding the naming clash.Rebuild your binding library—the CS0542 error should disappear immediately.
Using the Fixed Event
After applying the fix, you can subscribe to the renamed event in your code like this:
var overlayItem = new OverlayItem(...); overlayItem.OverlayItemClicked += (sender, args) => { // Handle the overlay item click here var clickedItem = args.OverlayItem; // Add your custom click logic here };
内容的提问来源于stack exchange,提问作者ibogolyubskiy

