iosxamarinxamarin.iosmvvmcross

Hide UIView on view load - Xamarin iOS and MvvmCross


I'm trying to hide an ui element on view load. I'm using Xamarin iOS and MvvmCross (6.2.3.0). In my view, I create an uitextfield and add following binding:

public override void ViewDidLoad()
{
//adding uitextfield
var set - this.CreateBindingSet(MyViewConroller, MyViewModel>();
set.Bind(uitextfield).For("Visibility").To(vm => vm.FieldVisibility).TwoWay().WithConversion("Visibility");
}  

And here is my viewmodel:

public MyViewModel : MvxViewModel
{
private bool _fieldVisibility;
public bool FieldVisibility
{
get {return _fieldVisibility;}
set {
SetProperty(ref _fieldVisibility, value);
}

public override Task Initialize() {
FieldVisibility = false;
}

But when the view is loaded, my uitextfield is still visible. I tried to call RaisePropertyChanged, but it didn't help:

private bool _fieldVisibility;
    public bool FieldVisibility
    {
    get {return _fieldVisibility;}
    set {
    SetProperty(ref _fieldVisibility, value);
RaisePropertyChanged(nameof(FieldVisibility));
    }

It works, when I change some other viewmodel property and simply call:

private string _otherProperty;
        public string OtherProperty
        {
        get {return _otherProperty;}
        set {
        SetProperty(ref _otherProperty, value);
FieldVisibility = false;
    RaisePropertyChanged(nameof(FieldVisibility));
        }

but I'd like to have this uitextfield hidden on view load. Can you suggest any solution for that? I'd be very grateful for any hint. Thanks!!


Solution

  • We have strongly typed bindings nowadays for stuff like this:

    set.Bind(uitextfield).For(v => v.BindVisible()).To(vm => vm.FieldVisibility);
    

    You don't really need the visibility converter here, should work without it.

    Remember to also call Apply() on your binding set.

    Also TwoWay() won't do anything here, because there are no Binding Targets which allow for Two Way bindings for UIView visibility.