覆盖UIActivityIndi​​catorView.Hidden绑定的UIActivityIndi​​catorView.StartAnimating [英] UIActivityIndicatorView.StartAnimating overriding UIActivityIndicatorView.Hidden binding

查看:68
本文介绍了覆盖UIActivityIndi​​catorView.Hidden绑定的UIActivityIndi​​catorView.StartAnimating的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Controller.ViewDidLoad中创建UIActivityIndi​​catorView

I am creating a UIActivityIndicatorView in my Controller.ViewDidLoad

UIActivityIndicatorView spinner = new UIActivityIndicatorView();
spinner.StartAnimating();
spinner.Hidden = true;
this.Add(spinner);

然后我将其与MVVMCross绑定

Then I am binding it with MVVMCross

var set = this.CreateBindingSet<TipView, TipViewModel>();
set.Bind(spinner).For(v => v.Hidden).To(vm => vm.IsBusy).WithConversion("Inverse");

最初加载视图时,UIActivityIndi​​catorView旋转 可见.这是不正确的,因为在ViewModel的Init()中将IsBusy属性显式设置为False.我可以看到这种情况的发生,也可以看到Converter将值取反.

When the View initially loads the UIActivityIndicatorView is spinning and visible. This is incorrect as the IsBusy property is being explicitly set to False in the ViewModel's Init(). I can see this happening and I can see the Converter invert the value.

我知道绑定已正确连接,因为如果我触发更新IsBusy属性的命令,则指示符将按我期望的方式显示和隐藏.只是初始状态不正确.

I know the binding is properly connected because if I fire a command that updates the IsBusy property the Indicator is shown and hidden as I would expect. It is just the initial state that is incorrect.

StartAnimating 方法似乎导致Hidden标志被覆盖.如果我不打电话给StartAnimating,指示器会隐藏并按预期显示.当然,这意味着我没有动画 指示器.

The StartAnimating method seems to cause the Hidden flag to be overridden. If I do not call StartAnimating the Indicator hides and shows as expected. Of course that means I have a non animating Indicator.

我可以得到对VM的WeakReference,听PropertyChanged并调用StartAnimating,但这似乎有点垃圾.

I could get a WeakReference to the VM, listen to PropertyChanged and call StartAnimating but that seems a bit rubbish.

有人有更好的主意吗?

推荐答案

阅读@slodge的回复后,我走上了弱事件监听器的道路,并在视图中运行了代码Hide和StartAnimating.复制并粘贴该方法3次后,我意识到必须进行更改,因此我实现了他的第4条建议并编写了自定义绑定. FWIW这是自定义绑定

After reading @slodge's reply I went down the road of Weak Event Listener and ran the code to Hide and StartAnimating in the View. Having copy and pasted that approach 3 times I realised something had to change so I implemented his 4th suggestion and wrote a Custom Binding. FWIW here is that custom binding

/// <summary>
/// Custom Binding for UIActivityIndicator Hidden. 
/// This binding will ensure the indicator animates when shown and stops when hidden
/// </summary>
public class ActivityIndicatorViewHiddenTargetBinding : MvxConvertingTargetBinding
{
    /// <summary>
    /// Initializes a new instance of the <see cref="ActivityIndicatorViewHiddenTargetBinding"/> class.
    /// </summary>
    /// <param name="target">The target.</param>
    public ActivityIndicatorViewHiddenTargetBinding(UIActivityIndicatorView target)
        : base(target)
    {
        if (target == null)
        {
            MvxBindingTrace.Trace(
                                MvxTraceLevel.Error,
                                "Error - UIActivityIndicatorView is null in ActivityIndicatorViewHiddenTargetBinding");
        }
    }

    /// <summary>
    /// Gets the default binding mode.
    /// </summary>
    /// <value>
    /// The default mode.
    /// </value>
    public override MvxBindingMode DefaultMode
    {
        get { return MvxBindingMode.OneWay; }
    }

    /// <summary>
    /// Gets the type of the target.
    /// </summary>
    /// <value>
    /// The type of the target.
    /// </value>
    public override System.Type TargetType
    {
        get { return typeof(bool); }
    }

    /// <summary>
    /// Gets the view.
    /// </summary>
    /// <value>
    /// The view.
    /// </value>
    protected UIActivityIndicatorView View
    {
        get { return Target as UIActivityIndicatorView; }
    }

    /// <summary>
    /// Sets the value.
    /// </summary>
    /// <param name="target">The target.</param>
    /// <param name="value">The value.</param>
    protected override void SetValueImpl(object target, object value)
    {
        var view = (UIActivityIndicatorView)target;
        if (view == null)
        {
            return;
        }

        view.Hidden = (bool)value;

        if (view.Hidden)
        {
            view.StopAnimating();
        }
        else
        {
            view.StartAnimating();
        }
    }
}

这篇关于覆盖UIActivityIndi​​catorView.Hidden绑定的UIActivityIndi​​catorView.StartAnimating的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆