Rename interface property in Metadata

2019-09-10 15:19发布

问题:

In Xamarin.Android Bindings, I have an interface wrapped from Java as follows:

// Metadata.xml XPath interface reference: path="/api/package[@name='com.buyandlarge.core.framework']/interface[@name='IView']"
[Register ("com/buyandlarge/core/framework/IView", "", "BuyAndLarge.Core.Framework.IViewInvoker")]
public partial interface IView : global::Core.Framework.IContextProvider, global::BuyAndLarge.Core.Framework.IHitTestable {


    global::Android.Views.ViewGroup.LayoutParams LayoutParams {
        // Metadata.xml XPath method reference: path="/api/package[@name='com.buyandlarge.core.framework']/interface[@name='IView']/method[@name='getLayoutParams' and count(parameter)=0]"
        [Register ("getLayoutParams", "()Landroid/view/ViewGroup$LayoutParams;", "GetGetLayoutParamsHandler:BuyAndLarge.Core.Framework.IViewInvoker, BuyAndLarge.Core")] get;
        // Metadata.xml XPath method reference: path="/api/package[@name='com.buyandlarge.core.framework']/interface[@name='IView']/method[@name='setLayoutParams' and count(parameter)=1 and parameter[1][@type='android.view.ViewGroup.LayoutParams']]"
        [Register ("setLayoutParams", "(Landroid/view/ViewGroup$LayoutParams;)V", "GetSetLayoutParams_Landroid_view_ViewGroup_LayoutParams_Handler:BuyAndLarge.Core.Framework.IViewInvoker, BuyAndLarge.Core")] set;
    }
}

Various classes which inherit global::Android.Views.View implement this interface. For some bizarre reason, Xamarin's implementation of Android.Views.View has the property defined as public virtual ViewGroup.LayoutParams LayoutParameters { get; set; } so I get hundreds of 'The type Foo does not implement IView.LayoutParams' errors in my code.

I'd like to use metadata.xml to rename IView.LayoutParams to IView.LayoutParameters, but not sure of the syntax.

I have tried this:

<attr path="/api/package[@name='com.buyandlarge.core.framework']/interface[@name='IView']/method[@name='getLayoutParams' and count(parameter)=0]" name="managedName">LayoutParameters</attr>
<attr path="/api/package[@name='com.buyandlarge.core.framework']/interface[@name='IView']/method[@name='setLayoutParams' and count(parameter)=1 and parameter[1][@type='android.view.ViewGroup.LayoutParams']]" name="managedName">LayoutParameters</attr>

but no success ... Any ideas?

回答1:

Hah, I figured this out! Go me!

Using the name="propertyName" you can change the generated property for an interface or class. The metadata becomes

  <!-- Rename IView.LayoutParams to LayoutParameters to be consistent with Android base classes -->
  <attr path="/api/package[@name='com.buyandlarge.core.framework']/interface[@name='IView']/method[@name='getLayoutParams' and count(parameter)=0]" 
        name="propertyName">LayoutParameters</attr>

  <attr path="/api/package[@name='com.buyandlarge.core.framework']/interface[@name='IView']/method[@name='setLayoutParams' and count(parameter)=1 and parameter[1][@type='android.view.ViewGroup.LayoutParams']]" 
        name="propertyName">LayoutParameters</attr>

The property is renamed, and the day has been saved ^_^