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.
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
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();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With