ObjectAnimator Proxy to Animate TopMargin can'

2020-03-24 03:34发布

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.

2条回答
Root(大扎)
2楼-- · 2020-03-24 04:05

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

查看更多
手持菜刀,她持情操
3楼-- · 2020-03-24 04:18

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();
    }
}
查看更多
登录 后发表回答