Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ObjectAnimator Proxy to Animate TopMargin can't find setting/getter

We are trying to use an objectanimator proxy to animate the TopMargin property in Android (Xamarin).

However, we get this error:

[PropertyValuesHolder] Couldn't find setter/getter for property TopMargin with value type float

Note: we tried TopMargin, topMargin, GetTopMargin, getTopMargin, etc., thinking that it might be an issue of casing conversion betwen Java and C#, but it doesn't look to be the case.

Our code in the Activity starting the animation:

translation = new int[] {0, 300};
var anim2 = ObjectAnimator.OfInt( new MarginProxyAnimator(myview), "TopMargin",translation);
anim2.SetDuration(500);
anim2.Start(); 

Our proxy class:

public class MarginProxyAnimator : Java.Lang.Object 
{
///... other code...
    public int getTopMargin() {
    var lp = (ViewGroup.MarginLayoutParams)mView.LayoutParameters;
    return lp.TopMargin;
    }

    public void setTopMargin(int margin) {
    var lp = (ViewGroup.MarginLayoutParams)mView.LayoutParameters;
        lp.SetMargins(lp.LeftMargin, margin, lp.RightMargin, lp.BottomMargin);
    mView.RequestLayout();
    }
}

Any advise? a pointer to a working Xamarin sample using a proxy would be helpful.

thanks.

like image 618
rufo Avatar asked Nov 29 '13 16:11

rufo


2 Answers

You need to add the [Export] attribute to your getTopMargin and setTopMargin methods. E.G.

[Export]
public int getTopMargin()
{
  var lp = (ViewGroup.MarginLayoutParams)mView.LayoutParameters;
  return lp.TopMargin;
}

[Export]
public void setTopMargin(int margin)
{
  var lp = (ViewGroup.MarginLayoutParams)mView.LayoutParameters;
  lp.SetMargins(lp.LeftMargin, margin, lp.RightMargin, lp.BottomMargin);
  mView.RequestLayout();
}

The [Export] attribute also requires that you add a reference to the Mono.Android.Export assembly.

Documentation:

http://androidapi.xamarin.com/?link=T%3aJava.Interop.ExportAttribute

like image 161
wdavo Avatar answered Nov 06 '22 03:11

wdavo


An alternative to @wdavo's answer you can also use [Export] attribute on C# Properties. So the code in his answer could be made into a property like so:

public int TopMargin
{
    [Export("getTopMargin")]
    get { 
        var lp = (ViewGroup.MarginLayoutParams)mView.LayoutParameters;
        return lp.TopMargin;
    }

    [Export("setTopMargin")]
    set {
        var lp = (ViewGroup.MarginLayoutParams)mView.LayoutParameters;
        lp.SetMargins(lp.LeftMargin, value, lp.RightMargin, lp.BottomMargin);
        mView.RequestLayout();
    }
}
like image 2
Cheesebaron Avatar answered Nov 06 '22 04:11

Cheesebaron