Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Android Mapping Java.Lang.Enum to C# Enum

In java I have the enum

package com.acme.common;

// In java world
public enum FooEnum {
    AValue,
    AnotherValue
}

This is used throughout the code in Java, e.g.

// In java world
public class Bar {
    void setFoo(FooEnum value) { 
       // ...  
    }
}

This is getting mapped to a class in C# which inherits Java.Lang.Enum as follows:

// Now in C# generated bindings 
// I want this to be a CLR Enum instead
public sealed partial class FooEnum : global::Java.Lang.Enum 
{ 
   // ... 
}   
// C# generated class uses Java.Lang.Enum type for getter and setter
// But I want FooEnum to be clr enum
public partial class Bar 
{        
   public FooEnum Foo { get; set; }
} 

I would like to map this to a C# enum instead using the EnumFields.xml, but no idea how to.

I googled Xamarin Android EnumFields and found a few results, and have read the Xamarin Android Binding walthrough but still not sure quite how to do this ...

like image 873
Dr. Andrew Burnett-Thompson Avatar asked Apr 28 '16 15:04

Dr. Andrew Burnett-Thompson


Video Answer


1 Answers

First you have define your Enums in EnumFields.xml how they will appear in C#

<mapping jni-class="com/acme/common/FooEnum " clr-enum-type="Com.Acme.Common.FooEnum">
    <field jni-name="AValue" clr-name="AValue" value="0" />
    <field jni-name="AnotherValue" clr-name="AnotherValue" value="1" />
</mapping>

Then you need to edit EnumMethods.xml for all Methods that consume this Enum

<mapping jni-class="com/skobbler/ngx/map/realreach/SKRealReachSettings">
    <method jni-name="getFoo" parameter="return" clr-enum-type="Com.Acme.Common.FooEnum" />
    <method jni-name="setFoo" parameter="measurementUnit" clr-enum-type="Com.Acme.Common.FooEnum" />
</mapping>
like image 76
Matt Avatar answered Oct 08 '22 06:10

Matt