使用 MvvmCross 和 FFImageLoading 的 Xamarin Android 可见性绑定问题

问题描述

为了在我的应用程序(使用 Mvx 和 FFimageLoading 的 Xamarin AndroidX)中​​创建一个动画加载器 UI 控件,我创建了一个继承自 MvxCachedImageView自定义控件。 然后我按如下方式应用我的动画:

public class LoaderView : MvxCachedImageView
    {
        protected LoaderView(IntPtr javaReference,JniHandleOwnership transfer)
            : base(javaReference,transfer)
        {
            Create();
        }

        public LoaderView(Context context)
            : base(context)
        {
            Create();
        }

        public LoaderView(Context context,IAttributeSet attrs)
            : base(context,attrs)
        {
            Create();
        }

        private void Create()
        {
            if (Context == null)
                return;

            ImageService.Instance.LoadCompiledResource("loader_indicator")
                .Into(this);

            ApplyRotation();

            StartAnimation(Animation);
        }
        
        private void ApplyRotation()
        {
            var rotation = new RotateAnimation(
                0,360,Dimension.RelativetoSelf,0.5f,0.5f)
            {
                Duration = 1200,Interpolator = new DecelerateInterpolator(1.25f),RepeatCount = Animation.Infinite
            };

            Animation = rotation;
        }
    }

注意loader_indicator一个 gif 文件

此控件一直正常工作,直到我尝试将其可见性属性绑定到“经典”IsLoading Baseviewmodel 的属性,如下所示:

var set = CreateBindingSet();
set.Bind(loader).For(v => v.Visibility).To(vm => vm.IsLoading)
                .WithConversion<MvxVisibilityValueConverter>();
set.Apply();

按上述方式执行应用程序,Visibility 绑定和 gif 动画 随机工作:有时会,有时不会;有时,RotateAnimation 正在工作,但针对父元素而不是它本身 - 在糟糕的情况下,您的图标会进入页面..

做不同的测试我明白,如果我删除 ApplyRotationStartAnimation,控件会正确显示binded

这似乎是某个地方的线程问题。有没有人见过并(可能)解决了这个问题?

编辑:Cheesebaron anwser 给了我一个正确的提示,所以我改变了 LoaderView 实现如下,它实际上解决了这个问题:

public class LoaderView : MvxCachedImageView
{
    private Animation _animation;

    protected LoaderView(IntPtr javaReference,JniHandleOwnership transfer)
        : base(javaReference,transfer)
    { }

    public LoaderView(Context context)
        : base(context)
    { }

    public LoaderView(Context context,IAttributeSet attrs)
        : base(context,attrs)
    { }

    protected override void OnVisibilityChanged(View changedView,ViewStates visibility)
    {
        base.OnVisibilityChanged(changedView,visibility);

        if (visibility == ViewStates.Visible)
            StartAnimation(_animation);
        else
            Clearanimation();
    }

    protected override void OnAttachedToWindow()
    {
        base.OnAttachedToWindow();
        Create();
    }

    private void Create()
    {
        if (Context == null)
            return;

        ImageService.Instance.LoadCompiledResource("loader_indicator")
            .Into(this);

        _animation = ApplyRotation();
    }

    private static RotateAnimation ApplyRotation() =>
        new RotateAnimation(
            0,0.5f)
        {
            Duration = 1200,RepeatCount = Animation.Infinite
        };

}

解决方法

您可以尝试将绑定表达式更改为:

set.Bind(loader).For(v => v.BindVisible()).To(vm => vm.IsLoading);

但是,您是在元素实例化时应用动画,而不是在设置可见性时应用动画。所以动画可能正在运行,而元素不可见,反之亦然。您可能希望在更合适的时间点开始动画。