【问题标题】:Animation not working in Xamarin动画在 Xamarin 中不起作用
【发布时间】:2014-09-25 00:12:42
【问题描述】:

我在我的原生平台 (android) 中使用了下面的代码 sn-p 来为值设置动画,它在 android 中运行良好。

 public void setValue(double value) {
        if(this.renderer != null)
        {
            ObjectAnimator anim= ObjectAnimator.ofFloat(this.renderer,"Value",(float)this.value,(float)value);
            anim.setDuration(1500);
            anim.start();
        }

        this.value = value;
    }

虽然尝试从Xamarin 为Value 属性(android)设置newValue,但它设置了newvalue,但动画在xamarin 中不起作用。

我错过了什么吗?

Android 运行时需要调用托管代码的任何时候都需要 Android 可调用包装器 (ACW),因为无法在运行时向 Dalvik 注册类。reference link

所以我尝试早点声明ObjectAnimator anim 并为下面的值设置动画,

public void setValue(double value) {
if(this.renderer != null)
{
    anim= ObjectAnimator.ofFloat(this.renderer,"Value",(float)this.value,(float)value);
    anim.setDuration(1500);
    anim.start();
}

this.value = value;

}

但是上面提到的问题没有解决。

【问题讨论】:

  • 也许使用正确的方法名称? Xamarin 不使用小驼峰式大小写。
  • @Cheesebaron,上面提到的代码在Android(native)而不是Xamarin

标签: android xamarin xamarin.android xamarin.forms


【解决方案1】:

ObjectAnimator 正在尝试在您的 Renderer 类上找到方法 setValue(float)。

您需要添加 [Export] 属性以使其对 Java 端可见。

public class Renderer: Java.Lang.Object
{
    private float _value;
    [Export]
    public float getValue()
    {
        return _value;
    }

    [Export]
    public void setValue(float value)
    {
        _value = value;
        Log.Debug("TestRenderer", "Value: {0}", value);
    }
}

现在你可以像这样使用ObjectAnimator了:

var renderer = new Renderer();
var anim = ObjectAnimator.OfFloat(renderer, "value", 0f, 1f);
anim.SetDuration(2000);
anim.Start();

请参阅ObjectAnimator Proxy to Animate TopMargin can't find setting/getter 了解更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-18
    • 2012-08-06
    • 2015-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多