Mvvmcross使用autolayout绑定到Visibility并完全隐藏视图

时间:2014-03-24 23:46:12

标签: xamarin.ios mvvmcross

目前绑定到“可见性”设置Hidden = true。如何创建一个通用的Visibility绑定,它也会更改约束:将视图高度设置为0?

1 个答案:

答案 0 :(得分:1)

有关创建绑定的教程,请参阅http://mvvmcross.blogspot.com/

上的N = 28视频

要替换现有的可见性绑定,只需根据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;
    }
}

并将此注册为Setup使用的最后一步:

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

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

}

有关替换现有绑定的更多信息,请参阅MVVMCross Binding decimal to UITextField removes decimal point


请注意,如果您要替换所有Visible个绑定,那么您可能希望替换所有VisibleVisibilityHidden - 请参阅{{3}中的注册}}