Mvvmcross bind to Visibility with autolayout and fully hide view

风格不统一 提交于 2019-12-25 04:22:54

问题


Currently binding to "Visibility" sets Hidden=true. How would you create a generic Visibility binding which also changes a constraint: sets the view height to 0 ?


回答1:


For a tutorial on creating bindings, see the N=28 video on http://mvvmcross.blogspot.com/

To replace the existing visibility binding, simply create your own class based on https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Binding.Touch/Target/MvxUIViewVisibleTargetBinding.cs

public class MyUIViewVisibleTargetBinding : MvxBaseUIViewVisibleTargetBinding
{
    public MyUIViewVisibleTargetBinding(UIView target)
        : base(target)
    {
    }

    protected override void SetValueImpl(object target, object value)
    {
        var view = View;
        if (view == null)
            return;

        var visible = value.ConvertToBoolean();
        // your code here
        // - in place of or in addition to:
        // view.Hidden = !visible;
    }
}

And register this as the last step in Setup using:

protected override void InitializeLastChance()
{
     base.InitializeLastChance();

     var registry = Mvx.Resolve<IMvxTargetBindingFactoryRegistry>();
     registry.RegisterCustomBindingFactory<UIView>("Visible",
                                                    view =>
                                                    new MyUIViewVisibleTargetBinding(view));

}

For more on replacing existing bindings, see MVVMCross Binding decimal to UITextField removes decimal point


Note that if you want to replace all Visible bindings, then you might want to replace all of Visible, Visibility and Hidden - see registrations in https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Binding.Touch/MvxTouchBindingBuilder.cs#L42



来源:https://stackoverflow.com/questions/22622836/mvvmcross-bind-to-visibility-with-autolayout-and-fully-hide-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!